I am using easy-rtc with socket.io for multiuser experience in my metaverse project. We maintain the connection in socket.io using polling and web-socket. I am facing problem in web-socket connection when I am trying to give path to the socket.io.
Firstly, I am putting forward the code where I am not using any path. You can get the complete code from here. https://github.com/networked-aframe/networked-aframe/blob/master/server/easyrtc-server.js
The web-socket connection request which I am getting from this code is giving me a 101 changing protocols as expected.
I am even getting the response for the web-socket request as connecting in the Postman.
Now, My backend is working at localhost:3333/api/ . I am trying to change the path of socket.io in the client and server as shown in the images below. but I am not getting the 101 changing protocol response from web-socket connection request. This is the code after path change.
This is the response which I am getting when running the code after doing the path changes.
When I am trying to hit the web-socket request in the postman also I am not getting any connection. One observation I have made is that in the request URL, it is not taking the localhost:3333/api/ path. (If this is the issue, suggest any work arounds for this.)
Without getting the web-socket connection, the server is not efficient for communication.
Anyone suggest working solutions for this.
I was able to reproduce it.
A working solution
The problem is in your server code. You are doing something like this:
The last line is not required. It should be used only with socket server istances not yet attached to any http server. In the previous line you are already importing the socket.io main function, calling it, attaching to the HTTP server.
Therefore, try removing the
.listen
call and you will receive the101 changing protocol http status code
again.With the above proposed solution, you can listen on socket events directly using the
io
istance. Like this:Why it was working without the path?
Well, the first code snipped you provided should not work at all. From what I can see you were doing this:
The above code can't work. The socketIo variable is an anonymous function. The
listen
method is not defined. Are you sure you have not initialized the module in a different way? Maybe are you using and old version of socket.io?A better approach
Anyway, I suggest the new fresh, clean syntax socket.io documentation suggests;
Backend:
Client (I'm using a CDN but you could rely to your server for the socket.io-client code):