85

How to Use Sockets in JavaScript\HTML?

May be using some cool HTML5?

Libraries? Tutorials? Blog Articles?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Rella
  • 65,003
  • 109
  • 363
  • 636

3 Answers3

79

How to Use Sockets in JavaScript/HTML?

There is no facility to use general-purpose sockets in JS or HTML. It would be a security disaster, for one.

There is WebSocket in HTML5. The client side is fairly trivial:

socket= new WebSocket('ws://www.example.com:8000/somesocket');
socket.onopen= function() {
    socket.send('hello');
};
socket.onmessage= function(s) {
    alert('got reply '+s);
};

You will need a specialised socket application on the server-side to take the connections and do something with them; it is not something you would normally be doing from a web server's scripting interface. However it is a relatively simple protocol; my noddy Python SocketServer-based endpoint was only a couple of pages of code.

In any case, it doesn't really exist, yet. Neither the JavaScript-side spec nor the network transport spec are nailed down, and no browsers support it.

You can, however, use Flash where available to provide your script with a fallback until WebSocket is widely available. Gimite's web-socket-js is one free example of such. However you are subject to the same limitations as Flash Sockets then, namely that your server has to be able to spit out a cross-domain policy on request to the socket port, and you will often have difficulties with proxies/firewalls. (Flash sockets are made directly; for someone without direct public IP access who can only get out of the network through an HTTP proxy, they won't work.)

Unless you really need low-latency two-way communication, you are better off sticking with XMLHttpRequest for now.

Community
  • 1
  • 1
bobince
  • 528,062
  • 107
  • 651
  • 834
  • What kinds of WS bindings will html5 support? – Rella Nov 15 '09 at 03:33
  • If you mean what will WebSocket objects look like in the browser, it still remains to be seen, but the spec (http://www.w3.org/TR/websockets/) hasn't changed a lot recently. If by WS Bindings you're talking about Web Services, then none: WebSocket won't talk SOAP or any other pre-existing protocol. – bobince Nov 15 '09 at 12:29
  • 1
    You say "There is no facility to use general-purpose sockets in JS or HTML. It would be a security disaster, for one." but what about phonegap or phonegap related applications? HTML+js is not just needed for standart browsers. – obayhan Jul 08 '15 at 06:54
57

Specifications:

Articles:

Tutorial:

Libraries:

Unreality
  • 4,111
  • 11
  • 48
  • 67
o.k.w
  • 25,490
  • 6
  • 66
  • 63
  • 1
    Update: `bobince` provided a simple tutorial on this page :P – o.k.w Nov 15 '09 at 03:09
  • 3
    Yes, that user would presumably be Kaazing's own Jonas Jacobi. Often pops up anywhere WebSocket is mentioned! I don't quite get Kaazing TBH, sitting on websocket.org to promote what appears to be a monolithic not-actually-free client-and-server solution that has has WebSocket support as seemingly quite a small portion of what it actually does. I don't know, It might be a good solution, but I'm not wholly comfortable with the marketing really. [edited, hence after okw's reply] – bobince Nov 15 '09 at 03:26
47

I think it is important to mention, now that this question is over 1 year old, that Socket.IO has since come out and seems to be the primary way to work with sockets in the browser now; it is also compatible with Node.js as far as I know.

Ricket
  • 33,368
  • 30
  • 112
  • 143