At a distant space view post, we chat about codes.

Node.js: Send messages between WebSocket clients with EventEmitter.

WebSocket(WS) has the “one endpoint to do it all” model. Sometime we need to build structure ourselves. Here we send messages between different WebSocket clients with EventEmitter.

Message Schema:

Since the WS .on('connection') callback has no way to pass in data, we let client send identification data to server after connected. Use an enum flag to differentiate types of message, and types of client if needed.




On connect, all clients are required to send server an identification message, then server starts to subscribe to an EventEmitter on current client ID.

When receiving more WS messages later, server sends the message into EventEmitter stream, internally. In the receiving client’s EventEmitter callback, it holds a reference to its ws object, which we use to actually send the message through the internet.

Notice: If you planned to send and receive EventEmitter message across your node app, the eventEmitter must be the same instance.


This is good for single node. If you’re looking for a more scalable solution and richer API, I recommend looking into and

Take a look at this:


  • Michael Kuhlman

    08/28/2019 at 3:37 PM → Reply

    I just wanted to followup on the request I submitted through your contact form a couple weeks ago. I pasted it below for your reference.

    Is it okay if we feature your site in our next email newsletter? It’s a perfect fit for a piece we’re doing and I think our audience would find some of the content on your site super useful.

    I know you’re probably busy, so just a simple yes or no would suffice.

    Many Thanks,

    • dahis39

      04/13/2020 at 5:53 AM → Reply

      Absolutely yes and thanks you. I’m very sorry to take that long to get back to you. I don’t have email reminder setup for this site’s comment section, but I will do it now.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Node.js: Send messages between WebSocket clients with EventEmitter.