What's websocket?
- A client/server connexion
- Work both ways
- Stays open
- Event based messages
How it's done
Starts with an HTTP request:
GET /socket.io/1/websocket/6mDqTWQ0hWpKY5V_GYWo HTTP/1.1
Host:websocket.symfony2.local:8000
Upgrade:websocket
Connection:Upgrade
Sec-WebSocket-Key:ijIdeSs13H1t6Ez3rZtdaA==
Sec-WebSocket-Version:13
Origin:http://websocket.symfony2.local
Cookie:PHPSESSID=pnsdaav81i2v1lpopenbd9aq93
How it's done
And the server's response:
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Sec-WebSocket-Accept: 6GRVpnnWRvk90r/nLFsxfrP3+D0=
Upgrade: websocket
Sending messages:
socket.onmessage = function (event) {
console.log(event.data);
}
socket.send("Much string. Very message. Wow.");
Socket.io Event system
socket.emit(
"user:say",
{user: 1, message: "Hi."}
);
socket.on(
"user:say",
function (data) { console.log(data.message); }
);