Broadcast to connected clients

How to broadcast a message to all connected clients? This is possible without a Collection? Should I create a Messages Collection? What is the impact of storing those messages in my Mongo database, does it scale?

Your client needs to subscribe to a null publication and use a client-side collection (does not exist on the server, so no MongoDB storage). Then you can “roll your own” publication which creates and sends the correct messages to your client. Check the docs I linked to and read on for added, changed, removed and ready.

1 Like

Got it, thanks! As I can see, I will need a Observer implementation, simulating what a Colletion.observeChanges would do.

Well, you don’t actually need to code an observer if you use pub/sub with a client-only collection. The pub/sub manages the observer for you as long as you name the “fake” data in your roll-your-own publication as belonging to that (client) collection.

Here’s a similar use case (although this isn’t a null publication):

I need a Observer implementation do listen and emit new broadcast messages, don’t I?
Used Node’s EventEmitter, that’s my conclusion, what do you think? Can it be better?


You are right. I just updated the pad to work without EventEmitter, it works.