# Web Socket
How to write Web Socket API client (included with browsers) and server?
- IETF RFC-6455
- Low overhead <1% HTTP, low latency
- open TCP under HTTP, SSL/TLS security
- Full Duplex Communication RT communication protocol over single TCP Connection
- Used for delivery of dynamic data, doesnt replace HTTP
- HTTP Upgrade request, do you spek web-socket? then Handshake
- Web socket protocol supports text and binary data. In terms of Javascript, text refers to as a string, while binary data is represented like ArrayBuffer.
Just messaging to servers from IoT
# web socket server
- express app using
ws
package - What to do when client connects?
- What to send?
- What to do when recieves a message from client?
# web socket client API
- Connection events
open
,message
,close
,send
WebSockets as REST APIs
useEffect(() => {
wsc.current = new WebSocket("ws://localhost:8080");
wsc.current.onopen = (msg) =>
console.log("wsc opened " + JSON.stringify(msg));
wsc.current.onclose = (msg) =>
console.log("wsc closed " + JSON.stringify(msg));
wsc.current.message = (msg) => console.log("recieved " + JSON.stringify(msg));
return () => {
wsc.current.close();
};
}, []);
# Use clases
Simulate Real Time, Polling and Long Polling
- Brodcast messages to all clients, group chat
- IoT type use cases
- exxchange, trading applications
- Replay things, transactions
- Chat apps XMPP (opens new window)
- Multiplayer Games, Collaborative Apps, Developer Tools, Location dependent apps
- Currency Exchanges
# Issues
- They're unreliable & hard to scale
- the connection might drop, and you might find yourself doing polling for the sake of reliability...
# questions
- How well does WS scale? #people making || connections? HTTP is probably behind LB
- How reselient is it to connection drops?
- Does regular browser security constraints apply? like CORS?
- Can data be compressed (gzip) through WS?
- Can send bytes? plain text? yes => watch it in Wireshark
- How to uniquely identify each client?
- High volume storage backends for WS? (opens new window)
- Express vs Django? (opens new window)
- web socket client using react hooks (opens new window)