# Web Socket

2 min read, 368 words websocketwsapi

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

Why use web sockets

Workarounds to web socket

# 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

Subscribe to our Newsletter

If you like my work and think it was helpful kindly support my work


Buy Me A Coffee