SlideShare a Scribd company logo
1 of 11
Download to read offline
Socket.IO
@ Speck&Tech - 25.05.2016
Who am I?
Davide Pedranz
Student in Computer Science @ UniTN
Software Developer @ FBK
@davidepedranz
Specker
Game
http://tinyurl.com/speckers-game
WebSockets
What?
● A persistent connection between the client and the server
● Both parties can start sending data at any time
Why?
● Low-latency communication
● Bi-directional communication
● Performances
Adapted from: http://chimera.labs.oreilly.com/books/1230000000545/ch17.html
Polling vs WebSockets
Important event on
the server side
polling
delay
Polling vs WebSockets
Polling WebSockets
Connections multiple single
Communication client-to-server bi-directional
Latency high low
Cost high, both for server and client low
Socket.IO
What?
● Realtime application framework
● Wrapper around Websockets (browser + Node.js)
● Send events between the client and the server
Why?
● Fallback for old browsers (IE8+)
● JavaScript, Android, iOS clients
● Trivial APIs
Setup
Server side: Client side:
user ~ $ npm install --save socket.io
// import socket.io
const socket = require('socket.io');
// attach socket.io an http server
var io = socket().listen(httpServer);
// listen for connections
io.on('connect', function (socket) {
// listen for events
socket.on(‘event’, function (data) {
// reply to events
io.emit(‘other-event’, data);
});
};
<!-- include socket.io -->
<script src="/socket.io/socket.io.js"></script>
// create socket.io instance
var socket = io();
// connected to the server
socket.on('event', function (msg) {
console.log(msg);
});
Server API
Server:
● io.on(‘connection’, callback(socket)) - new connected client
Socket:
● socket.on(event, callback(data)) - attach a new listener for the given event
● socket.emit(event, data) - send the event to this client
● socket.broadcast.emit(event, data) - send the event to all clients
Client API
Socket
● socket.on(event, callback(data)) - attach a new listener for the given event
● socket.emit(event, data) - send the event to the server
… yes, that’s it!
Demo
The Specker Game

More Related Content

What's hot

What's hot (20)

REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
Spring Security 5
Spring Security 5Spring Security 5
Spring Security 5
 
What is Ajax technology?
What is Ajax technology?What is Ajax technology?
What is Ajax technology?
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
Introduction to WebSockets Presentation
Introduction to WebSockets PresentationIntroduction to WebSockets Presentation
Introduction to WebSockets Presentation
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
.Net Core
.Net Core.Net Core
.Net Core
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
 
Local storage
Local storageLocal storage
Local storage
 
OK Google, How Do I Red Team GSuite?
OK Google, How Do I Red Team GSuite?OK Google, How Do I Red Team GSuite?
OK Google, How Do I Red Team GSuite?
 
Modern API Security with JSON Web Tokens
Modern API Security with JSON Web TokensModern API Security with JSON Web Tokens
Modern API Security with JSON Web Tokens
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutines
 
.Net Core
.Net Core.Net Core
.Net Core
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
Express js
Express jsExpress js
Express js
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 

Viewers also liked

Viewers also liked (9)

Horus - The Invisible Made Audible
Horus - The Invisible Made AudibleHorus - The Invisible Made Audible
Horus - The Invisible Made Audible
 
Speck & Tech: Attacking iOS (A brief overview)
Speck & Tech: Attacking iOS (A brief overview)Speck & Tech: Attacking iOS (A brief overview)
Speck & Tech: Attacking iOS (A brief overview)
 
Test Driven Development SpeedRun
Test Driven Development SpeedRunTest Driven Development SpeedRun
Test Driven Development SpeedRun
 
Virtual Reality programming and designing issues
Virtual Reality programming and designing issuesVirtual Reality programming and designing issues
Virtual Reality programming and designing issues
 
Speck&Tech 3 - The Right to be Forgotten
Speck&Tech 3 - The Right to be ForgottenSpeck&Tech 3 - The Right to be Forgotten
Speck&Tech 3 - The Right to be Forgotten
 
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
 
Google Next Cloud Trento
Google Next Cloud TrentoGoogle Next Cloud Trento
Google Next Cloud Trento
 
No borders innovation: how to build a remote working-based “liquid company”
No borders innovation: how to build a remote working-based “liquid company”No borders innovation: how to build a remote working-based “liquid company”
No borders innovation: how to build a remote working-based “liquid company”
 
reveal.js 3.0.0
reveal.js 3.0.0reveal.js 3.0.0
reveal.js 3.0.0
 

Similar to Socket.IO

Similar to Socket.IO (20)

WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
 
The Real World - Plugging the Enterprise Into It (nodejs)
The Real World - Plugging  the Enterprise Into It (nodejs)The Real World - Plugging  the Enterprise Into It (nodejs)
The Real World - Plugging the Enterprise Into It (nodejs)
 
Best practices of building data streaming API
Best practices of building data streaming APIBest practices of building data streaming API
Best practices of building data streaming API
 
Real time web_apps_pycon2012-v1
Real time web_apps_pycon2012-v1Real time web_apps_pycon2012-v1
Real time web_apps_pycon2012-v1
 
Signal R 2015
Signal R 2015Signal R 2015
Signal R 2015
 
From Data Push to WebSockets
From Data Push to WebSocketsFrom Data Push to WebSockets
From Data Push to WebSockets
 
WebRTC - Brings Real-Time to the Web
WebRTC - Brings Real-Time to the WebWebRTC - Brings Real-Time to the Web
WebRTC - Brings Real-Time to the Web
 
WebRTC: Bring real-time to the web - Barcamp Saigon 2012
WebRTC: Bring real-time to the web - Barcamp Saigon 2012WebRTC: Bring real-time to the web - Barcamp Saigon 2012
WebRTC: Bring real-time to the web - Barcamp Saigon 2012
 
The Considerations for Internet of Things @ 2017
The Considerations for Internet of Things @ 2017The Considerations for Internet of Things @ 2017
The Considerations for Internet of Things @ 2017
 
Zero Downtime JEE Architectures
Zero Downtime JEE ArchitecturesZero Downtime JEE Architectures
Zero Downtime JEE Architectures
 
Internet of Things and Edge Compute at Chick-fil-A
Internet of Things and Edge Compute at Chick-fil-AInternet of Things and Edge Compute at Chick-fil-A
Internet of Things and Edge Compute at Chick-fil-A
 
Programming for the Internet of Things
Programming for the Internet of ThingsProgramming for the Internet of Things
Programming for the Internet of Things
 
Comet: Making The Web a 2-Way Medium
Comet: Making The Web a 2-Way MediumComet: Making The Web a 2-Way Medium
Comet: Making The Web a 2-Way Medium
 
The Web of Things
The Web of ThingsThe Web of Things
The Web of Things
 
Realtime Messaging und verteilte Systeme mit SharePoint und Windows Azure Ser...
Realtime Messaging und verteilte Systeme mit SharePoint und Windows Azure Ser...Realtime Messaging und verteilte Systeme mit SharePoint und Windows Azure Ser...
Realtime Messaging und verteilte Systeme mit SharePoint und Windows Azure Ser...
 
Cloud Experience: Data-driven Applications Made Simple and Fast
Cloud Experience: Data-driven Applications Made Simple and FastCloud Experience: Data-driven Applications Made Simple and Fast
Cloud Experience: Data-driven Applications Made Simple and Fast
 
«Real Time» Web Applications with SignalR in ASP.NET
«Real Time» Web Applications with SignalR in ASP.NET«Real Time» Web Applications with SignalR in ASP.NET
«Real Time» Web Applications with SignalR in ASP.NET
 
Building the Internet of Things with Thingsquare and Contiki - day 1, part 3
Building the Internet of Things with Thingsquare and Contiki - day 1, part 3Building the Internet of Things with Thingsquare and Contiki - day 1, part 3
Building the Internet of Things with Thingsquare and Contiki - day 1, part 3
 
Node js - Yns
Node js - YnsNode js - Yns
Node js - Yns
 
Web performance Part 1 "Networking theoretical basis"
Web performance Part 1 "Networking theoretical basis"Web performance Part 1 "Networking theoretical basis"
Web performance Part 1 "Networking theoretical basis"
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
"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 ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Socket.IO

  • 2. Who am I? Davide Pedranz Student in Computer Science @ UniTN Software Developer @ FBK @davidepedranz
  • 4. WebSockets What? ● A persistent connection between the client and the server ● Both parties can start sending data at any time Why? ● Low-latency communication ● Bi-directional communication ● Performances
  • 5. Adapted from: http://chimera.labs.oreilly.com/books/1230000000545/ch17.html Polling vs WebSockets Important event on the server side polling delay
  • 6. Polling vs WebSockets Polling WebSockets Connections multiple single Communication client-to-server bi-directional Latency high low Cost high, both for server and client low
  • 7. Socket.IO What? ● Realtime application framework ● Wrapper around Websockets (browser + Node.js) ● Send events between the client and the server Why? ● Fallback for old browsers (IE8+) ● JavaScript, Android, iOS clients ● Trivial APIs
  • 8. Setup Server side: Client side: user ~ $ npm install --save socket.io // import socket.io const socket = require('socket.io'); // attach socket.io an http server var io = socket().listen(httpServer); // listen for connections io.on('connect', function (socket) { // listen for events socket.on(‘event’, function (data) { // reply to events io.emit(‘other-event’, data); }); }; <!-- include socket.io --> <script src="/socket.io/socket.io.js"></script> // create socket.io instance var socket = io(); // connected to the server socket.on('event', function (msg) { console.log(msg); });
  • 9. Server API Server: ● io.on(‘connection’, callback(socket)) - new connected client Socket: ● socket.on(event, callback(data)) - attach a new listener for the given event ● socket.emit(event, data) - send the event to this client ● socket.broadcast.emit(event, data) - send the event to all clients
  • 10. Client API Socket ● socket.on(event, callback(data)) - attach a new listener for the given event ● socket.emit(event, data) - send the event to the server … yes, that’s it!