I’m currently trying to create a WebSocket object using node (version 18.17.0) but every time I try to set the protocol using the “ws” library it never sets it and it always ends up as an empty string.
Interestingly, the Vanilla JS WebSocket object can easily connect with a specified protocol (used jsfiddle.net). Wanted to know if it’s something to do with me trying to create the WebSocket object server-side compared to the browser version of JS?
I’m only using the “ws” package because node won’t let me just use the Vanilla JS WebSocket and forces me to import the library with a ReferenceError.
Any tips are appreciated!
const ws = new WebSocket("url", ["graphql-ws"]);
const ws = new WebSocket("url", "graphql-ws");
const ws = new WebSocket("url", {protocol: "graphql-ws"});
These are the three different variations I’ve tried to create for the object and for some reason the protocol never gets set (url is fine).
I also tried sleeping the function and letting it set but sleeping just makes the connection immediately error out. A while loop waiting for the readyState of the WebSocket just stays in the loop forever as well.
Expecting it to be like the browser Vanilla JS environment I used where it can pretty easily connect.
Student is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.