SlideShare a Scribd company logo
1 of 30
Download to read offline
socket.io
an unconventional tutorial
PART 1
Hello!
I am Andrea Tarquini
I am a full stack developer (mainly
working with MEAN stack) and an IT
(security) geek and fan.
Follow me at:
@h4t0n
h4t0n
Andrea Tarquini
blog.h4t0n.com
socket.io an unconventional tutorial -- PART 1
We’ll see a socket.io simple introduction:
1. how it works (hacker style)
○ taking a look at what happens within the browser
2. get started with socket.io
○ some references to good starting points for
beginners (but not followed by this tutorial)
3. how it works (engineer style)
○ writing some application examples from the official
documentation
Before starting… What is Socket.io ?
■ Essentially a fast real time engine
■ It enables real-time bidirectional event-
based communication
■ It works on every platform, browser, device.
■ Mainly used for
○ Instant messaging and chat
○ Document collaboration
○ Binary streaming (image/video/audio… )
from version 1.0
1.
How it works (hacker style)
Let’s start taking a look at what
happens within the browser
So let’s open socket.io with the Chrome Dev Console
Let’s type /socket.io on the network filter
There are two socket.io realtime connections
One for slack
There are two socket.io realtime connections
One for tweets
There are some strange requests….
Both the socket.io connections
■ start with xhr-polling (long polling)
■ then switch to websocket transport (if supported)
[more info here]
Let’s see the web socket data for tweets..
We have some control data and we can also
identify the tweets seen in the home page
Let’s play with the chat to understand exchanged data...
I talked a bit in the chat…
… but let’s see what happened using the console
A lot of data → control, messages and events
What have we learned from the client (browser)?
Events
The client listen to events
notifications and also emits
events. In the chat example,
events are:
■ client → server
○ typing
○ stop typing
○ new message
■ server → client
○ typing
○ user left
○ new message
Messages
Each event (listened or
emitted) can have an
associated message that is
the most important content:
■ strings
■ objects / arrays
■ but also (images, audio,...)
○ Buffers
○ Blobs
○ ArrayBuffers
○ Files
If you followed me, you have understood
events and messages without reading any
line of code or documentation.
But if you want to play more with the browser I suggest to
take a look at the source code and to use the console with
localStorage.debug=”socket.io-client:*”
to see debug messages (NB: reload the page)
That’s all ?
Namespaces
Can be used to assign
different endpoints (paths) to
sockets (default is root / )
It is often used to separate
concerns within an
application. For example:
■ notification namespace
■ chat namespace
Room
Inside a namespace (also the
root) you can define channels
that a socket can join or
leave. For example
■ a channel for a restricted
chat or team
!!!NO!!! The Server has its roles, it can be used to group
sockets in namespaces and rooms
We don’t see here. We’ll see
both in PART 2 !!!!
2.
Get Started with socket.io
Let’s start with the chat application
at socket.io/get-started/chat/
The “Hello Word” for Socket.IO !!!!
The socket.io chat application is recommended for beginners to
Socket.IO or Node.js
Follow the tutorial at socket.io/get-started/chat could be very
useful for all and it is highly recommended.
You can get the full chat example code also on Github:
github.com/rauchg/chat-example
ATTENTION!!!!! In the following slides we won’t follow the
chat application, we’ll work with the documentation.
3.
How it works (engineer style)
Let’s follow the official socket.io
documentation at socket.io/docs/
Get the sample code from documentation...
You can write your own app with the code
available at socket.io/docs or you can get
and run the code from socket.io-sample
github repository, which contains a list of
samples ready to be executed.
express-sample1 contains the simplest example you can find
on socket.io/docs
… code from express-sample1: The Server
// express-sample1/server.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(8080, function () {
var port = server.address().port;
console.log('Example app listening on port', port);
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', {
hello: 'world'
});
socket.on('my other event', function (data) {
console.log(data);
});
});
Require dependencies and use socket.io with the
same express Server
Both express and socket.io listen connection on
port 8080 (default host is 0.0.0.0)
Express is used to easily serve the index page
When a client connects, we have a socket instance
that can be used to listen/emit event messages:
● the server emit a message (hello world
object) on the “news” event (only to the just
connected socket)
● the server listen to messages coming from
the “my other event” event
… code from express-sample1: The index (html page)
<!-- index.html -->
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
GET the library from the server (it is
automatically served by socket.io)
Connect to socket.io and get a socket to use for
the event based communication
When the client connects, we have a socket
instance that can be used to listen/emit event
messages:
● the client listen to messages coming from the
“news” event
● once one is received the client emit a
message (my data object) to “my other
event” event
… code from express-sample1: run the sample
DOWNLOAD THE SAMPLES AND INSTALL NODE DEPENDENCIES
$ git clone git@github.com:h4t0n/socket.io-sample.git
$ cd socket.io-sample
# install package.json dependencies
# including socket.io,express,...
$ npm install
RUN EXPRESS-SAMPLE1
$ node express-sample1/server.js
● now open your browser at http://127.0.0.1:8080
● take a look at browser and server console
… code from express-sample1: another type of client
// client.js
var io = require('socket.io-client');
var socket = io.connect('http://localhost:
8080');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
ATTENTION!!!!! To interact with a socket.io server we don’t need a
browser at all. We can use a standalone Node.js script client.
We have to use the socket.io-client module (already installed in the
previous slide command $ npm install )
TODO!! Run the client more times from several
terminal and take a look at the server console.
From the example we have seen how to:
■ create a socket.io server
■ connect to the server by using a browser or a
standalone node app (script)
■ listen and emit event messages
○ from a single client to the server and vice versa
SO… VERY SIMPLE AND BASIC CONCEPTS...
.. .BUT THERE IS MUCH MORE !!!!!!!
Socket.io allows also to:
■ emit messages to all the connected clients
■ broadcast messages
○ sending message to everyone else except the socket that
start the communication (that emit an event)
■ send volatile messages
■ send and get acknowledgments
■ listen to other default events
○ such as the disconnect one
■ be used just as a cross browser websocket
○ without events or other socket.io additions
The socket.io/docs overview treats these concepts...
express-sample2 is a simple web app that
easily shows other most used features:
■ broadcast messages
■ emit messages to all connected clients
■ listen to disconnect event
■ emit messages without using socket.io
events (just as a cross browser websocket)
…or you can take a look at express-sample2 from socket.
io-sample repository
!!!! SO PLAY WITH EXPRESS-SAMPLE2 !!!
■ namespaces
■ rooms
■ middlewares
■ etcetera…
WE’LL SEE MORE FEATURES AND EXAMPLES IN THE
TUTORIAL PART 2
Thanks!
Any questions?
You can find me at:
@h4t0n
h4t0n
Andrea Tarquini
blog.h4t0n.com

More Related Content

What's hot

XSS - Do you know EVERYTHING?
XSS - Do you know EVERYTHING?XSS - Do you know EVERYTHING?
XSS - Do you know EVERYTHING?Yurii Bilyk
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing SwaggerTony Tam
 
Rapid Prototyping & Axure RP
Rapid Prototyping & Axure RPRapid Prototyping & Axure RP
Rapid Prototyping & Axure RPISsoft
 
How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?
How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?
How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?Katy Slemon
 
Designing APIs with OpenAPI Spec
Designing APIs with OpenAPI SpecDesigning APIs with OpenAPI Spec
Designing APIs with OpenAPI SpecAdam Paxton
 
Web Security Horror Stories
Web Security Horror StoriesWeb Security Horror Stories
Web Security Horror StoriesSimon Willison
 
Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questionsSujata Regoti
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJSSandi Barr
 
NoSql Injection
NoSql InjectionNoSql Injection
NoSql InjectionNSConclave
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js ExpressEyal Vardi
 
Regular Expression Injection
Regular Expression InjectionRegular Expression Injection
Regular Expression InjectionNSConclave
 
HTTP HOST header attacks
HTTP HOST header attacksHTTP HOST header attacks
HTTP HOST header attacksDefconRussia
 

What's hot (20)

XSS - Do you know EVERYTHING?
XSS - Do you know EVERYTHING?XSS - Do you know EVERYTHING?
XSS - Do you know EVERYTHING?
 
Rest in flask
Rest in flaskRest in flask
Rest in flask
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing Swagger
 
Rapid Prototyping & Axure RP
Rapid Prototyping & Axure RPRapid Prototyping & Axure RP
Rapid Prototyping & Axure RP
 
How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?
How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?
How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?
 
Xss ppt
Xss pptXss ppt
Xss ppt
 
ASP.NET Web Security
ASP.NET Web SecurityASP.NET Web Security
ASP.NET Web Security
 
Designing APIs with OpenAPI Spec
Designing APIs with OpenAPI SpecDesigning APIs with OpenAPI Spec
Designing APIs with OpenAPI Spec
 
Json web token
Json web tokenJson web token
Json web token
 
Web Security Horror Stories
Web Security Horror StoriesWeb Security Horror Stories
Web Security Horror Stories
 
JSON Web Tokens
JSON Web TokensJSON Web Tokens
JSON Web Tokens
 
Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questions
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
 
NoSql Injection
NoSql InjectionNoSql Injection
NoSql Injection
 
Express JS
Express JSExpress JS
Express JS
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 
Regular Expression Injection
Regular Expression InjectionRegular Expression Injection
Regular Expression Injection
 
API
APIAPI
API
 
HTTP HOST header attacks
HTTP HOST header attacksHTTP HOST header attacks
HTTP HOST header attacks
 
Building Advanced XSS Vectors
Building Advanced XSS VectorsBuilding Advanced XSS Vectors
Building Advanced XSS Vectors
 

Viewers also liked

Going real time with Socket.io
Going real time with Socket.ioGoing real time with Socket.io
Going real time with Socket.ioArnout Kazemier
 
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
 
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
 
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
 
Better d3 charts with tdd
Better d3 charts with tddBetter d3 charts with tdd
Better d3 charts with tddMarcos Iglesias
 
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
 
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
 
Socket.IO - Alternative Ways for Real-time Application
Socket.IO - Alternative Ways for Real-time ApplicationSocket.IO - Alternative Ways for Real-time Application
Socket.IO - Alternative Ways for Real-time ApplicationVorakamol Choonhasakulchok
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONTomomi Imura
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsasync_io
 

Viewers also liked (20)

Socket.IO
Socket.IOSocket.IO
Socket.IO
 
Going real time with Socket.io
Going real time with Socket.ioGoing real time with Socket.io
Going real time with Socket.io
 
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
 
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
 
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
 
Better d3 charts with tdd
Better d3 charts with tddBetter d3 charts with tdd
Better d3 charts with tdd
 
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
 
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
 
Web socket with php v2
Web socket with php v2Web socket with php v2
Web socket with php v2
 
Socket.IO - Alternative Ways for Real-time Application
Socket.IO - Alternative Ways for Real-time ApplicationSocket.IO - Alternative Ways for Real-time Application
Socket.IO - Alternative Ways for Real-time Application
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.js
 
Socket.io
Socket.ioSocket.io
Socket.io
 

Similar to Socket.io (part 1)

Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server InternalsPraveen Gollakota
 
How to create multiprocess server on windows with ruby - rubykaigi2016 Ritta ...
How to create multiprocess server on windows with ruby - rubykaigi2016 Ritta ...How to create multiprocess server on windows with ruby - rubykaigi2016 Ritta ...
How to create multiprocess server on windows with ruby - rubykaigi2016 Ritta ...Ritta Narita
 
Exploring the Internet of Things Using Ruby
Exploring the Internet of Things Using RubyExploring the Internet of Things Using Ruby
Exploring the Internet of Things Using RubyMike Hagedorn
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backendDavid Padbury
 
TCP Sockets Tutor maXbox starter26
TCP Sockets Tutor maXbox starter26TCP Sockets Tutor maXbox starter26
TCP Sockets Tutor maXbox starter26Max Kleiner
 
Exploring Async PHP (SF Live Berlin 2019)
Exploring Async PHP (SF Live Berlin 2019)Exploring Async PHP (SF Live Berlin 2019)
Exploring Async PHP (SF Live Berlin 2019)dantleech
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkAarti Parikh
 
Ransomware for fun and non-profit
Ransomware for fun and non-profitRansomware for fun and non-profit
Ransomware for fun and non-profitYouness Zougar
 
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
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for RubistsSagiv Ofek
 
How to build a chat application with react js, nodejs, and socket.io
How to build a chat application with react js, nodejs, and socket.ioHow to build a chat application with react js, nodejs, and socket.io
How to build a chat application with react js, nodejs, and socket.ioKaty Slemon
 
Java Networking
Java NetworkingJava Networking
Java NetworkingSunil OS
 
maXbox starter30 Web of Things
maXbox starter30 Web of ThingsmaXbox starter30 Web of Things
maXbox starter30 Web of ThingsMax Kleiner
 

Similar to Socket.io (part 1) (20)

Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server Internals
 
Book
BookBook
Book
 
Python networking
Python networkingPython networking
Python networking
 
A.java
A.javaA.java
A.java
 
How to create multiprocess server on windows with ruby - rubykaigi2016 Ritta ...
How to create multiprocess server on windows with ruby - rubykaigi2016 Ritta ...How to create multiprocess server on windows with ruby - rubykaigi2016 Ritta ...
How to create multiprocess server on windows with ruby - rubykaigi2016 Ritta ...
 
Exploring the Internet of Things Using Ruby
Exploring the Internet of Things Using RubyExploring the Internet of Things Using Ruby
Exploring the Internet of Things Using Ruby
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
TCP Sockets Tutor maXbox starter26
TCP Sockets Tutor maXbox starter26TCP Sockets Tutor maXbox starter26
TCP Sockets Tutor maXbox starter26
 
Exploring Async PHP (SF Live Berlin 2019)
Exploring Async PHP (SF Live Berlin 2019)Exploring Async PHP (SF Live Berlin 2019)
Exploring Async PHP (SF Live Berlin 2019)
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
 
Java sockets
Java socketsJava sockets
Java sockets
 
Ransomware for fun and non-profit
Ransomware for fun and non-profitRansomware for fun and non-profit
Ransomware for fun and non-profit
 
Node.js essentials
 Node.js essentials Node.js essentials
Node.js essentials
 
Proposal
ProposalProposal
Proposal
 
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
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
 
How to build a chat application with react js, nodejs, and socket.io
How to build a chat application with react js, nodejs, and socket.ioHow to build a chat application with react js, nodejs, and socket.io
How to build a chat application with react js, nodejs, and socket.io
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Python, do you even async?
Python, do you even async?Python, do you even async?
Python, do you even async?
 
maXbox starter30 Web of Things
maXbox starter30 Web of ThingsmaXbox starter30 Web of Things
maXbox starter30 Web of Things
 

Recently uploaded

Networking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOGNetworking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOGAPNIC
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)Damian Radcliffe
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...APNIC
 
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Roomdivyansh0kumar0
 
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Delhi Call girls
 
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceDelhi Call girls
 
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Roomdivyansh0kumar0
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
AlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with FlowsAlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with FlowsThierry TROUIN ☁
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts servicevipmodelshub1
 
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130  Available With RoomVIP Kolkata Call Girl Kestopur 👉 8250192130  Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Roomdivyansh0kumar0
 
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts servicesonalikaur4
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024APNIC
 

Recently uploaded (20)

Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
Networking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOGNetworking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOG
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
 
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
 
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
 
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
 
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
 
AlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with FlowsAlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with Flows
 
Call Girls In South Ex 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In South Ex 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICECall Girls In South Ex 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In South Ex 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
 
Rohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130  Available With RoomVIP Kolkata Call Girl Kestopur 👉 8250192130  Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Room
 
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024
 

Socket.io (part 1)

  • 2. Hello! I am Andrea Tarquini I am a full stack developer (mainly working with MEAN stack) and an IT (security) geek and fan. Follow me at: @h4t0n h4t0n Andrea Tarquini blog.h4t0n.com
  • 3. socket.io an unconventional tutorial -- PART 1 We’ll see a socket.io simple introduction: 1. how it works (hacker style) ○ taking a look at what happens within the browser 2. get started with socket.io ○ some references to good starting points for beginners (but not followed by this tutorial) 3. how it works (engineer style) ○ writing some application examples from the official documentation
  • 4. Before starting… What is Socket.io ? ■ Essentially a fast real time engine ■ It enables real-time bidirectional event- based communication ■ It works on every platform, browser, device. ■ Mainly used for ○ Instant messaging and chat ○ Document collaboration ○ Binary streaming (image/video/audio… ) from version 1.0
  • 5. 1. How it works (hacker style) Let’s start taking a look at what happens within the browser
  • 6. So let’s open socket.io with the Chrome Dev Console
  • 7. Let’s type /socket.io on the network filter
  • 8. There are two socket.io realtime connections One for slack
  • 9. There are two socket.io realtime connections One for tweets
  • 10. There are some strange requests…. Both the socket.io connections ■ start with xhr-polling (long polling) ■ then switch to websocket transport (if supported) [more info here]
  • 11. Let’s see the web socket data for tweets.. We have some control data and we can also identify the tweets seen in the home page
  • 12. Let’s play with the chat to understand exchanged data...
  • 13. I talked a bit in the chat…
  • 14. … but let’s see what happened using the console A lot of data → control, messages and events
  • 15. What have we learned from the client (browser)? Events The client listen to events notifications and also emits events. In the chat example, events are: ■ client → server ○ typing ○ stop typing ○ new message ■ server → client ○ typing ○ user left ○ new message Messages Each event (listened or emitted) can have an associated message that is the most important content: ■ strings ■ objects / arrays ■ but also (images, audio,...) ○ Buffers ○ Blobs ○ ArrayBuffers ○ Files
  • 16. If you followed me, you have understood events and messages without reading any line of code or documentation. But if you want to play more with the browser I suggest to take a look at the source code and to use the console with localStorage.debug=”socket.io-client:*” to see debug messages (NB: reload the page)
  • 17. That’s all ? Namespaces Can be used to assign different endpoints (paths) to sockets (default is root / ) It is often used to separate concerns within an application. For example: ■ notification namespace ■ chat namespace Room Inside a namespace (also the root) you can define channels that a socket can join or leave. For example ■ a channel for a restricted chat or team !!!NO!!! The Server has its roles, it can be used to group sockets in namespaces and rooms We don’t see here. We’ll see both in PART 2 !!!!
  • 18. 2. Get Started with socket.io Let’s start with the chat application at socket.io/get-started/chat/
  • 19. The “Hello Word” for Socket.IO !!!! The socket.io chat application is recommended for beginners to Socket.IO or Node.js Follow the tutorial at socket.io/get-started/chat could be very useful for all and it is highly recommended. You can get the full chat example code also on Github: github.com/rauchg/chat-example ATTENTION!!!!! In the following slides we won’t follow the chat application, we’ll work with the documentation.
  • 20. 3. How it works (engineer style) Let’s follow the official socket.io documentation at socket.io/docs/
  • 21. Get the sample code from documentation... You can write your own app with the code available at socket.io/docs or you can get and run the code from socket.io-sample github repository, which contains a list of samples ready to be executed. express-sample1 contains the simplest example you can find on socket.io/docs
  • 22. … code from express-sample1: The Server // express-sample1/server.js var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); server.listen(8080, function () { var port = server.address().port; console.log('Example app listening on port', port); }); app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); }); io.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); Require dependencies and use socket.io with the same express Server Both express and socket.io listen connection on port 8080 (default host is 0.0.0.0) Express is used to easily serve the index page When a client connects, we have a socket instance that can be used to listen/emit event messages: ● the server emit a message (hello world object) on the “news” event (only to the just connected socket) ● the server listen to messages coming from the “my other event” event
  • 23. … code from express-sample1: The index (html page) <!-- index.html --> <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost:8080'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script> GET the library from the server (it is automatically served by socket.io) Connect to socket.io and get a socket to use for the event based communication When the client connects, we have a socket instance that can be used to listen/emit event messages: ● the client listen to messages coming from the “news” event ● once one is received the client emit a message (my data object) to “my other event” event
  • 24. … code from express-sample1: run the sample DOWNLOAD THE SAMPLES AND INSTALL NODE DEPENDENCIES $ git clone git@github.com:h4t0n/socket.io-sample.git $ cd socket.io-sample # install package.json dependencies # including socket.io,express,... $ npm install RUN EXPRESS-SAMPLE1 $ node express-sample1/server.js ● now open your browser at http://127.0.0.1:8080 ● take a look at browser and server console
  • 25. … code from express-sample1: another type of client // client.js var io = require('socket.io-client'); var socket = io.connect('http://localhost: 8080'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); ATTENTION!!!!! To interact with a socket.io server we don’t need a browser at all. We can use a standalone Node.js script client. We have to use the socket.io-client module (already installed in the previous slide command $ npm install ) TODO!! Run the client more times from several terminal and take a look at the server console.
  • 26. From the example we have seen how to: ■ create a socket.io server ■ connect to the server by using a browser or a standalone node app (script) ■ listen and emit event messages ○ from a single client to the server and vice versa SO… VERY SIMPLE AND BASIC CONCEPTS... .. .BUT THERE IS MUCH MORE !!!!!!!
  • 27. Socket.io allows also to: ■ emit messages to all the connected clients ■ broadcast messages ○ sending message to everyone else except the socket that start the communication (that emit an event) ■ send volatile messages ■ send and get acknowledgments ■ listen to other default events ○ such as the disconnect one ■ be used just as a cross browser websocket ○ without events or other socket.io additions The socket.io/docs overview treats these concepts...
  • 28. express-sample2 is a simple web app that easily shows other most used features: ■ broadcast messages ■ emit messages to all connected clients ■ listen to disconnect event ■ emit messages without using socket.io events (just as a cross browser websocket) …or you can take a look at express-sample2 from socket. io-sample repository !!!! SO PLAY WITH EXPRESS-SAMPLE2 !!!
  • 29. ■ namespaces ■ rooms ■ middlewares ■ etcetera… WE’LL SEE MORE FEATURES AND EXAMPLES IN THE TUTORIAL PART 2
  • 30. Thanks! Any questions? You can find me at: @h4t0n h4t0n Andrea Tarquini blog.h4t0n.com