Socket
BootcampBackend2020-12-17
Socket.IO
- cross browser (including IE6)
- Real-time and bi-directional persistent connection (WebSocket)
- Wworking for https and cross-domain (if use webSocket or JSONP)
- Authentication . Cookie & Session . Cookie does not cross domain, use SSL + Access Token instead
- Use Nginx for loading Balancing.
- Configurations . Store(default: memoryStore, single process only) . transports(default: websocket, htmlfile, xhr-polling, jsonp-polling) . authorization (default: false)
- Socket.IO primarily use WebSocket, but can fallback to other methods lik AJAX Long polling, JSONP polling In addition to 1-to-1 communication as in WebSocket, it enables broadcasting to multiple nodes
modules
- Redis
- Socket.io
- isomorphic-fetch
- node-jsonwebtoken
Q&A
- Pub/Sub or Message Queue
Processing flow
- in browser, when React Component is rendered, in
componentDidMountfires asocket.emit, - in server,
socket.onis triggered and doesredis publish - in server,
redis.ondoes redis-stuff (hgetall…) - in server,
redis.onfiressocket.emit()to pass/broadcast data to browser. - in browser,
socket.onis triggered to process the data, and render the components.
Reference
- Forever Iframe
- XMLHttpRequest Long Polling
- Cometed
- Websocket
