title: How to send this request to websockets using the `ws` library
body: <p>I am working with a private API, trying to send the following request with websockets using the <code>ws</code> library. I can get this request to work using the <a href="https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo/related?hl=en" rel="nofollow noreferrer">Simple WebSocket Client extension</a> in Google Chrome, but everything I've tried with <code>ws</code> is not working. The websockets connection is established. I know this because I'm getting a generic heartbeat message and I'm able to get events for other purposes. But this simple piece has me stumped, mostly because the private API's documentation for websockets usage sparse, to say the least, if not completely non-existent.</p>
  <p>Request: <code>{&quot;message&quot;:&quot;ping&quot;}</code></p>
  <p>My code:</p>
  <pre><code>            const ws = new WebSocket(channelData.connectUri)
              ws.onerror = error =&gt; {
                  console.log('Connection Error', error)
              }
              ws.onopen = () =&gt; {
                  console.log(&quot;Connected&quot;)
                  ws.emit(&quot;message&quot;, {&quot;message&quot;:&quot;ping&quot;}) // this sends me my own message
                  // it should send me this:
                  // {&quot;topicName&quot;: &quot;channel.metadata&quot;, &quot;eventBody&quot;: {&quot;message&quot;: &quot;pong&quot;}}
  
                  // ws.emit(&quot;message&quot;, &quot;ping&quot;) // attempted this also, without success
              }
              ws.on(&quot;message&quot;, msg =&gt; console.log(msg))
  </code></pre>
  <p>I have tried using <code>ws.send({&quot;message&quot;:&quot;ping&quot;})</code> but I get this error:</p>
  <pre><code>node:buffer:323
    throw new ERR_INVALID_ARG_TYPE(
    ^
  
  TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object
      at new NodeError (node:internal/errors:329:5)
      at Function.from (node:buffer:323:9)
      at toBuffer (/Users/user/Documents/GitHub/lib/node_modules/ws/lib/buffer-util.js:97:18)
      at Sender.send (/Users/user/Documents/GitHub/lib/node_modules/ws/lib/sender.js:261:17)
      at WebSocket.send (/Users/user/Documents/GitHub/lib/node_modules/ws/lib/websocket.js:361:18)
      at WebSocket.ws.onopen (/Users/user/Documents/GitHub/lib/server.js:55:20)
      at WebSocket.onOpen (/Users/user/Documents/GitHub/lib/node_modules/ws/lib/event-target.js:144:16)
      at WebSocket.emit (node:events:378:20)
      at WebSocket.setSocket (/Users/user/Documents/GitHub/lib/node_modules/ws/lib/websocket.js:177:10)
      at ClientRequest.&lt;anonymous&gt; (/Users/user/Documents/GitHub/lib/node_modules/ws/lib/websocket.js:671:15) {
    code: 'ERR_INVALID_ARG_TYPE'
  }
  </code></pre>
  <p>This <code>ws.send(&quot;message&quot;, &quot;ping&quot;)</code> does nothing.</p>