Build community with open collaboration
Techstars backed
I love great product and build them.
I'm developing my dream with code.
Enable users to receive information as soon as it is published by its authors.
60 FPS makes a smooth video.
60 frames per second (FPS)
1000 millisecond / 60 frames
~= 16.67 ms/frame
if it only shows when data arrives
that's what most modern web app does
server can't actively push data to client
real-time requires to send updates to client
HTTP | WebSocket | |
---|---|---|
Overhead | 100s bytes | 2-6 bytes |
Latency (traditional) | New connection every time | None: use existing connection |
Latency (long-polling) | Time to set up next request | No waiting |
see some examples
Agar.io
An online multipleplayer game
also uses it to update pages
Socket.IO
Socket.IO
Everyone now is happy
Socket.IO
Socket.IO
Client
<script src="/socket.io/socket.io.js"></script>
Include socket.io-client
script in the html.
Server
npm install --save socket.io
Install socket.io
from npm
const server = require('http').createServer() const io = require('socket.io')(server) server.listen(3000)
Create HTTP server and bind with socket.io
in the node.js app.
const socket = io()
Client tries to connect chat room
io.on('connection', socket => { console.log('a user connected') socket.on('disconnect', () => { console.log('user disconnected') }) })
Server accepts client connection
io.emit('room message', 'room is ready')
Server broadcasts room status
socket.emit('chat message', 'hello world!')
Client emits chat message to room
socket.on('chat message', msg => { console.log('message: ' + msg) })
Server receives the message
excepts for a certain socket
io.on('connection', socket => { socket.on('chat message', msg => { console.log('message: ' + msg) socket.broadcast.emit(msg) }) })
Server broadcasts chat message to other clients
an alternative for sending from server to client
Can be easily polyfilled!
https://caniuse.com/#search=server sent
HTTP | WebSocket | SSE | |
---|---|---|---|
Direction | Client to Server | Bidirectional | Server to Client |
Native support | 100% | 90% | 84% |
Overhead | High | Low | Medium |
Latency | 1-2s | < 1s | 1s |
Socket.IO
is the one for all