IoTReadyTraceable Quality
Talk to us
Menu

Insight

Websockets for Hardware Engineers - Part 1

Websockets are a communication protocol that allow the creation of persistent, bidirectional communication channels between client and server.

The WebSocket logo, in orange

Before we start, what is HTTP?

This is what HTTP REST looks like…

ClientServer
Identify Yourself!
Identify Yourself!
ClientServer
SSL Handshake
SSL Handshake
Client
Authenticate
Authenticate
Server
Authorize
Authorize
Make a Request
Make a Request
Client
Ask for data
Ask for data
Server
Send data
Send data
Typical Client-Server Flow
ClientServer
Identify Yourself!
Identify Yourself!
ClientServer
SSL Handshake
SSL Handshake
Client
Authenticate
Authenticate
Server
Authorize
Authorize
Make a Request
Make a Request
Client
Send data
Send data
Server
Acknowledge
Acknowledge
A bit later...

We noticed some issues…

  • Each request is preceded by an SSL handshake (if HTTPS) and authentication
  • This can consume significant resources (CPU, time, memory) each time

The server can never send data to the client

  • The client needs to implement polling to request data any time it is needed.

What are websockets?

Websockets are a communication protocol that allow the creation of persistent, bidirectional communication channels between client and server.

ClientServer
Identify Yourself!
Identify Yourself!
ClientServer
SSL Handshake
SSL Handshake
Client
Authenticate
Authenticate
Server
Authorize
Authorize
Make a Request
Make a Request
Client
Send data
Send data
Server
Acknowledge
Acknowledge
Event on Server!
Event on Server!
Server
Send data
Send data
Make a Request
Make a Request
Client
Get data
Get data
Server
Send data
Send data
A Websockets Journey

Persistent

Once established, the connection between client and server is kept-alive for a long period

  • No theoretical limit
  • Connection can be kept alive through regular pings
  • Disconnections are events that can be monitored to trigger reconnection

Bidirectional

Once the connection is established, the server can send information to the client at any time.

  • No need for the client to initiate the request
  • Client can also send requests to the server

Channels

Each websocket connection is actually a channel

  • Imagine a long pipe where information can flow both ways
  • Each channel is opened on a specific address, and typically, has a specific purpose
  • Each client can open multiple channels with the same server
  • Multiple clients can open channels with the same server

Points to Note

Websockets is based on TCP so the server cannot broadcast messages to all clients

  • Instead, on the server, maintain a list of connected clients and send them messages in a loop

Each websocket connection consumes memory so close unused connections

  • Enforce ping based check of alive clients - if no response, close the connection!

Websockets only concerns itself with the transport layer (on top of TCP) and makes no recommendation about the encoding of the data being transmitted.

Exercises

Tools we need

1. Public Websocket Server

Use websocat from a terminal to connect to the Binance websocket streams

These endpoints are known to work:

  • wss://stream.binance.com:9443/ws/!miniTicker@arr
  • wss://stream.binance.com:9443/ws/btcusdt@depth
  • wss://stream.binance.com:9443/ws/bnbbtc@depth
  • Open another terminal/tab and connect to a different stream from Binance

For the really patient, check how long the connection remains open for with:

date; websocat wss://stream.binance.com:9443/ws/bnbbtc@depth; date

2. Local Websocket Server

NodeJS

Python

  • Follow this tutorial to set up a local websocket client and server

3. ESP32 as a Websocket Server

Build and flash the Websocket Echo Server example from ESP-IDF on the ESP32

  • You will need to configure your WiFi settings with idf.py menuconfig
  • Once booted, the ESP32 will print its ip_address. You will need this.
  • Using websocat, connect to ws://ip_address/ws
  • Once connected, type and send any string message, e.g. hello

Open a new terminal and repeat the websocat messaging.

  • What do you notice across the two clients?

More advanced example

Questions

  • What are the similarities and differences between HTTP and BLE?
  • What are the similarities and differences between Websockets and BLE?
  • How would you implement an API for client to request changes (e.g. switch on lights)?

Ideas, questions or corrections?

Write to us at sales@iotready.com

Next step

Tell us what you are trying to trace.

Describe the flow: what arrives, what happens to it and what leaves. We will tell you what it takes to capture it.