Convert Figma logo to code with AI

Links2004 logoarduinoWebSockets

arduinoWebSockets

1,869
554
1,869
263

Top Related Projects

21,534

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js

22,033

Package gorilla/websocket is a fast, well-tested and widely used WebSocket implementation for Go.

Simple, secure & standards compliant web server for the most demanding of applications

60,879

Realtime application framework (Node.JS server)

C++ websocket client/server library

A curated list of Websocket libraries and resources.

Quick Overview

Links2004/arduinoWebSockets is a WebSocket client and server library for Arduino and ESP8266/ESP32. It provides an easy-to-use interface for implementing WebSocket functionality in Arduino projects, enabling real-time, bidirectional communication between devices and web applications.

Pros

  • Easy integration with Arduino and ESP8266/ESP32 projects
  • Supports both client and server implementations
  • Includes SSL/TLS support for secure connections
  • Compatible with various Arduino boards and ESP8266/ESP32 modules

Cons

  • Limited documentation and examples for advanced use cases
  • May require additional memory and processing power, which can be a constraint on some Arduino boards
  • Potential compatibility issues with older Arduino IDE versions or boards

Code Examples

  1. Creating a WebSocket server:
#include <WebSocketsServer.h>

WebSocketsServer webSocket = WebSocketsServer(81);

void setup() {
    webSocket.begin();
    webSocket.onEvent(webSocketEvent);
}

void loop() {
    webSocket.loop();
}
  1. Sending a message to all connected clients:
void sendToAllClients(String message) {
    webSocket.broadcastTXT(message);
}
  1. Handling incoming WebSocket events:
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
    switch(type) {
        case WStype_DISCONNECTED:
            Serial.printf("[%u] Disconnected!\n", num);
            break;
        case WStype_CONNECTED:
            Serial.printf("[%u] Connected!\n", num);
            break;
        case WStype_TEXT:
            Serial.printf("[%u] Received text: %s\n", num, payload);
            break;
    }
}

Getting Started

  1. Install the library in Arduino IDE: Sketch > Include Library > Manage Libraries > Search for "WebSockets" > Install
  2. Include the library in your sketch:
#include <WebSocketsServer.h>

WebSocketsServer webSocket = WebSocketsServer(81);

void setup() {
    Serial.begin(115200);
    WiFi.begin("SSID", "PASSWORD");
    
    while(WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    
    webSocket.begin();
    webSocket.onEvent(webSocketEvent);
    Serial.println("WebSocket server started");
}

void loop() {
    webSocket.loop();
}

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
    // Handle WebSocket events here
}

Competitor Comparisons

21,534

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js

Pros of ws

  • More robust and feature-rich, suitable for complex Node.js applications
  • Better performance and scalability for high-traffic scenarios
  • Extensive documentation and active community support

Cons of ws

  • Not designed for embedded systems or microcontrollers
  • Larger codebase and dependencies, potentially increasing resource usage
  • Steeper learning curve for beginners compared to simpler libraries

Code Comparison

ws:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });
});

arduinoWebSockets:

#include <WebSocketsServer.h>

WebSocketsServer webSocket = WebSocketsServer(81);

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  // Handle WebSocket events
}

void setup() {
  webSocket.begin();
  webSocket.onEvent(webSocketEvent);
}

Summary

ws is a powerful WebSocket library for Node.js applications, offering extensive features and scalability. arduinoWebSockets is tailored for embedded systems and microcontrollers, providing a simpler implementation for resource-constrained environments. Choose based on your project requirements and target platform.

22,033

Package gorilla/websocket is a fast, well-tested and widely used WebSocket implementation for Go.

Pros of gorilla/websocket

  • Written in Go, offering better performance and concurrency support
  • More comprehensive WebSocket protocol implementation
  • Wider adoption and community support in the Go ecosystem

Cons of gorilla/websocket

  • Not suitable for Arduino or embedded systems
  • Requires more system resources compared to arduinoWebSockets
  • Steeper learning curve for developers new to Go

Code Comparison

arduinoWebSockets:

#include <WebSocketsClient.h>

WebSocketsClient webSocket;
webSocket.begin("example.com", 80, "/ws");
webSocket.onEvent(webSocketEvent);
webSocket.setReconnectInterval(5000);

gorilla/websocket:

import "github.com/gorilla/websocket"

var upgrader = websocket.Upgrader{}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
    log.Print("upgrade failed: ", err)
    return
}

Summary

arduinoWebSockets is tailored for Arduino and embedded systems, offering a lightweight solution for WebSocket communication. It's ideal for IoT projects and resource-constrained environments.

gorilla/websocket, on the other hand, is a robust Go library designed for server-side applications. It provides a more complete WebSocket implementation and is better suited for scalable web applications and services running on more powerful hardware.

The choice between these libraries depends on the target platform, available resources, and specific project requirements.

Simple, secure & standards compliant web server for the most demanding of applications

Pros of uWebSockets

  • Higher performance and scalability, capable of handling millions of connections
  • More feature-rich, supporting advanced WebSocket features and protocols
  • Better suited for large-scale, high-performance applications

Cons of uWebSockets

  • More complex to set up and use, especially for beginners
  • Not specifically designed for Arduino or embedded systems
  • Requires more system resources, which may not be suitable for constrained devices

Code Comparison

uWebSockets:

uWS::Hub h;
h.onMessage([](uWS::WebSocket<uWS::SERVER> *ws, char *message, size_t length, uWS::OpCode opCode) {
    ws->send(message, length, opCode);
});
h.listen(3000);
h.run();

arduinoWebSockets:

WebSocketsServer webSocket = WebSocketsServer(81);
webSocket.begin();
webSocket.onEvent(webSocketEvent);
webSocket.loop();

Summary

uWebSockets is a high-performance WebSocket library designed for scalability and advanced features, making it suitable for large-scale applications. However, it may be overkill for simple Arduino projects. arduinoWebSockets, on the other hand, is specifically tailored for Arduino and embedded systems, offering easier setup and lower resource requirements, but with potentially lower performance for high-load scenarios.

60,879

Realtime application framework (Node.JS server)

Pros of Socket.IO

  • Broader platform support, including web browsers and various server-side environments
  • Built-in features like automatic reconnection and room-based broadcasting
  • Extensive documentation and large community support

Cons of Socket.IO

  • Heavier library size, which may not be suitable for resource-constrained devices
  • More complex setup and configuration compared to simpler WebSocket implementations
  • Potential overhead due to additional features that may not be necessary for all use cases

Code Comparison

Socket.IO (client-side):

const socket = io('http://localhost:3000');
socket.on('connect', () => {
  console.log('Connected to server');
  socket.emit('message', 'Hello, server!');
});

arduinoWebSockets (Arduino):

WebSocketsClient webSocket;
webSocket.begin("192.168.1.1", 81, "/");
webSocket.onEvent(webSocketEvent);
webSocket.setReconnectInterval(5000);

Key Differences

  • Socket.IO provides a higher-level abstraction with additional features, while arduinoWebSockets focuses on a lightweight WebSocket implementation for Arduino and ESP8266/ESP32 devices.
  • Socket.IO offers cross-platform compatibility, whereas arduinoWebSockets is specifically designed for embedded systems.
  • arduinoWebSockets is more suitable for projects with limited resources, while Socket.IO excels in complex, feature-rich applications.

C++ websocket client/server library

Pros of websocketpp

  • More comprehensive and feature-rich WebSocket implementation
  • Supports both client and server-side WebSocket functionality
  • Better suited for desktop and server applications

Cons of websocketpp

  • Larger codebase and higher resource requirements
  • Steeper learning curve due to more complex API
  • Not optimized for embedded systems or Arduino platforms

Code Comparison

arduinoWebSockets:

#include <WebSocketsClient.h>

WebSocketsClient webSocket;
webSocket.begin("example.com", 80, "/");
webSocket.onEvent(webSocketEvent);
webSocket.loop();

websocketpp:

#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>

typedef websocketpp::client<websocketpp::config::asio_client> client;
client c;
c.init_asio();
c.connect("ws://example.com:80/");
c.run();

Summary

arduinoWebSockets is designed specifically for Arduino and embedded systems, offering a simpler API and smaller footprint. It's ideal for IoT projects and resource-constrained environments.

websocketpp is a more robust and versatile library, suitable for a wide range of applications, including desktop and server-side development. It provides more features and flexibility but requires more resources and has a steeper learning curve.

Choose arduinoWebSockets for Arduino and lightweight embedded projects, and websocketpp for more complex, full-featured applications on desktop or server platforms.

A curated list of Websocket libraries and resources.

Pros of awesome-websockets

  • Comprehensive list of WebSocket resources and libraries for various languages and platforms
  • Regularly updated with community contributions
  • Serves as a valuable reference for developers seeking WebSocket solutions

Cons of awesome-websockets

  • Not an actual WebSocket implementation, just a curated list
  • Requires additional research to determine the best tool for specific use cases
  • May include outdated or deprecated libraries if not actively maintained

Code comparison

Not applicable, as awesome-websockets is a curated list and doesn't contain actual implementation code.

arduinoWebSockets, on the other hand, provides a WebSocket client and server implementation for Arduino. Here's a simple example of creating a WebSocket client:

#include <WebSocketsClient.h>

WebSocketsClient webSocket;

void setup() {
  webSocket.begin("example.com", 80, "/");
  webSocket.onEvent(webSocketEvent);
}

While awesome-websockets offers a wide range of WebSocket resources, arduinoWebSockets provides a specific implementation for Arduino devices. The choice between them depends on whether you need a reference for various WebSocket tools or a concrete implementation for Arduino projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

WebSocket Server and Client for Arduino Build Status

a WebSocket Server and Client for Arduino based on RFC6455.

Supported features of RFC6455
  • text frame
  • binary frame
  • connection close
  • ping
  • pong
  • continuation frame
Limitations
  • max input length is limited to the ram size and the WEBSOCKETS_MAX_DATA_SIZE define
  • max output length has no limit (the hardware is the limit)
  • Client send big frames with mask 0x00000000 (on AVR all frames)
  • continuation frame reassembly need to be handled in the application code
Limitations for Async
  • Functions called from within the context of the websocket event might not honor yield() and/or delay(). See this issue for more info and a potential workaround.
  • wss / SSL is not possible.
Supported Hardware
  • ESP8266 Arduino for ESP8266
  • ESP32 Arduino for ESP32
  • ESP31B
  • Raspberry Pi Pico W Arduino for Pico
  • Particle with STM32 ARM Cortex M3
  • ATmega328 with Ethernet Shield (ATmega branch)
  • ATmega328 with enc28j60 (ATmega branch)
  • ATmega2560 with Ethernet Shield (ATmega branch)
  • ATmega2560 with enc28j60 (ATmega branch)
  • Arduino UNO R4 WiFi
  • Arduino Nano 33 IoT, MKR WIFI 1010 (requires WiFiNINA library)
  • Seeeduino XIAO, Seeeduino Wio Terminal (requires rpcWiFi library)
Note:

version 2.0.0 and up is not compatible with AVR/ATmega, check ATmega branch.

version 2.3.0 has API changes for the ESP8266 BareSSL (may brakes existing code)

Arduino for AVR not supports std namespace of c++.

wss / SSL

supported for:

  • wss client on the ESP8266
  • wss / SSL is not natively supported in WebSocketsServer however it is possible to achieve secure websockets by running the device behind an SSL proxy. See Nginx for a sample Nginx server configuration file to enable this.

Root CA Cert Bundles for SSL/TLS connections

Secure connections require the certificate of the server to be verified. One option is to provide a single certificate in the chain of trust. However, for flexibility and robustness, a certificate bundle is recommended. If a server changes the root CA from which it derives its certificates, this will not be a problem. With a single CA cert it will not connect.

Including a bundle with all CA certs will use 77.2 kB but this list can be reduced to 16.5 kB for the 41 most common. This results in 90% absolute usage coverage and 99% market share coverage according to W3Techs. The bundle is inserted into the compiled firmware. The bundle is not loaded into RAM, only its index.

ESP Async TCP

This libary can run in Async TCP mode on the ESP.

The mode can be activated in the WebSockets.h (see WEBSOCKETS_NETWORK_TYPE define).

ESPAsyncTCP libary is required.

High Level Client API

  • begin : Initiate connection sequence to the websocket host.
void begin(const char *host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
void begin(String host, uint16_t port, String url = "/", String protocol = "arduino");
  • onEvent: Callback to handle for websocket events
 void onEvent(WebSocketClientEvent cbEvent);
  • WebSocketClientEvent: Handler for websocket events
 void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length)

Where WStype_t type is defined as:

  typedef enum {
      WStype_ERROR,
      WStype_DISCONNECTED,
      WStype_CONNECTED,
      WStype_TEXT,
      WStype_BIN,
      WStype_FRAGMENT_TEXT_START,
      WStype_FRAGMENT_BIN_START,
      WStype_FRAGMENT,
      WStype_FRAGMENT_FIN,
      WStype_PING,
      WStype_PONG,
  } WStype_t;

Issues

Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues

License and credits

The library is licensed under LGPLv2.1

libb64 written by Chris Venter. It is distributed under Public Domain see LICENSE.