Possible for WebSocket client on browser to talk to TCP Socket server?

10,372

Solution 1

TCP and WebSocket are not the same protocol or framing, so wiring them up blindly isn't going to work. Well ... technically, websocket is an upgrade of http which is layered on ssl (optionally) which in turn is layered on tcp.

TCP can be thought of as a stream of bytes, while WebSocket is a set frames.

WebSocket frames can be any of the following:

  • TEXT - consists of 1 or more frames making up a single UTF8 message
  • BINARY - consists of 1 or more frames making up a byte array message
  • CONTINUATION - used by TEXT and BINARY to piece together 2 or more frames.
  • PING - obvious
  • PONG - obvious
  • CLOSE - request a close/disconnect of the protocol.

In short, you'd need to implement the websocket protocol framing in order to have TCP wired up to websocket. And you'll need to implement basic HTTP UPGRADE in order to reach that point.

Solution 2

Absolutely! That is the purpose of websockify.

Of course your WebSocket client application will need to be able to implement the protocol of the TCP server. For example, noVNC is a browser VNC client that implements the RFB protocol and by using websockify it can connect to a normal TCP based VNC server.

Disclaimer: I created both websockify and noVNC

Share:
10,372
jaffa
Author by

jaffa

Updated on July 21, 2022

Comments

  • jaffa
    jaffa almost 2 years

    Is it possible to have a TCP Socket server running which accepts incoming connections from WebSocket clients? I have a TCP Socket server and I'd like to be able to test this in a browser. Is this possible?

  • jaffa
    jaffa over 11 years
    So in short, no its not possible?
  • Joakim Erdfelt
    Joakim Erdfelt over 11 years
    It is possible, but with a lot of extra work. It will not work "out of the box" so to speak.
  • kanaka
    kanaka over 11 years
    To clarify, it is not stream vs frames, it's stream vs messages. The TCP protocol carries a stream of bytes that must be re-assembled by the application into the original messages. The WebSocket protocol carries messages so each message that is sent is received as a single whole message. The term "framing" is a concept that applies to both TCP and WebSockets (any wire protocol actually).
  • kanaka
    kanaka over 11 years
    @jaffa, yes, that's the main use case.
  • HackNone
    HackNone over 9 years
    But this is like a proxy? Just like artemyankov.com/tcp-client-for-browsers?
  • kanaka
    kanaka over 9 years
    @HackNone technically a "bridge" (different protocols on each side), but yes it's similar to WebTCP (linked in that article). Just a few differences: WebTCP is both younger and hasn't had updates in the past year. WebTCP is implemented in JS/node, websockify has implementations in several languages (including JS). websockify is lower level (raw websock data, no JSON message wrappers). WebTCP has a huge security issue because it allows browser to connect to arbitrary remote destinations; see github.com/kanaka/websockify/issues/3 websockify has large deployments (e.g. OpenStack).
  • Cameron
    Cameron about 6 years
    So you're saying I can speak to a program using TCP, just through WebSockets? Does that mean my Server doesn't even need WebSockets (just my browser)?
  • kanaka
    kanaka about 6 years
    @Cameron your server either needs to support WebSockets or you need to use something like websockify to bridge between WebSockets and TCP.
  • Cameron
    Cameron about 6 years
    Yes. I think I was just confused initially. It sounded like I could talk directly to a socket using a websocket client. But that’s not the case. However, with the websockify bridge it’s actually not much extra work at all. I ended up demoing it in one my network engineering classes and it seemed like everyone was really impressed so thanks for making this software
  • Predrag Manojlovic
    Predrag Manojlovic about 3 years
    Websockify is broken and it doesn't work. Maybe some alternative should be offered.
  • kanaka
    kanaka about 3 years
    @PredragManojlovic websockify works and has been used in production for years. Can you please be more specific or ask the upstream project maintainers (I no longer am) if you have a specific context where you are having trouble with it?
  • Predrag Manojlovic
    Predrag Manojlovic about 3 years
    @kanaka Latest release 0.9.0 doesn't work in any context on windows. Even the simplest example will drop the "Client disconnect" error. Before you ask, I've been using a binary frame. On the other side, the 0.8.0 version won't run on python 3 as it is complaining about the missing module SocketServer. On Linux it does work. That is why I'm looking for a more stable solution.
  • kanaka
    kanaka about 3 years
    @PredragManojlovic Websockify implements a very simple protocol. There are implementations of websockify in just about every common language. Some of them are under the official project on github at github.com/noVNC, but many are in language specific package repositories. Some of them definitely work on Windows. I think the default python one can be made to run on Windows: github.com/novnc/websockify/blob/master/Windows/… However, I have not followed the current state of that version in years.