sturdy-websocket icon indicating copy to clipboard operation
sturdy-websocket copied to clipboard

how use this lib in nodejs

Open rj03hou opened this issue 2 years ago • 3 comments

rj03hou avatar Jan 31 '23 08:01 rj03hou

@rj03hou A bit of a late reply, but I found a solution through ws

npm install ws
import SturdyWebSocket from "sturdy-websocket";
import WebSocket from "ws";

const ws = new SturdyWebSocket.default("wss://whateverwebsocket", {
    wsConstructor: WebSocket
});

ws.onopen = () => {
  console.log("Connected!")
}

This works since WebSocket needs a constructor in the global scope and Node.js doesn't provide one. By using the ws library you can bypass this and it should work.

IlyaSinyugin avatar Jul 08 '23 10:07 IlyaSinyugin

@rj03hou A bit of a late reply, but I found a solution through ws

npm install ws
import SturdyWebSocket from "sturdy-websocket";
import WebSocket from "ws";

const ws = new SturdyWebSocket.default("wss://whateverwebsocket", {
    wsConstructor: WebSocket
});

ws.onopen = () => {
  console.log("Connected!")
}

This works since WebSocket needs a constructor in the global scope and Node.js doesn't provide one. By using the ws library you can bypass this and it should work.

sturdy-websocket version is ? i have no default function in the libiary

bigtotorouk avatar Jan 23 '24 02:01 bigtotorouk

@rj03hou A bit of a late reply, but I found a solution through ws

npm install ws
import SturdyWebSocket from "sturdy-websocket";
import WebSocket from "ws";

const ws = new SturdyWebSocket.default("wss://whateverwebsocket", {
    wsConstructor: WebSocket
});

ws.onopen = () => {
  console.log("Connected!")
}

This works since WebSocket needs a constructor in the global scope and Node.js doesn't provide one. By using the ws library you can bypass this and it should work.

sturdy-websocket version is ? i have no default function in the libiary

version 0.2.1 works for me and include the default function

liwenzhu avatar Feb 18 '24 12:02 liwenzhu