var socket = new WebSocket('ws://html5rocks.websocket.org/echo');
socket
.onopen = function(event) {
socket
.send('Hello, WebSocket');
};
socket
.onmessage = function(event) { alert(event.data); }
socket
.onclose = function(event) { alert('closed'); }

Full-duplex, bi-directional communication over the Web: Both the server and client can send data at any time, or even at the same time. Only the data itself is sent, without the overhead of HTTP headers, dramatically reducing bandwidth.

Use the echo demo below to test a WebSocket connection from your browser. Both the message you send and the response you receive travel over the same WebSocket connection.

Location:



Message:

Output: