Moshe Shaham
Moshe Shaham

Reputation: 15984

Can Google Pub/Sub be used for chat messaging?

I'm developing an app that has chat between users. Does it make sense to use Pub/Sub for this purpose? I couldn't find in the documentation as a possible use case. Are there any good reasons why it's not a good fit?

Upvotes: 3

Views: 6039

Answers (3)

Apoorv Bhardwaj
Apoorv Bhardwaj

Reputation: 1

Pub/Sub for chat? No. As it is rightly mentioned above, Pub/sub has 2 major use cases, for data ingestion and for decoupling the system.

I would recommend you search a bit about web sockets if are still interested making a chat app.

Upvotes: -1

Kamal Aboul-Hosn
Kamal Aboul-Hosn

Reputation: 17216

Google Cloud Pub/Sub would not be a good fit for communicating with end user devices in a chat product. Cloud Pub/Sub is designed for torrents: a relatively small number of long-lived streams that require high throughput. In a chat app, you need trickles: a very large number of ephemeral streams that have a low throughput.

Cloud Pub/Sub quotas only allow for 10,000 topics project and 10,000 subscriptions per topic or per project. When setting up a new streaming pipeline, one generally creates the topic and its subscriptions once at the beginning and then publishes and subscribes from that point on. Therefore, Cloud Pub/Sub is a better fit for server-to-server communication or for large-scale streaming event ingestion, often in connection with Dataflow and/or BigQuery.

The permission model for Cloud Pub/Sub doesn't make it a good choice for a chat app, either. Permission to write to or read from a topic or subscription requires the user to be authenticated. Authenticating each individual user with permissions in your project is not really feasible, so you are left with the option of using common credentials for all of your users, which could be insecure.

That's not to say that Cloud Pub/Sub couldn't be part of a chat service, it just wouldn't be used to deliver messages to specific users. In fact, Cloud Pub/Sub can be used as the transport mechanism for messages sent to Hangouts Chat Bots. In this situation, though, the response to the user goes directly to the Hangouts Chat API, not back through Pub/Sub. Therefore, if you were implementing a chat service, you could potentially have your users send messages to a frontend server you design that then publishes those messages to Cloud Pub/Sub. Another server you build could be a subscriber to those messages and then deliver the appropriate messages to the appropriate individual users. In this situation, Cloud Pub/Sub is used for ingesting all of the messages from your frontend server (torrent) and delivery of messages to individual users falls to some other mechanism (trickles). The product in Google Cloud designed for this individual device delivery is Firebase Cloud Messaging,

Upvotes: 3

John Hanley
John Hanley

Reputation: 81376

Can Google Pub/Sub be used for chat messaging?

Yes. I would not use Pub/Sub for person-to-person chat messaging. Pub/Sub is designed for systems to de-couple themselves to support distributed systems.

Why not use Pub/Sub:

  • Permissions. You will need to create service account credentials and distribute this Json file to each subscriber. This is not secure.
  • Each chat person will need a subscription to a Pub/Sub Topic and then poll that subscription. This means one subscription per chat person. This is neither a good design nor inexpensive.

There are much better solutions for chat.

Upvotes: 2

Related Questions