4
votes

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

1 Answers

4
votes

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.