A socket.io-client implementation for ESP8266 and Arduino.
To compile you have to add a reference to the linker. To do so edit platform.txt in $ARDUINO_IDE/hardware/esp8266com/esp8266 and add '-lstdc++' to the line
compiler.c.elf.libs= ...
💡 Note: See ESP8266 for Arduino IDE (xtensa-lx106-elf-gcc) and std::map linking error.
Best thing is to use the Arduino Library Manager.
- Go to Sketch > Include Library > Manage Libraries.
- Install WebSockets by Markus Sattler
- Install SocketIoClient
- Select Sketch > Include Library > SocketIoClient
- SocketIoClient::begin()
- SocketIoClient::beginSSL()
- SocketIoClient::disconnect()
- SocketIoClient::emit()
- SocketIoClient::loop()
- SocketIoClient::on()
- SocketIoClient::remove()
- SocketIoClient::setAuthorization()
Open connection to socket.io server.
host
url to socket.io server
port
port to connect on. Defaults to 80 or 443 (SSL)
path
path to connect to on server. Defaults to "/socket.io/?transport=websocket"
socket.begin("my.socket-io.server", 443, "/socket.io/?transport=websocket");
Open SSL connection to socket.io server.
host
url to socket.io server
port
port to connect on. Defaults to 80 or 443 (SSL)
path
path to connect to on server. Defaults to "/socket.io/?transport=websocket"
fingerprint
the SSL fingerprint. Defaults to ""
socket.beginSSL("my.socket-io.server", 443, "/socket.io/?transport=websocket", "26 96 1C 2A 51 07 FD 15 80 96 93 AE F7 32 CE B9 0D 01 55 C4");
Disconnect from the server.
socket.disconnect();
Emits an event to the server.
event
name of the event to be emitted
payload
string of the payload to be sent with the event. Plain strings and object property names should be encapsulated in quotes.
socket.emit("plainString", "\"this is a plain string\"");
socket.emit("jsonObject", "{\"foo\":\"bar\"}");
Processes the websocket. Should be called in Arduino main loop.
Binds a function to an event.
event
name of the event to bind to
callback
callback function to call when the event is triggered
Function signature must be
void (const char * payload, size_t length)
void event(const char * payload, size_t length) {
// do stuff
}
socket.on("event", event);
connect
- when user is connected to serverdisconnect
- when user is disconnected from the server
Removes the previously added event.
event
name of the event binded
socket.remove("event");
Set HTTP Basic auth username and password.
socket.setAuthorization("username", "password");
To go along with the socket.io-client implementation of socket.io the connect
event is triggered upon successfully opened connection to server. To utilize simply add
socket.on("connect", handler)
Likewise, disconnect
event is triggered upon terminated connection.
See Example
Based on the great work of Links2004 / arduinoWebSockets.