garply
garply

Reputation: 23

NodeJS mixing TCP sockets with websockets

I have a NodeJS TCP socket server running and I'd like to add support for websockets. Has anyone tried mixing the two in one server?

It seems like you could create the HTTP server, implement the websocket upgrade handshake, and then hand off the socket to the TCP socket server. Is that the right approach?

I considered using socket.io, but it appears that would cut out TCP sockets.

Upvotes: 2

Views: 1031

Answers (1)

Jeremy Roman
Jeremy Roman

Reputation: 16355

Socket.IO supports a number of fallback technologies (e.g. JSONP long polling) which make passing the raw net.Socket back a hack that will often break.

Instead, I would suggest abstracting away the actual application logic and then connecting it via some form of adapter to both the TCP socket and Socket.IO implementations. You can then interact with each on its own API.

The APIs are somewhat similar, so this shouldn't be too difficult, but it isn't just a matter of getting some existing net.Socket object out of Socket.IO.

Upvotes: 1

Related Questions