Dshiz
Dshiz

Reputation: 3331

How to send this request to websockets using the `ws` library

I am working with a private API, trying to send the following request with websockets using the ws library. I can get this request to work using the Simple WebSocket Client extension in Google Chrome, but everything I've tried with ws 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.

Request: {"message":"ping"}

My code:

            const ws = new WebSocket(channelData.connectUri)
            ws.onerror = error => {
                console.log('Connection Error', error)
            }
            ws.onopen = () => {
                console.log("Connected")
                ws.emit("message", {"message":"ping"}) // this sends me my own message
                // it should send me this:
                // {"topicName": "channel.metadata", "eventBody": {"message": "pong"}}

                // ws.emit("message", "ping") // attempted this also, without success
            }
            ws.on("message", msg => console.log(msg))

I have tried using ws.send({"message":"ping"}) but I get this error:

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.<anonymous> (/Users/user/Documents/GitHub/lib/node_modules/ws/lib/websocket.js:671:15) {
  code: 'ERR_INVALID_ARG_TYPE'
}

This ws.send("message", "ping") does nothing.

Upvotes: 1

Views: 2091

Answers (1)

Gershom Maes
Gershom Maes

Reputation: 8140

You can only send String or Buffer data with WebSocket.prototype.send. Fortunately, JSON.parse + JSON.stringify quickly bridge the gap between structured data (Object, Array, etc.) and String.

Try:

ws.send(JSON.stringify({"message":"ping"}));

The receiving end will get a String, not an Object - in order to recreate the structured data, you can then perform:

let structuredData = JSON.parse(stringReceivedFromWebSocket);

Upvotes: 3

Related Questions