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:

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.