Download presentation
Presentation is loading. Please wait.
Published byMarlene Chase Modified over 9 years ago
1
zz SOCKET.IO going real time with Arnout Kazemier / @3rdEden
2
what is socket.io
3
persistent connection between server & client
4
real time
5
cross browser even IE 5.5 should work <3
6
magic
7
open source learnboost/socket.io learnboost/socket.io-client - node.js server - javascript client available on github.com
8
getting started
9
npm install socket.io installing the server installation
10
npm install socket.io-client installing the client installation
11
var http = require(‘http’); var s = http.createServer(function(q,r){ r.writeHead(200); r.end(‘sup’); }); s.listen(80); var io = require(‘socket.io).listen(s); listening on the server
12
var io = require(‘socket.io).listen(80); listening on the server
13
io.sockets.on(‘connection’, function (socket) { socket.on(‘custom event’, function (data) { socket.emit(‘custom event received’); }); socket.on(‘disconnect’, function (){ console.log(‘client disconnected’); }); listening on the server
14
var socket = io.connect(); socket.on(‘connect’, function () { socket.emit(‘custom event’, ‘pewpew’); }); listening on the client
15
var io = require(‘socket.io’).listen(80); // express styled configuration io.configure(‘development’, function(){ io.set(‘log level’, 3); }); io.configure(‘production’, function(){ io.set(‘log level’, 1); }); configuring
16
messaging
17
socket.emit(‘event’, ‘message’); socket.emit(`event`, { json:‘here’ }); sending events client & server socket.on(‘event’, function (data) { // data is plain text, or our json });
18
socket.send(‘plain text message’); socket.json.send({ json:‘here’ }); sending messages client & server // send method triggers the message // event socket.on(‘message’, function (data) { // data is plain text, or our json });
19
socket.send(‘plain text message’); socket.json.send({ json:‘here’ }); message flag socket.on(‘message’, function (data) { // data is plain text, or our json });
20
socket.broadcast.emit(‘event’); message flag // broadcasts a message to all sockets
21
socket.volatile.emit(‘event’); message flag // no need to internally buffer this msg
22
socket.broadcast.to(‘room’).emit(‘event’); message flag // broadcast, but only to people in this room
23
io.sockets.emit(‘event’); io.sockets.send(‘hello nodejsconf.it’); message flag // broadcast to everyone
24
socket.send(‘hello nodejsconf.it’, function(arg) { console.log(‘message reached the server’); console.log(‘arg:’, arg); // ‘hello’ }); socket.on(‘message’, function (msg, fn) { console.log(‘msg:’, msg); fn(‘hello’); // confirm the acknowledgement }); acknowledgements // on the client // on the server
25
what happens when you connect
26
socket.io client socket.io server
27
socket.io client socket.io serverhandshake request
28
socket.io client socket.io server handshake is accepted accepted transports, connection id and config is returned
29
socket.io client socket.io server feature detection is used to find a working transport layer
30
available transports Web Socket
31
available transports Web Socket Flash Socket
32
available transports Web Socket Flash Socket HTML File
33
available transports Web Socket Flash Socket HTML File XHR Polling
34
available transports Web Socket Flash Socket HTML File XHR Polling JSONP Polling
35
socket.io client socket.io server real time connection is established with the server
36
socket.io client socket.io server real time connection is established with the server heartbeats are send to ensure proper connection
37
DEMO
38
upcoming release
39
gzip support dynamic socket.io.js generation rewrite of static backend upcoming release
40
io.configure(function(){ io.enable(‘browser client minification’); io.set(‘transports’, [ ‘websocket’, ‘xhr-polling’ ]); }); upcoming release
41
/socket.io/socket.io+xhr-polling.js upcoming release /socket.io/socket.io+websocket+htmlfile.js
42
scaling socket.io across multiple processes and servers by introducing 1/2 lines of code release of the RedisStore upcoming release
43
DEMO
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.