Index

Table of contents

websockets

open websocket
ws = new WebSocket(url);
sending a message
ws.send('hello world!');

events

on message
ws.onmessage = function(e) {
	console.log('received message: ' + e.data);
}
on open
ws.onopen = function() {
	console.log('socket open');
}
on close
ws.onclose = function() {
	console.log('closing socket');
}
checking whether or not a socket exists and is open
if(typeof ws !== 'undefined' && ws.readyState === WebSocket.OPEN) {
	...
}
documentation
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket

ping - pong

browsers don't currently have an implementation of a ping method, you can at best define a custom message
ws.send('ping');
browsers will automatically respond to a ping with a pong
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#pings_and_pongs_the_heartbeat_of_websockets

links

websocket documentation
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
websockets echo service
http://eriklievaart.com/web/websockets.html
https://www.websocket.org/echo.html