Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
NodeJS + SocketIO
Search
Federico Ravasio
October 15, 2012
Programming
0
140
NodeJS + SocketIO
Lightning talk @ Codelovers.
Federico Ravasio
October 15, 2012
Tweet
Share
More Decks by Federico Ravasio
See All by Federico Ravasio
The art of Mocking
razielgn
1
170
Concurrency vs. Parallelism 2.0 - RubyDay 2013
razielgn
3
160
Concurrency vs. Parallelism - Codelovers 2013
razielgn
3
180
Guida intergalattica per contributori Open Source
razielgn
0
45
Other Decks in Programming
See All in Programming
TypeScript を活かしてデザインシステム MCP を作る / #tskaigi_after_night
izumin5210
4
480
JVM の仕組みを理解して PHP で実装してみよう
m3m0r7
PRO
1
250
TypeScript だけを書いて Tauri でデスクトップアプリを作ろう / Tauri with only TypeScript
tris5572
2
540
Passkeys for Java Developers
ynojima
1
210
DevTalks 25 - Create your own AI-infused Java apps with ease
kdubois
2
120
TSConfigからTypeScriptの世界を覗く
planck16
2
1.3k
RubyKaigi Hack Space in Tokyo & 函館最速 "予習" 会 / RubyKaigi Hack Space in Tokyo & The Fastest Briefing of RubyKaigi 2026 in Hakodate
moznion
1
130
List Unfolding - 'unfold' as the Computational Dual of 'fold', and how 'unfold' relates to 'iterate'"
philipschwarz
PRO
0
140
ts-morph実践:型を利用するcodemodのテクニック
ypresto
1
540
#QiitaBash TDDでAIに設計イメージを伝える
ryosukedtomita
2
1.6k
UPDATEがシステムを複雑にする? イミュータブルデータモデルのすすめ
shimomura
0
220
Cursor Meetup Tokyo ゲノミクスとCursor: 進化と制約のあいだ
koido
1
320
Featured
See All Featured
How GitHub (no longer) Works
holman
314
140k
GitHub's CSS Performance
jonrohan
1031
460k
Why Our Code Smells
bkeepers
PRO
336
57k
Making Projects Easy
brettharned
116
6.2k
Testing 201, or: Great Expectations
jmmastey
42
7.5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Embracing the Ebb and Flow
colly
85
4.7k
Gamification - CAS2011
davidbonilla
81
5.3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.5k
Raft: Consensus for Rubyists
vanstee
137
7k
The World Runs on Bad Software
bkeepers
PRO
68
11k
Transcript
+
Orientato agli eventi I/O asincrono Reactor pattern Google V8 Singolo
thread
Websocket con fallback Semplice messaggistica
None
“In attesa di uno straniero...” “Stai parlando con uno straniero,
salutalo!” Straniero: asl? Tu: 98 m antarctica Straniero: o_o” [Qualcuno lascia la chat]
Server
var waiting = []; // Lista client in attesa var
couples = {}; // { // clientID: stranger, // strangerID: client // ... // }
var io = SocketIO.listen(server); // server è un server HTTP
già inizializzato io.sockets.on('connection', function(client) { // ... });
io.sockets.on('connection', function(client) { if (waiting.length == 0) { waiting.push(client); client.emit('waiting');
} else { var stranger = waiting.pop(); couples[client.id] = stranger; couples[stranger.id] = client; client.emit('partner'); stranger.emit('partner'); } // ... });
io.sockets.on('connection', function(client) { // ... client.on('message', function(message) { couples[client.id].emit('message', message);
}); // ... });
io.sockets.on('connection', function(client) { // ... client.on('disconnect', function() { if (stranger
= couples[client.id]) { stranger.emit('left'); delete couples[stranger.id]; delete couples[client.id]; } }); });
Client
var socket = io.connect(); socket.on('waiting', function() { logMessage('Waiting for a
stranger...'); }); socket.on('partner', function() { logMessage('You are now chatting with...'); }); socket.on('message', function(message) { logMessage(message); }); socket.on('left', function() { this.logMessage('Stranger has left...'); socket.disconnect(); });
var leaveConversation = function() { logMessage('You have left...'); socket.disconnect(); };
var sendMessage = function(message) { socket.emit('message', message); logMessage(message); };
Demo!