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
180
Concurrency vs. Parallelism 2.0 - RubyDay 2013
razielgn
3
170
Concurrency vs. Parallelism - Codelovers 2013
razielgn
3
180
Guida intergalattica per contributori Open Source
razielgn
0
49
Other Decks in Programming
See All in Programming
GraphRAGの仕組みまるわかり
tosuri13
8
480
Cursor AI Agentと伴走する アプリケーションの高速リプレイス
daisuketakeda
1
130
LINEヤフー データグループ紹介
lycorp_recruit_jp
0
890
明示と暗黙 ー PHPとGoの インターフェイスの違いを知る
shimabox
2
320
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
1
190
Elixir で IoT 開発、 Nerves なら簡単にできる!?
pojiro
1
150
PostgreSQLのRow Level SecurityをPHPのORMで扱う Eloquent vs Doctrine #phpcon #track2
77web
2
350
今ならAmazon ECSのサービス間通信をどう選ぶか / Selection of ECS Interservice Communication 2025
tkikuc
20
3.6k
#kanrk08 / 公開版 PicoRubyとマイコンでの自作トレーニング計測装置を用いたワークアウトの理想と現実
bash0c7
1
460
ReadMoreTextView
fornewid
1
480
Haskell でアルゴリズムを抽象化する / 関数型言語で競技プログラミング
naoya
17
4.9k
Select API from Kotlin Coroutine
jmatsu
1
190
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
17
940
Side Projects
sachag
455
42k
A designer walks into a library…
pauljervisheath
207
24k
A Tale of Four Properties
chriscoyier
160
23k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.8k
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!