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:

const data = {
  id: String,
  msgType: String, // "IDENTIFICATION", "SEND_MSG", etc
  msg: String, // message sent to other client
  receiverId: String

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.


const WebSocket = require('ws')
const EventEmitter = require("events")

const eventEmitter = new EventEmitter()
const wss = new WebSocket.Server({ port: 8080 })

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    const inputData = JSON.parse(message)

    // Subscripting
    if (inputData.msgType === "IDENTIFICATION") {
      eventEmitter.on(, outputMessage => {
        if (ws.readyState === ws.OPEN) {
        } else {
          eventEmitter.removeAllListeners([])  // remove all listeners registered with this ID.

    // Sending
    if (inputData.msgType === "SEND_MSG") {
      eventEmitter.emit(inputData.receiverId, inputData.msg)

    // More message handlings ...

    ws.on("close", () => {
      // remember to un-sub



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.