S
S
semki0962020-02-06 00:51:54
Node.js
semki096, 2020-02-06 00:51:54

Why is my js code not working on node?

Don't drink noob. Code in myserver/client.js file

var socket = new WebSocket("wss://stream....блабла");
socket.onopen = function() {
    console.log("Соединение установлено.");
};

I launch from the console node myserver/client.jsI get an error Error: WebSocket is not defined

If I run it from a browser, then everything works. Apparently it is necessary to install some libraries? Please tell me the reason why this does not work, and which library can be used.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
semki096, 2020-02-06
@semki096

I think I found the reason - Node.js does not have a built-in WebSocket constructor (unlike browsers), so you need to install a third-party websocket package. https://github.com/vitalets/websocket-as-promised/... Nevertheless, I would be grateful if someone supplements this topic with their information.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question