IoTReady - Websockets for Hardware Engineers - Part 1

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

 · 2 min read

Websockets for Hardware Engineers - Part 1

Before we start, what is HTTP?

This is what HTTP REST looks like…

We noticed some issues…

  1. Each request is preceded by an SSL handshake (if HTTPS) and authentication
  2. This can consume significant resources (CPU, time, memory) each time
  3. The server can never send data to the client
  4. 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.


  1. Once established, the connection between client and server is kept-alive for a long period
  2. No theoretical limit
  3. Connection can be kept alive through regular pings
  4. Disconnections are events that can be monitored to trigger reconnection


  1. Once the connection is established, the server can send information to the client at any time.
  2. No need for the client to initiate the request
  3. Client can also send requests to the server


  1. Each websocket connection is actually a channel
  2. Imagine a long pipe where information can flow both ways
  3. Each channel is opened on a specific address, and typically, has a specific purpose
  4. Each client can open multiple channels with the same server
  5. Multiple clients can open channels with the same server

Points to Note 

  1. Websockets is based on TCP so the server cannot broadcast messages to all clients
  2. Instead, on the server, maintain a list of connected clients and send them messages in a loop
  3. Each websocket connection consumes memory so close unused connections
  4. Enforce ping based check of alive clients - if no response, close the connection!
  5. Websockets only concerns itself with the transport layer (on top of TCP) and makes no recommendation about the encoding of the data being transmitted.

  1. You can transmit text
  2. Or JSON
  3. Or even binary files


Tools we need 

  1. websocat or similar command line websockets client for Exercise 1
  2. NodeJS or Python 3 for Exercise 2
  3. ESP32 and ESP-IDF for Exercise 3

1. Public Websocket Server 

  1. Use websocat from a terminal to connect to the Binance websocket streams
  2. These endpoints are known to work:
  3. wss://!miniTicker@arr
  4. wss://
  5. wss://
  6. Open another terminal/tab and connect to a different stream from Binance
  7. For the really patient, check how long the connection remains open for with:

date; websocat wss://; date

2. Local Websocket Server 


  1. Follow this excellent tutorial to set up a local websocket client and server


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

3. ESP32 as a Websocket Server 

  1. Set up ESP-IDF on your computer
  2. Build and flash the Websocket Echo Server example from ESP-IDF on the ESP32
  3. You will need to configure your WiFi settings with menuconfig
  4. Once booted, the ESP32 will print its ip_address. You will need this.
  5. Using websocat, connect to ws://ip_address/ws
  6. Once connected, type and send any string message, e.g. hello
  7. Open a new terminal and repeat the websocat messaging.
  8. What do you notice across the two clients?

More advanced example

  1. Try this slightly more advanced example


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

Ideas, questions or corrections? 

Write to us at

Tej Pochiraju

Tej holds a PhD in Wireless Hardware and has built and sold patents covering innovations in RFID and microwave processing. He has been building IoT products with enterprises, large and small, for nearly 15 years.

No comments yet

No comments yet. Start a new discussion.

Add Comment