title: Send a custom event with ws
body: <p>I am trying to listen to a custom event on the client with native websockets and with ws on the back end.</p>
  <p>I first create a pool of connected clients.</p>
  <pre><code>import express from 'express'
  import http from 'http'
  import WebSocket from 'ws'
  import { v4 as uuidv4 } from 'uuid'
  import processes from './routes/process.js'
  import { EventEmitter } from 'events'
  
  // Express
  const app = express()
  
  // Initialize a simple http server
  const server = http.createServer(app)
  
  // Initialize the WebSocket server instance
  const wss = new WebSocket.Server({ server })
  
  let connections = {}
  
  wss.on('connection', (socket) =&gt; {
    socket.id = uuidv4()
    connections[socket.id] = socket
    socket.send(socket.id)
  
    console.log(`Client connected ID: ${socket.id}`)
  
    socket.on('close', () =&gt; {
      delete connections[socket.id]
      delete socket.id
    })
  })
  </code></pre>
  <p>And on react I store the <code>socketID</code></p>
  <pre><code>const [socketID, setSocketID] = useState('')
  
  useEffect(() =&gt; {
    const socket = new WebSocket('ws://localhost:4000')
    socket.onmessage = (message) =&gt; setSocketID(message.data)
  }, [])
  </code></pre>
  <p>What I would like to do is then emit a message to the specific client using the <code>socketID</code>, I use axios from react to send the <code>socketID</code> in a request to an express route.</p>
  <p>Then back at the server I use EventEmitter to pick up the client ID from the route, I have omitted the details cause is a bit lengthy, but at this point the server correctly identifies the client, the problem seems to be with sending a custom event.</p>
  <pre><code>connections[socketID].send('notify', 'Hello specific client')
  </code></pre>
  <p>And on the client I listen for the custom <code>nofify</code> event</p>
  <pre><code>useEffect(() =&gt; {
    const socket = new WebSocket('ws://localhost:4000')
    socket.onmessage = (message) =&gt; setSocketID(message.data)
    // Set notify listener to notify this specific client only
    socket.addEventListener('notify', (data) =&gt; {
      // I am expecting 'Hello specific client'
      console.log(data)
    })
  }, [])
  </code></pre>
  <p>When I inspect react dev tools I can see that the <code>socketID</code> is receiving the 'custom event' and it changes the <code>socketID</code> to the word <code>notify</code>. So clearly ws is just sending the word notify and not as a custom event but as normal message that the client picks up as <code>onmessage</code> rather than the eventListener.</p>