ArduinoWebsockets: a websocket client for the ESP32 and ESP8266
Yes, with limits. You can use the ArduinoWebsockets library online in Mokxi with nothing to install, and the sketch runs in your browser. Part of it is not simulated, and the notes below say which. The example below, "The Esp32-Client example, against the simulated echo server", runs on the ESP32 DevKit V1.
Built in on Mokxi: #include <ArduinoWebsockets.h> and it builds, with nothing to install. Free, and no account needed.
What ArduinoWebsockets does
A websocket keeps one connection open so a server can push data to your board the moment it has some, and your board can answer without opening a new request each time. ArduinoWebsockets by Gil Maimon is a client and server for the ESP32 and ESP8266, and its Esp32-Client example is one of the most copied websocket sketches there is.
You create a WebsocketsClient, connect to a host, port and path, send text with send(), and register a callback with onMessage() that runs for every message the server sends. The one thing people forget is poll(): messages are only read and your callbacks only run when the loop calls it.
In Mokxi the network carries web requests and no raw TCP, so the client rides on the same simulated echo server as the Links2004 WebSockets library. connect() to echo.mokxi works and the server answers every message with the same text, which is enough to run the whole example. Any other host, or a wss address, returns false and says why.
| Library Manager name | ArduinoWebsockets |
|---|---|
| Author | Gil Maimon |
| Include | #include <ArduinoWebsockets.h> |
| Boards | ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-S3-CAM, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi |
| In the simulator | Runs, with a gap the note below names. |
Install it
It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-S3-CAM, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi.
In Mokxi it is built in: #include <ArduinoWebsockets.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "ArduinoWebsockets" by Gil Maimon from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add ArduinoWebsockets to lib_deps in PlatformIO. Mokxi follows the calls of release 0.5.4, so a sketch written here builds there too.
What to know in the simulator: Not a WebSocket on a wire: the simulated network carries HTTP requests and no raw TCP, so the client rides on the same simulated echo server as WebSockets.h. connect() to echo.mokxi works, each send() is one HTTP POST and the echo comes back from poll(); any other host, or wss://, returns false and says why. The server builds and never gets a client.
The calls you will use
In the order a sketch meets them.
WebsocketsClient client
The connection object. It lives in the websockets namespace, so most sketches add using namespace websockets.
client.connect("echo.mokxi", 80, "/")
Opens the connection to a host, port and path and returns true once the handshake is done. A single ws:// URL string works too.
client.send("Hello Server")
Sends one text message. sendBinary() sends raw bytes instead. Both return false when the connection is not open.
client.onMessage(callback)
Registers a function or lambda that receives each incoming WebsocketsMessage. Its data() is the text, and isText() and isBinary() tell the kind.
client.onMessage([](WebsocketsMessage message) {
Serial.println(message.data());
});client.poll()
Reads what has arrived and runs the callbacks. Call it from loop() often, usually guarded by client.available().
client.ping()
Sends a ping. The answer arrives as a GotPong event, which onEvent() reports.
Example: The Esp32-Client example, against the simulated echo server
On the ESP32 DevKit V1. It runs in the browser exactly as printed.
#include <ArduinoWebsockets.h>
#include <WiFi.h>
const char* ssid = "Mokxi-Lab";
const char* password = "breadboard";
const char* websockets_server_host = "echo.mokxi";
const uint16_t websockets_server_port = 80;
using namespace websockets;
WebsocketsClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
for (int i = 0; i < 10 && WiFi.status() != WL_CONNECTED; i++) {
Serial.print(".");
delay(1000);
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("No Wifi!");
return;
}
Serial.println("Connected to Wifi, Connecting to server.");
bool connected = client.connect(websockets_server_host, websockets_server_port, "/");
if (connected) {
Serial.println("Connected!");
client.send("Hello Server");
} else {
Serial.println("Not Connected!");
}
client.onMessage([&](WebsocketsMessage message) {
Serial.print("Got Message: ");
Serial.println(message.data());
});
client.onEvent([](WebsocketsEvent event, String) {
if (event == WebsocketsEvent::GotPong) Serial.println("Got a pong");
});
client.ping();
}
void loop() {
if (client.available()) {
client.poll();
}
delay(500);
}Common errors and how to fix them
Not Connected!
connect() returned false: the host, port or path is wrong, the server is not running, or the board has no network yet. In Mokxi only echo.mokxi answers, and wss addresses never connect because TLS is not simulated.
Messages never arrive
poll() is not being called, or it is only called while the board is stuck in a long delay(). Call client.poll() on every pass of loop() and keep the delay in loop() short.
The sketch compiles on the ESP32 but not on an Uno
The library needs a network-capable board. It builds for the ESP32 family and the ESP8266; an Arduino Uno has no WiFi, so add an ESP board or use a library for its Ethernet shield instead.
Keep going
Try ArduinoWebsockets without installing anything
Open the example, change the sketch, and keep your version in a free account.