views:

1211

answers:

2

How to Use Sockets in JavaScript\HTML?

May be using some cool HTML5 technics?

Libs? Tutorials? Blog Articles?

+6  A: 

Specifications:

Articles:

Tutorial:
None found so far

Libraries:

o.k.w
Update: `bobince` provided a simple tutorial on this page :P
o.k.w
@Yea, same sentiment here.
o.k.w
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
+5  A: 

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.

bobince
What kinds of WS bindings will html5 support?
Blender
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