1

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.

4
  • did you consider using send, like on any normal websocket? ws is spec-compliant and the client behaves like the normal browser websocket? Commented Mar 12, 2021 at 17:13
  • Yes, but I get an error. I've added it to my question... Commented Mar 12, 2021 at 17:15
  • Maybe you should read a minimal working websocket example, and see how websocket works. PS: the error message even states that very explicitly. Commented Mar 12, 2021 at 17:17
  • I know how websockets works. Nothing I've attempted has worked for this, which is why I'm asking the question, because clearly I'm missing something. Commented Mar 12, 2021 at 17:19

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - appreciate the confirmation and nice answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.