0

Right now I am setting the nodejs server to use webscoket to receive the data from other server

I have tried to use websocket(ws) in nodejs server, and it can connect to the other server

const WebSocket = require('ws');
var ws = new WebSocketClient(); 
ws.open("ws://ws.something/?token="+Token);

But when I try to connect using socket.io, the console does not display a debug message, and no data is received

var io = require('socket.io-client')

const socket = io.connect('ws://ws.something/?token='+Token);
socket.on("connection", function(mSocket){
  console.log('debug message')
  mSocket.on("message", function(myData){
    console.log(myData)
  });
});
socket.on('error', function (err) {
  console.log(err);
});

Can socket.io connect to ws:// ? Or is there something wrong with my code? (This is the first time to use it)

test test
  • 63
  • 1
  • 6

1 Answers1

3

No, it cannot.

Socket.IO is a layer on top of several transports, with Web Sockets being only one of them. Socket.IO clients can only connect to Socket.IO servers.

If you want to connect to your Web Socket server from a browser, use the browser's built-in Web Socket client:

const socket = new WebSocket('wss://example.com');

https://developer.mozilla.org/en-US/docs/Web/API/WebSocket

Brad
  • 159,648
  • 54
  • 349
  • 530