4

Note: This is not about using both node.js and HTML5 sockets. I'm also not interested in discussing the merits of the setup I'm describing.

node.js runs on the server, and, since it supports connecting via sockets as a client, it can act as a middle layer between an HTML5/JS client and server that uses TCP/IP (such as a database server.) So, both node.js and WebSockets include ways of opening socket connections to a server.

My question is, has anyone successfully ported a node.js script to WebSockets, i.e., cut node.js out of the equation, so that your web browser connects to the database directly? I imagine it would look like:

  1. cut out everything to do with HTTP
  2. port the usage of all node.js-specific functions to use the WebSockets API

If this has been done, was it a lot of trouble or were the node.js and WebSockets APIs relatively similar?

1 Answer 1

4

Your question is a bit hard to parse but I'll take a stab.

If you are interested in connecting from a WebSockets client (browser) to an arbitrary TCP socket server, then you might be interesting in wsproxy which is a generic WebSockets to TCP sockets proxy. wsproxy is included with noVNC (HTML5 VNC client) and has three reference implementations in C, python and Node (node.js).

If you are interested in adding WebSockets support to a specific given server (i.e. the database server), then you might find this fork of libvncserver. It has support for clients that speak WebSockets (i.e. noVNC) so no proxy is needed.

The basic wsproxy proxying functionality was pretty straight forwards to implement. The trickiest part is that the current WebSockets draft in use (v76) does not specify a binary transfer payload (only UTF-8) so wsproxy base64 encodes/decodes all traffic to/from the WebSockets client. The implementation of WebSockets connections in libvncserver was somewhat more tricky because libvncserver has some pretty hard-coded ideas about buffering/framing that needed to be worked around.

Disclaimer: I'm responsible for noVNC, wsproxy and the WebSockets patches to libvncserver.

Sign up to request clarification or add additional context in comments.

3 Comments

At first, I was going to ask, "Why do you need a proxy?" Your docs say wxproxy "translates WebSockets traffic to normal socket traffic," and the notion that WebSockets traffic wasn't normal socket traffic is new to me. I thought the whole idea with WebSockets is that they were normal sockets. So servers always need to explicitly support WebSockets then? That's unfortunate...I wonder why it was done this way...
(I guess that's why you had trouble parsing my question...thanks for working with it.)
Yeah, in addition to the handshake (which includes origin info and makes WebSockets more HTTP upgrade friendly), WebSockets frames have a leading 0 (zero) and trailing 255. My understanding is that having a higher level framing makes the protocol much easier to work with. As an example, VNC/RFB is really hard to work with because there is no framing. In fact the next iteration of the WebSockets protocol will not only be framed but likely have a header with payload type and length info (rather than using frame delimiters as the current one does). The name doesn't help with the confusion though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.