Issue
First things first: My Pub/Sub knowledge is basic, my server-sent event knowledge even below that.
What I am wondering is how to build a single page application (with 1-10 concurrent users, something small and internal) that can (near-)instantly show (not write just read) Pub/Sub messages as they are coming in. I've so far been able to botch together a Java Servlet that starts an asynchronous pull and writes to an EventSource in the frontend, however I'm asking myself whether that is complete nonsense* to begin with and whether there is something else out there that I should be looking into (I guess I'm asking for the right keywords).
* it works 1/10 times and only locally right now.
Any CONSTRUCTIVE tips are appreciated.
Solution
The correct answer here really depends on the use case. One thing that is certain is that the client code itself calling into Cloud Pub/Sub would not be the right thing to do. Cloud Pub/Sub is not designed to scale in the number of subscriptions, both per topic and per project; the limit on each of these is 10,000.
Firebase Cloud Messaging would be a good candidate to use here. It is designed to deliver messages to end users, including those accessing a web page. Depending on the original producer of the messages, Cloud Pub/Sub may be used to send messages from one server to another and these messages may ultimately be fed to Firebase.
Some design questions to consider would be:
- Do all users need to see all messages or does each need to see a subset of messages?
- What is the original source of the messages? Are they already in Cloud Pub/Sub and now they need to be distributed to the individual users or are other transport layers being considered?
- If the messages are in Cloud Pub/Sub, how many frontend servers will you have? If it's more than one, then you'll need to have a subscription per server in order for all instances to receive the full feed of messages that are then sent on to users via other means.
Answered By - Kamal Aboul-Hosn