SlideShare a Scribd company logo
WebSocket
Two-way realtime communication for websites
Chat on a webpage




“Four times.”   “Four times.”
Chat on a webpage

Requirements:

• Send messages client→server
• Send messages server→client
• Detect when client is connected to server
  (“presence”)
Traditional HTTP



XMLHttpRequest                        Long Polling
(HTTP from JavaScript)                (Long-running XMLHttpRequest)
var request = new XMLHttpRequest();   var request = new XMLHttpRequest();
request.open("POST", "/send");        request.open("GET", "/receive");
request.send("Four times.");          request.send();
                                      request.onload = function() {
                                          appendMessage(this.responseText);
                                          startNewRequest();
                                      };
Other techniques and problems

• Long-polling HTTP requests: XHR, script-tag
• Streaming HTTP request: XHR, iframe
• Using rtmp with Flash

• Misuse of HTTP (proxies, browsers, servers)
• Performance (network, httpd cpu)
• Not supported in all browsers
WebSocket

• Full-duplex persistent connection over TCP
• Designed by W3C for the web (RFC Dec. ’11)
• Uses port 80
• Handshake features HTTP’s “Upgrade”-header
• The TLS version passes some HTTP-proxies
• Ping/pong frames for staying alive
WebSocket JavaScript API


var ws = new WebSocket("wss://host");   var ws = new WebSocket("wss://host");
ws.send("Four times.");                 ws.onmessage = function(event) {
ws.send("It has to be.");                   appendMessage(event.data);
                                        };
Demo
├── README.md
├── client-sockjs
│   ├── chat.js
│   └── index.html
├── client-websocket
│   ├── chat.js
│   └── index.html
└── server
 ├── package.json
 └── server.js

 https://github.com/njam/websocket-demo-chat
Compatibility




http://caniuse.com/websockets
Emulation

• WebSocket-like client-API plus server
• Enables realtime-comm. in most used browsers

• SockJS: Node.js, Erlang, Ruby, Python, ..
• Socket.IO: Node.js, Java, Erlang, PHP, .......
• Ratchet: PHP
• APE (C), Atmosphere (Java), Tambur.io (WS),
  Pusher (WS)
Server application Integration

1. Same application base:
   WS-server which reuses your app code



                Application code


   Apache                          WS Server
Server application Integration

2. Message Queue / Service API:
   Messaging between app and WS server
  (Redis-SockJS push-only broker: cargomedia/socket-redis)

                     Message Queue
Application code                            Broker


    Apache                                WS Server
Thanks



cargomedia.ch/jobs

More Related Content

PPTX
Web sockets in Java
PPTX
Magento Meetup Wrocław 6. "Docker for Mac - possible solutions to performance...
PPT
WebSockets and Java
PDF
Nginx وب سروری برای تمام فصول
PDF
WebSockets with Spring 4
PDF
Writing a fast HTTP parser
PDF
JugTAAS ReSTful
Web sockets in Java
Magento Meetup Wrocław 6. "Docker for Mac - possible solutions to performance...
WebSockets and Java
Nginx وب سروری برای تمام فصول
WebSockets with Spring 4
Writing a fast HTTP parser
JugTAAS ReSTful

What's hot (20)

PDF
From One to a Cluster
PDF
Alternative Infrastucture
PDF
Phorum MySQL tricks
PDF
Fighting XMPP abuse and spam with ejabberd - ejabberd Workshop #1
PDF
RabbitMQ + CouchDB = Awesome
PDF
Netty - anfix tech&beers
PPTX
Asynchronous Web Programming with HTML5 WebSockets and Java
PPTX
HTTP::Parser::XS - writing a fast & secure XS module
PDF
Using Communication and Messaging API in the HTML5 World
PDF
Realtime web application with java
PDF
Introducing TokuMX: The Performance Engine for MongoDB (NYC.rb 2013-12-10)
PPTX
Scaling application with RabbitMQ
PPTX
Websockets and SockJS, Real time chatting
PPT
High Performance Wordpress: “Faster, Cheaper, Easier : Pick Three”
PDF
Dissecting the rabbit: RabbitMQ Internal Architecture
PDF
Communication in Python and the C10k problem
PDF
WordPress Performance & Scalability
PDF
GWT Web Socket and data serialization
PPTX
WP-CLI Workshop at WordPress Meetup Cluj-Napoca
PDF
Scaling WordPress
From One to a Cluster
Alternative Infrastucture
Phorum MySQL tricks
Fighting XMPP abuse and spam with ejabberd - ejabberd Workshop #1
RabbitMQ + CouchDB = Awesome
Netty - anfix tech&beers
Asynchronous Web Programming with HTML5 WebSockets and Java
HTTP::Parser::XS - writing a fast & secure XS module
Using Communication and Messaging API in the HTML5 World
Realtime web application with java
Introducing TokuMX: The Performance Engine for MongoDB (NYC.rb 2013-12-10)
Scaling application with RabbitMQ
Websockets and SockJS, Real time chatting
High Performance Wordpress: “Faster, Cheaper, Easier : Pick Three”
Dissecting the rabbit: RabbitMQ Internal Architecture
Communication in Python and the C10k problem
WordPress Performance & Scalability
GWT Web Socket and data serialization
WP-CLI Workshop at WordPress Meetup Cluj-Napoca
Scaling WordPress
Ad

Viewers also liked (20)

PDF
Admit or Die: Addressing Admission Decision Factors on Websites
PDF
Ejercicios de Propulsión
PPT
Modeling and simulation ch 1
PDF
Emos chile
PPTX
Tahona caf atenci-n al cliente
PDF
sociale media als didactisch hulpmiddel
PDF
Sentimiento Medular Ítalo Violo Venezuela 2009
PDF
Skyrocket training company profile
PPTX
Working ett present.
PPTX
La resurrección a través de la pintura. celia martín (6º curso de primaria)
PPTX
Ehealth in japan
PDF
Soziale Netzwerke im Versicherungsvertrieb – Digital noch näher am Kunden
PDF
Eotvos Lorand University (ELTE) services for incoming students spring 2014-2015
PDF
iPhone vs. Samsung Galaxy S II - User Experience Case Study (English)
PPTX
El teletrabajo y el trabajo a domicilio
PPTX
Novocentro Ecomadera UTPL
PPTX
Reingeniería de Marca de quesos
PPTX
Pizza Point Of Sale System - SpeedLine Overview
PPTX
Red Tide Phenomenon
PDF
Networkvial difunde : El Manual de buenas practicas de seguridad vial
Admit or Die: Addressing Admission Decision Factors on Websites
Ejercicios de Propulsión
Modeling and simulation ch 1
Emos chile
Tahona caf atenci-n al cliente
sociale media als didactisch hulpmiddel
Sentimiento Medular Ítalo Violo Venezuela 2009
Skyrocket training company profile
Working ett present.
La resurrección a través de la pintura. celia martín (6º curso de primaria)
Ehealth in japan
Soziale Netzwerke im Versicherungsvertrieb – Digital noch näher am Kunden
Eotvos Lorand University (ELTE) services for incoming students spring 2014-2015
iPhone vs. Samsung Galaxy S II - User Experience Case Study (English)
El teletrabajo y el trabajo a domicilio
Novocentro Ecomadera UTPL
Reingeniería de Marca de quesos
Pizza Point Of Sale System - SpeedLine Overview
Red Tide Phenomenon
Networkvial difunde : El Manual de buenas practicas de seguridad vial
Ad

Similar to WebSocket (20)

PPT
JUG louvain websockets
PDF
Introduction to WebSockets
KEY
The HTML5 WebSocket API
PDF
Building HTML5 WebSocket Apps in Java at JavaOne Latin America 2012
PDF
HTML5/JavaScript Communication APIs - DPC 2014
KEY
Pushing the web — WebSockets
PDF
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
KEY
Socket.io
PPTX
Real time websites and mobile apps with SignalR
PPTX
Programming WebSockets with Glassfish and Grizzly
PPT
Get Real: Adventures in realtime web apps
PPT
PPTX
WebSockets in JEE 7
PPTX
Building WebSocket and Server Side Events Applications using Atmosphere
PDF
HTML5 Websockets and Java - Arun Gupta
PPTX
KEY
Realtime rocks
PDF
Websocket 1.0
PDF
Real-Time with Flowdock
ZIP
Websockets at tossug
JUG louvain websockets
Introduction to WebSockets
The HTML5 WebSocket API
Building HTML5 WebSocket Apps in Java at JavaOne Latin America 2012
HTML5/JavaScript Communication APIs - DPC 2014
Pushing the web — WebSockets
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
Socket.io
Real time websites and mobile apps with SignalR
Programming WebSockets with Glassfish and Grizzly
Get Real: Adventures in realtime web apps
WebSockets in JEE 7
Building WebSocket and Server Side Events Applications using Atmosphere
HTML5 Websockets and Java - Arun Gupta
Realtime rocks
Websocket 1.0
Real-Time with Flowdock
Websockets at tossug

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Machine Learning_overview_presentation.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PPT
Teaching material agriculture food technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Group 1 Presentation -Planning and Decision Making .pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Machine learning based COVID-19 study performance prediction
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
Machine Learning_overview_presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Network Security Unit 5.pdf for BCA BBA.
Spectral efficient network and resource selection model in 5G networks
Agricultural_Statistics_at_a_Glance_2022_0.pdf
cloud_computing_Infrastucture_as_cloud_p
Assigned Numbers - 2025 - Bluetooth® Document
Diabetes mellitus diagnosis method based random forest with bat algorithm
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
Teaching material agriculture food technology

WebSocket

  • 2. Chat on a webpage “Four times.” “Four times.”
  • 3. Chat on a webpage Requirements: • Send messages client→server • Send messages server→client • Detect when client is connected to server (“presence”)
  • 4. Traditional HTTP XMLHttpRequest Long Polling (HTTP from JavaScript) (Long-running XMLHttpRequest) var request = new XMLHttpRequest(); var request = new XMLHttpRequest(); request.open("POST", "/send"); request.open("GET", "/receive"); request.send("Four times."); request.send(); request.onload = function() { appendMessage(this.responseText); startNewRequest(); };
  • 5. Other techniques and problems • Long-polling HTTP requests: XHR, script-tag • Streaming HTTP request: XHR, iframe • Using rtmp with Flash • Misuse of HTTP (proxies, browsers, servers) • Performance (network, httpd cpu) • Not supported in all browsers
  • 6. WebSocket • Full-duplex persistent connection over TCP • Designed by W3C for the web (RFC Dec. ’11) • Uses port 80 • Handshake features HTTP’s “Upgrade”-header • The TLS version passes some HTTP-proxies • Ping/pong frames for staying alive
  • 7. WebSocket JavaScript API var ws = new WebSocket("wss://host"); var ws = new WebSocket("wss://host"); ws.send("Four times."); ws.onmessage = function(event) { ws.send("It has to be."); appendMessage(event.data); };
  • 8. Demo ├── README.md ├── client-sockjs │   ├── chat.js │   └── index.html ├── client-websocket │   ├── chat.js │   └── index.html └── server ├── package.json └── server.js https://github.com/njam/websocket-demo-chat
  • 10. Emulation • WebSocket-like client-API plus server • Enables realtime-comm. in most used browsers • SockJS: Node.js, Erlang, Ruby, Python, .. • Socket.IO: Node.js, Java, Erlang, PHP, ....... • Ratchet: PHP • APE (C), Atmosphere (Java), Tambur.io (WS), Pusher (WS)
  • 11. Server application Integration 1. Same application base: WS-server which reuses your app code Application code Apache WS Server
  • 12. Server application Integration 2. Message Queue / Service API: Messaging between app and WS server (Redis-SockJS push-only broker: cargomedia/socket-redis) Message Queue Application code Broker Apache WS Server