socket.io client works in browser but not in node.js

183 views Asked by At

The following code only works in the browser. It does not work in node.js

let socket1 = io('http://localhost:3031/nsp')

socket1.on("connect", (error) => {
  console.log("socket1: connect")
});

I am connecting to a netty-socketio (v1.7.7) java server. I am able to connect to the root namespace on both browser and node.js clients and everything works as expected. However, if I try to connect to a namespace, only the browser client can connect as expected.

Furthermore, I can see on the server that the node.js client (v2.4.0) is connecting to the root namespace when it should be connecting to the named namespace "nsp". As such, the clients join the root namespace and seemingly never join the "nsp" namespace.

On further inspection, the only event I can get to fire on the node.js client, when specifying a namespace, after connecting is "ping" all other events (connect,connect_error,error,reconnect...) never trigger.

Update: the above code works when connecting to a node.js server, so the issue appears to be with the netty-socketio server.

Here is how the netty server is initialized:

Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(3031);

server = new SocketIOServer(config);
server.addNamespace('/nsp')

1

There are 1 answers

0
Jack On

I finally figured it out. I had to revert the node.js client version to 1.7.4 (https://www.npmjs.com/package/socket.io-client/v/1.7.4) to get it to work. Not sure why I was forced to use such an old client to be able to use namespaces.