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

Viewed 387

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?

1 Answers

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.

enter image description here

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