Can we pull messages from Pub/sub topic to Angular? - python

I'm able to push messages from Python backend (which is on a VM instance) to topic & see messages on Pub/sub topic. But there is no code for pulling data from topic using angular. I want to pull that data & show it to Angular UI. Could you please help me with this?

With PubSub, there are 2 subscriptions mode that imply 2 kind of authentication:
Push Subscription, where the sender (PubSub subscription) need to be authenticated to push the message to a secure endpoint
Pull Subscription, where the client need to be authenticated to be able to get the messages.
So, in your case, you need to authenticate your Angular app on PubSub Pull Subscription to be able to read the messages. You have 2 solutions:
Either you generate a service account key file and you put it in your static code. It's obviously a stupid idea, because you share publicly a secret, and thus it's like if there is no security!
Or, because the previous solution is like having no security, you can make the pull subscription public. Grant allUsers as PubSub Subscriber.
It will work, but there is a design issue: anyone will be able to subscribe to your Pull subscription, and because the messages aren't duplicated between the subscriber, you will potentially loose messages.
A better solution could be to serve an endpoint in streaming, with Cloud Run for example, to authenticate your user on the Cloud Run endpoint, and to stream the messages from PubSub pull subscription through Cloud Run streaming connexion.
Like this, you add a security layer, something like a proxy.

Related

Using FastAPI for socket chat system?

How would one implement a comprehensive chat system using sockets within FastAPI. Specifically keeping the following in mind:
Multiple Chat rooms many-to-many between users
Storing messages with a SQL or NoSQL database for persistence
Security: Authentication or possibly encryption
I've looked at some libraries, but actual useful implementations are far between, regrettably.
Any advice or redirects towards places for more information will be of great help!
For chat rooms you could use FastAPI builtin websockets support and add redis pubsub or PostgreSQL pg_notify to it for sending messages to all participants in the room.
Storing messages in PostgreSQL is a solid choice because of its long history and stability.
Authentication can be handled by OAuth2 provider in FastAPI. Authorization can be handled by OAuth2 scopes that is hidden in the Advanced Security section in the FastAPI Documentation. Encryption is provided by HTTPS and reverse proxy that you put in front of your app.
There aren't any fully ready made libraries that provide everything out of the box. But breaking down the problem in to smaller pieces and then working on those will get you pretty far.
Write down what fields/data you want to store about your users, chat rooms, messages.
Implement those basic models in FastAPI probably using SQLAlchemy.
Wire up those models to api endpoints so that you can use those models in Swagger (list chatrooms, get and post messages into chatrooms).
Implement a websocket endpoint in FastAPI that will echo back everything sent to it. That should allow you to wire up some client side javascript for sending and receiving messages from the websocket.
Modify your exising message storing endpoint to push the same message also to redis publish topic and change your websocket endpoint to subscribe to the redis subscribe topic.
Add authentication to your endpoints. At first basic user/password, later more advanced configurations.
Add reverse proxy with https in front and voila.

E-commerce website to Google Cloud database

How does cloud pubsub receive user activities on a e-commerce website?. Is the endpoint going to be a URL or an IP address and a port
On this graph you can see the workflow. You will need your app or your service to publish a message to the Pub/Sub topic which will then the topic will pass to a subscriber via Pull or Push.
To publish a message you can call a URL like
"http s://pubsub.googleapis.com/v1/projects/myproject/topics/mytopic:publish" as mentioned on the docs
or with the help of a Client library wich will need you to call a method, fo example in NodeJS it will be somehting like
const messageId = await pubSubClient.topic(topicName).publish(dataBuffer);
console.log(`Message ${messageId} published.`);
that is also described on the docs mentioned before.
I'm not really getting the use of Pub/Sub since it will be better if you explained your arquitecture and use case, but for example here is a quick architecture model on GCP

How to check message delivery in Firebase Cloud Messaging? [duplicate]

I'm sending messages to several iOS Apps via
FCM by using their HTTP protocol API,
and I need a list of message records including the payload, sent time, and platform like what I could monitor in the Firebase Notification Console.
It seems that messages sent via HTTP API wouldn't be recorded and shown in the console (only those sent manually in the console would).
I haven't found any API documents related to those messaging events/log fetching.
Is there a way to fetch this kind of message information from FCM?
As of August 2018, stats for messages sent using the FCM API are now visible from the console. From the Cloud Messaging section, click on the Reports tab. From there, you will be able to filter by message type (notification, data, and all)
It would seem that the Impressions and Opens are still only available for messages sent using the console. But Sends for sure now counts the messages sent using the FCM API.
Reference: https://firebase.googleblog.com/2018/08/in-app-messaging-crashlytics.html
Update: The FCM Diagnostics page has been disabled since last year.
If you have a Play Dev Console account and if your app is in at least Alpha Testing, you can make use of the Diagnostics and Statistics page. However, if you're looking for an API, there is currently no API available for it.
Otherwise, I think most of the details you need (like the payload and time sent) can be logged from your app server alone.
And just to support what you already mentioned in your post, as stated by #FrankvanPuffelen here:
The Firebase Notifications charts only show analytics for messages that were sent using the Firebase Notifications panel.
There is currently no public API to send Notifications to audiences. The web interface in the console is the only way to send them.
There is currently no API to feed your own FCM messages into the Firebase Notifications analytics panels.
If you want to fetch data on push notification client side you could use the BigQuery exports of Analytics data.
Here is a list of events, as you can see there are several events on notifications, including notification_open and notification_send.
Unfortunately notification_send seems to be available only for Android apps. I don't know why since the Firebase GUI console Message Delivery Report does contain iOS sends as well.

How do you register a python-based client with GCM (Google Cloud Messenger) to get a registration_id for the device?

I have been trying to figure out how to use one of the following python packages to create a python-based client that is capable of receiving XMPP-based messages via Google Cloud Messenging.
https://github.com/geeknam/python-gcm
https://github.com/daftshady/py-gcm
https://pypi.python.org/pypi/gcm-client/
https://github.com/pennersr/pulsus
From all I can see, (e.g., the documentation for gcm-client), these packages can send messages to other clients that are identified by registration_id. But how do I get a registration IDs for each client in the first place? In other words, how do I register the client-app that I am creating so that it can receive messages?
It is starting to seem to me that these are not clients per-se, but just libraries that can be used to push messages to clients. I hope that I am wrong about that and just missing a key concept.
Each client application has to call the getRegistrationId() to get the registration id once. Then they can receive messages. A more detailed function call is here
I hope this give you an idea on client devices. :)

What is the correct way to expose an AWS in an API without giving out your keys?

Sorry about the awkward title.
I am building a Python API. Part of it involves sending and receiving data to an Amazon SQS to communicate with some stuff on an EC2 instance. I don't want to distribute the API with my amazon keys in it though.
What is the correct way around an issue like this? Do I have to write a separate layer that sits in front of SQS with my own authentication or is there a way to add permissions to amazon keys such that uses could just send and receive messages to SQS but couldn't create additional queues or access any other web services?
It depends on your identity requirements. If it's ok for your clients to have AWS accounts, you can give their accounts permission to send messages to your queue. If you want your own identity, then yes, you would need to build a service layer infront of AWS to broker API requests

Categories