How to establish a TCP Socket connection from a web browser (client side)? How to establish a TCP Socket connection from a web browser (client side)? ajax ajax

How to establish a TCP Socket connection from a web browser (client side)?


I've read about WebSockets but they don't seem to be pure "sockets", because there is an application layer protocol over them.

[Is the] websocket protocol so simple to implement that [it is] "almost"-sockets?

Allowing regular socket connections directly from the browser is never going to happen because it opens up a huge risk. WebSockets is about as close to raw sockets from the browser as you are going to get. The initial WebSockets handshake is similar to an HTTP handshake (allowing web servers to proxy/bridge it) and adds CORS type security. In addition, WebSockets is a message based transport (rather than streaming as raw TCP) and this is done using a two byte header on each message frame.

Even flash is not able to quite make raw TCP connections. Flash sockets also add CORS security, but instead of an in-band handshake, flash socket connections make a connection to port 843 on the target server to request a security policy file.

Is there any way of doing a pure socket connection from a web browser, to enliven webpages?

Yes, you can use my websockify bridge/proxy which allows a WebSockets enabled browser to connect directly to a TCP socket via websockify.

But about HTML5, Why are they called WebSockets if they aren't Sockets?

WebSockets are a transport built on TCP sockets. After the handshake there is very minimal overhead (typically just a two byte header).


I can't improve on Kanaka's answers to your secondary questions, and I know this question is a year old. But for the main question, Is there any way of doing a pure socket connection from a web browser, to enliven webpages? There is a project called the Java / JavaScript Socket Bridge that might be what you (or anyone coming across this page from a Google search) are looking for. The advantage of this method over what others have mentioned is that it does not require either a client-side or a server-side service to be run. So, for instance, if you wanted to implement an IRC client purely in JavaScript but your web host does not allow you sufficient rights to proxy the connection, this Java applet would be the way to go. The only concern is making sure the client has Java installed and allowed.


You can just send data between a client and a server with WebSockets. Simply speaking, the only difference that WebSockets introduces is that the client:

  • adds some header bytes, like the type of data and the length
  • adds masks and encodes the data using them

The server also has to add header bytes, but does not need to encode the data.

If you implement the protocol correctly (server side, that is, since the browser already has an implementation), you can use it with ease to send text and binary data. (Although browser support is narrow, especially for the latter.)