I'm trying to connect my node tcp server to my socket.io server using websockify in javascript. I found the git repo for it at https://github.com/novnc/websockify/wiki/websock.js , but can't seem to find how to start it off in code. Can anyone provide an example or point me to some kind of documentation for the js version? Also, when I download websockify off of npm, I don't receive websock.js, if anyone can tell how to do that too I'd appreciate it.
-
If you really want this code to be about how to make websockify work for you, then show the specific code you are using since that's the only way someone could help you solve a problem with your code. The only generic help for websockify (absent any code) is to just go read the doc which you can do without our help and we can't help you with. – jfriend00 Jun 15 '18 at 16:44
3 Answers
Websocket needs an HTTP(s) server. You cannot connect it to a TCP server.
You can try express.js.

- 2,781
- 1
- 14
- 30
A socket.io server can only talk to a socket.io client. So, you cannot connect a plain TCP server to a socket.io server. To connect to a socket.io server, you need a socket.io client. You can certainly get the socket.io-client for node.js and then you can use that to connect to a socket.io server.
Let me explain a bit. socket.io is built on top of webSocket and has it's own connection initiation on top of webSocket. webSocket uses HTTP to initiate a connection and then after some connection handshaking switches to the webSocket protocol. If any of those pieces are missing, the connection will fail to get established and/or will shut-down shortly after connecting.
So, to connect to a socket.io server, all of the following has to happen:
- A socket.io client connection request must be initiated.
- That will make an HTTP request to the socket.io server and send some custom data.
- After some initial back-and-forth, the socket.io client will initiate a webSocket connection as the main transport.
- That webSocket connection will start with a custom HTTP request with an upgrade header that specifies a request to "upgrade" to the webSocket protocol and it will also contain some custom headers used in webSocket security.
- The server responds affirmatively to the upgrade request and also returns some security-related headers.
- The protocol is switched from HTTP to webSocket.
- All communication on this socket now uses the webSocket data frame format and associated webSocket security.
- Now, when socket.io wants to send data over this connection it adds its own higher level packet format on top of the webSocket data frame format.
So, only a socket.io client, using a supported transport (typically webSocket) can connect to a socket.io server. You can't use a webSocket client to connect to a socket.io server. You can't use a plain TCP client. You can't use an HTTP client.

- 683,504
- 96
- 985
- 979
-
While I understand all of that. My question was more pertaining to the usage of websockify, as it supposedly bridges the gap between TCP and websockets by handling the handshake protocol in each. I've set up the client side of it just fine, what I was trying to do was get my two servers communicating. – Eduardo Jun 15 '18 at 14:38
-
@Eduardo - I don't understand why you're trying to use websockify or what you're trying to accomplish with it. It appears to be some sort of a proxy. Can you describe what overall problem you're trying to solve? The first sentence of your question says this: ***I'm trying to connect my node tcp server to my socket.io server***. The usual way to solve that problem would be to use a socket.io client library from your node program to connect to the socket.io server. So, it is unclear why you're trying to use websockify at all. – jfriend00 Jun 15 '18 at 16:35
-
@Eduardo - Also, you can't use a webSocket proxy (which is what it looks like websockify is) to connect to a socket.io server. A webSocket client cannot connect to a socket.io server. – jfriend00 Jun 15 '18 at 17:06
-
Oh okay, my apologies. I didn't realize it was a proxy. Thank you for the clarification – Eduardo Jun 15 '18 at 17:54
-
@Eduardo - Does this answer your question? If so, please show that to the community by clicking the checkmark to the left of the answer. That will also earn you some reputation points for following the proper procedure here. If not, then please indicate what is not yet answered. – jfriend00 Jul 28 '18 at 23:46
Also, you can't use a webSocket proxy (which is what it looks like websockify is) to connect to a socket.io server. A webSocket client cannot connect to a socket.io server. – jfriend00

- 3
- 2
-
This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker.](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead) – Til Apr 25 '19 at 16:51