SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
Going realtime with
    Socket.IO
  Christian Joudrey - @cjoudrey
The goal

• Create a web application to chat in realtime
The challenges

• We want persistent connection between client and server
The challenges

• We want persistent connection between client and server

  o   Easy! We've got HTML5 Websockets.
The challenges

• We want persistent connection between client and server

  o   Easy! We've got HTML5 Websockets.

• We want to detect client disconnection and timeouts
The challenges

• We want persistent connection between client and server

  o   Easy! We've got HTML5 Websockets.

• We want to detect client disconnection and timeouts

  o   Easy! We can detect when a websocket closes.
The challenges

• We want persistent connection between client and server

  o   Easy! We've got HTML5 Websockets.

• We want to detect client disconnection and timeouts

  o   Easy! We can detect when a websocket closes.

• We want cross-browser support
Oh :(
That's okay...

• We can use different transports:

  o   HTML5 WebSocket

  o   Flash Socket

  o   AJAX Long Polling

  o   Forever Iframe
That's a lot of ugly code. :(
“Socket.IO aims to make realtime apps
possible in every browser and mobile device,
blurring the differences between the different
transport mechanisms. It's care-free realtime
100% in JavaScript.”
Getting started

• Socket.IO: http://socket.io

• Install Socket.IO using npm:
  npm install socket.io

• Documentation: http://bit.ly/SocketIODocs
Your first server
# Using Express as http server
var app = require('express').createServer();
var io = require('socket.io').listen(app);

io.sockets.on('connection', function (socket) {
  console.log('Someone connected!');
});

app.listen(8080);
                     - or -
# You can also use node's http module
var app = require('http').createServer(callback);
var io = require('socket.io').listen(app);

app.listen(8080);
Client-side JS

<script src='/socket.io/socket.io.js'></script>
<script>
  var socket = io.connect();
  socket.on('connect', function () {
    console.log('We are connected!');
  });
</script>
Sending a message to server

<script src='/socket.io/socket.io.js'></script>
<script>
  var socket = io.connect();
  socket.on('connect', function () {
    console.log('We are connected!');
    var name = prompt('What is your name?');
    this.emit('set nickname', name);
  });
</script>
Receiving a message from client
var app = require('express').createServer();
var io = require('socket.io').listen(app);

io.sockets.on('connection', function (socket) {
  console.log('Someone connected!');
  socket.on('set nickname', function (nickname) {
    socket.nickname = nickname;
    console.log(nickname + ' just connected!');
  });
});

app.listen(8080);
Message acknowledgement (client)
<script src='/socket.io/socket.io.js'></script>
<script>
  var socket = io.connect();
  socket.on('connect', function () {
    console.log('We are connected!');

   var name = prompt('What is your name?');

   this.emit('set nickname', name, function (success) {
     console.log('The server got the message!');

     if (!success) {
       console.log('Nickname in use!');
     }
   });
 });
</script>
Message acknowledgement (server)
var app = require('express').createServer();
var io = require('socket.io').listen(app);

var users = [];

io.sockets.on('connection', function (socket) {
  socket.on('set nickname', function (nick, cb) {
    if (users.indexOf(nick) != -1) {
      return cb(false);
    }

    socket.nickname = nick;
    users.push(nick);
    cb(true);
  });
});

app.listen(8080);
Detecting disconnects
var app = require('express').createServer();
var io = require('socket.io').listen(app);

var users = [];

io.sockets.on('connection', function (socket) {
  // ...
  socket.on('disconnect', function () {
    var nickname = socket.nickname;

    if (nickname) {
      socket.broadcast.emit('user part', nickname);
      users.splice(users.indexOf(nickname), 1);
    }
  });
});

app.listen(8080);
Interesting links

• Source code of a working chat: http://bit.ly/nodechat1

• Socket.IO Docs: http://bit.ly/SocketIODocs
Demo! :)
Questions? :)

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Node js introduction
Node js introductionNode js introduction
Node js introduction
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Push Notification for Android, iOS & Sever Side Using Firebase Cloud Messaging
Push Notification for Android, iOS & Sever Side Using Firebase Cloud MessagingPush Notification for Android, iOS & Sever Side Using Firebase Cloud Messaging
Push Notification for Android, iOS & Sever Side Using Firebase Cloud Messaging
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
 
Express js
Express jsExpress js
Express js
 
Webchat using flask socket io
Webchat using flask socket ioWebchat using flask socket io
Webchat using flask socket io
 
Swagger UI
Swagger UISwagger UI
Swagger UI
 
Introduction to SignalR
Introduction to SignalRIntroduction to SignalR
Introduction to SignalR
 
Basic Concept of Node.js & NPM
Basic Concept of Node.js & NPMBasic Concept of Node.js & NPM
Basic Concept of Node.js & NPM
 
Intro to signalR
Intro to signalRIntro to signalR
Intro to signalR
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
 
Socket.io (part 1)
Socket.io (part 1)Socket.io (part 1)
Socket.io (part 1)
 
Build RESTful API Using Express JS
Build RESTful API Using Express JSBuild RESTful API Using Express JS
Build RESTful API Using Express JS
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
HTTP request and response
HTTP request and responseHTTP request and response
HTTP request and response
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
Express JS
Express JSExpress JS
Express JS
 
SignalR for ASP.NET Developers
SignalR for ASP.NET DevelopersSignalR for ASP.NET Developers
SignalR for ASP.NET Developers
 
Ionic Framework
Ionic FrameworkIonic Framework
Ionic Framework
 

Andere mochten auch

Building notification system in NodeJS + Redis
Building notification system in NodeJS + RedisBuilding notification system in NodeJS + Redis
Building notification system in NodeJS + RedisLe Duc
 
Real-time Web Application with Socket.IO, Node.js, and Redis
Real-time Web Application with Socket.IO, Node.js, and RedisReal-time Web Application with Socket.IO, Node.js, and Redis
Real-time Web Application with Socket.IO, Node.js, and RedisYork Tsai
 
Going real time with Socket.io
Going real time with Socket.ioGoing real time with Socket.io
Going real time with Socket.ioArnout Kazemier
 
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014Stéphane ESCANDELL
 
Websocket + Redis pubsub
Websocket + Redis pubsubWebsocket + Redis pubsub
Websocket + Redis pubsubKai Hsu
 
Real Time Recommendations Using WebSockets and Redis - Ninad Divadkar, Inuit
Real Time Recommendations Using WebSockets and Redis - Ninad Divadkar, InuitReal Time Recommendations Using WebSockets and Redis - Ninad Divadkar, Inuit
Real Time Recommendations Using WebSockets and Redis - Ninad Divadkar, InuitRedis Labs
 
Node js oc meetup 2 socket io intro
Node js oc meetup 2 socket io introNode js oc meetup 2 socket io intro
Node js oc meetup 2 socket io introeddify
 
Socket io - JSZurich
Socket io - JSZurichSocket io - JSZurich
Socket io - JSZurichstreunerlein
 
Node worshop Realtime - Socket.io
Node worshop Realtime - Socket.ioNode worshop Realtime - Socket.io
Node worshop Realtime - Socket.ioCaesar Chi
 
Better d3 charts with tdd
Better d3 charts with tddBetter d3 charts with tdd
Better d3 charts with tddMarcos Iglesias
 
Realtime web applications with ExpressJS and SocketIO
Realtime web applications with ExpressJS and SocketIORealtime web applications with ExpressJS and SocketIO
Realtime web applications with ExpressJS and SocketIOHüseyin BABAL
 
Data visualization
Data visualizationData visualization
Data visualizationsagalabo
 
Building your First MEAN App
Building your First MEAN AppBuilding your First MEAN App
Building your First MEAN AppMongoDB
 
Web in real time - technical project - socket.io
Web in real time - technical project - socket.ioWeb in real time - technical project - socket.io
Web in real time - technical project - socket.ioThomas Ferney
 

Andere mochten auch (20)

Building notification system in NodeJS + Redis
Building notification system in NodeJS + RedisBuilding notification system in NodeJS + Redis
Building notification system in NodeJS + Redis
 
Real-time Web Application with Socket.IO, Node.js, and Redis
Real-time Web Application with Socket.IO, Node.js, and RedisReal-time Web Application with Socket.IO, Node.js, and Redis
Real-time Web Application with Socket.IO, Node.js, and Redis
 
Going real time with Socket.io
Going real time with Socket.ioGoing real time with Socket.io
Going real time with Socket.io
 
Socket.io
Socket.ioSocket.io
Socket.io
 
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014
 
Websocket + Redis pubsub
Websocket + Redis pubsubWebsocket + Redis pubsub
Websocket + Redis pubsub
 
Real Time Recommendations Using WebSockets and Redis - Ninad Divadkar, Inuit
Real Time Recommendations Using WebSockets and Redis - Ninad Divadkar, InuitReal Time Recommendations Using WebSockets and Redis - Ninad Divadkar, Inuit
Real Time Recommendations Using WebSockets and Redis - Ninad Divadkar, Inuit
 
Learn node.js by building projects
Learn node.js by building projectsLearn node.js by building projects
Learn node.js by building projects
 
Node js oc meetup 2 socket io intro
Node js oc meetup 2 socket io introNode js oc meetup 2 socket io intro
Node js oc meetup 2 socket io intro
 
Socket io - JSZurich
Socket io - JSZurichSocket io - JSZurich
Socket io - JSZurich
 
Node worshop Realtime - Socket.io
Node worshop Realtime - Socket.ioNode worshop Realtime - Socket.io
Node worshop Realtime - Socket.io
 
Better d3 charts with tdd
Better d3 charts with tddBetter d3 charts with tdd
Better d3 charts with tdd
 
Realtime web applications with ExpressJS and SocketIO
Realtime web applications with ExpressJS and SocketIORealtime web applications with ExpressJS and SocketIO
Realtime web applications with ExpressJS and SocketIO
 
Data visualization
Data visualizationData visualization
Data visualization
 
tea
teatea
tea
 
Transforming WebSockets
Transforming WebSocketsTransforming WebSockets
Transforming WebSockets
 
Building your First MEAN App
Building your First MEAN AppBuilding your First MEAN App
Building your First MEAN App
 
Web in real time - technical project - socket.io
Web in real time - technical project - socket.ioWeb in real time - technical project - socket.io
Web in real time - technical project - socket.io
 
Mobile Application
Mobile ApplicationMobile Application
Mobile Application
 
Web socket with php v2
Web socket with php v2Web socket with php v2
Web socket with php v2
 

Ähnlich wie Going realtime with Socket.IO

Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSocketsGonzalo Ayuso
 
GWT Web Socket and data serialization
GWT Web Socket and data serializationGWT Web Socket and data serialization
GWT Web Socket and data serializationGWTcon
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineRicardo Silva
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 
Building Secure User Interfaces With JWTs (JSON Web Tokens)
Building Secure User Interfaces With JWTs (JSON Web Tokens)Building Secure User Interfaces With JWTs (JSON Web Tokens)
Building Secure User Interfaces With JWTs (JSON Web Tokens)Stormpath
 
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersWebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersViktor Gamov
 
SockJS Intro
SockJS IntroSockJS Intro
SockJS IntroNgoc Dao
 
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Ran Mizrahi
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Ran Mizrahi
 
Java client socket-20070327
Java client socket-20070327Java client socket-20070327
Java client socket-20070327Tsu-Fen Han
 
Asynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaAsynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaJames Falkner
 
Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)Spike Brehm
 
Java EE 7 (Lyon JUG & Alpes JUG - March 2014)
Java EE 7 (Lyon JUG & Alpes JUG  - March 2014)Java EE 7 (Lyon JUG & Alpes JUG  - March 2014)
Java EE 7 (Lyon JUG & Alpes JUG - March 2014)David Delabassee
 

Ähnlich wie Going realtime with Socket.IO (20)

Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSockets
 
GWT Web Socket and data serialization
GWT Web Socket and data serializationGWT Web Socket and data serialization
GWT Web Socket and data serialization
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
Building Secure User Interfaces With JWTs (JSON Web Tokens)
Building Secure User Interfaces With JWTs (JSON Web Tokens)Building Secure User Interfaces With JWTs (JSON Web Tokens)
Building Secure User Interfaces With JWTs (JSON Web Tokens)
 
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersWebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
 
SockJS Intro
SockJS IntroSockJS Intro
SockJS Intro
 
WebSockets with Spring 4
WebSockets with Spring 4WebSockets with Spring 4
WebSockets with Spring 4
 
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
 
Socket.IO
Socket.IOSocket.IO
Socket.IO
 
Web-Socket
Web-SocketWeb-Socket
Web-Socket
 
Java client socket-20070327
Java client socket-20070327Java client socket-20070327
Java client socket-20070327
 
WebSockets in JEE 7
WebSockets in JEE 7WebSockets in JEE 7
WebSockets in JEE 7
 
A.java
A.javaA.java
A.java
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Asynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaAsynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and Java
 
Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)
 
Java EE 7 (Lyon JUG & Alpes JUG - March 2014)
Java EE 7 (Lyon JUG & Alpes JUG  - March 2014)Java EE 7 (Lyon JUG & Alpes JUG  - March 2014)
Java EE 7 (Lyon JUG & Alpes JUG - March 2014)
 

Kürzlich hochgeladen

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

Going realtime with Socket.IO

  • 1. Going realtime with Socket.IO Christian Joudrey - @cjoudrey
  • 2. The goal • Create a web application to chat in realtime
  • 3. The challenges • We want persistent connection between client and server
  • 4. The challenges • We want persistent connection between client and server o Easy! We've got HTML5 Websockets.
  • 5. The challenges • We want persistent connection between client and server o Easy! We've got HTML5 Websockets. • We want to detect client disconnection and timeouts
  • 6. The challenges • We want persistent connection between client and server o Easy! We've got HTML5 Websockets. • We want to detect client disconnection and timeouts o Easy! We can detect when a websocket closes.
  • 7. The challenges • We want persistent connection between client and server o Easy! We've got HTML5 Websockets. • We want to detect client disconnection and timeouts o Easy! We can detect when a websocket closes. • We want cross-browser support
  • 9. That's okay... • We can use different transports: o HTML5 WebSocket o Flash Socket o AJAX Long Polling o Forever Iframe
  • 10. That's a lot of ugly code. :(
  • 11. “Socket.IO aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. It's care-free realtime 100% in JavaScript.”
  • 12. Getting started • Socket.IO: http://socket.io • Install Socket.IO using npm: npm install socket.io • Documentation: http://bit.ly/SocketIODocs
  • 13. Your first server # Using Express as http server var app = require('express').createServer(); var io = require('socket.io').listen(app); io.sockets.on('connection', function (socket) { console.log('Someone connected!'); }); app.listen(8080); - or - # You can also use node's http module var app = require('http').createServer(callback); var io = require('socket.io').listen(app); app.listen(8080);
  • 14. Client-side JS <script src='/socket.io/socket.io.js'></script> <script> var socket = io.connect(); socket.on('connect', function () { console.log('We are connected!'); }); </script>
  • 15. Sending a message to server <script src='/socket.io/socket.io.js'></script> <script> var socket = io.connect(); socket.on('connect', function () { console.log('We are connected!'); var name = prompt('What is your name?'); this.emit('set nickname', name); }); </script>
  • 16. Receiving a message from client var app = require('express').createServer(); var io = require('socket.io').listen(app); io.sockets.on('connection', function (socket) { console.log('Someone connected!'); socket.on('set nickname', function (nickname) { socket.nickname = nickname; console.log(nickname + ' just connected!'); }); }); app.listen(8080);
  • 17. Message acknowledgement (client) <script src='/socket.io/socket.io.js'></script> <script> var socket = io.connect(); socket.on('connect', function () { console.log('We are connected!'); var name = prompt('What is your name?'); this.emit('set nickname', name, function (success) { console.log('The server got the message!'); if (!success) { console.log('Nickname in use!'); } }); }); </script>
  • 18. Message acknowledgement (server) var app = require('express').createServer(); var io = require('socket.io').listen(app); var users = []; io.sockets.on('connection', function (socket) { socket.on('set nickname', function (nick, cb) { if (users.indexOf(nick) != -1) { return cb(false); } socket.nickname = nick; users.push(nick); cb(true); }); }); app.listen(8080);
  • 19. Detecting disconnects var app = require('express').createServer(); var io = require('socket.io').listen(app); var users = []; io.sockets.on('connection', function (socket) { // ... socket.on('disconnect', function () { var nickname = socket.nickname; if (nickname) { socket.broadcast.emit('user part', nickname); users.splice(users.indexOf(nickname), 1); } }); }); app.listen(8080);
  • 20. Interesting links • Source code of a working chat: http://bit.ly/nodechat1 • Socket.IO Docs: http://bit.ly/SocketIODocs