Skip to main content

chumicro-websockets

Non-blocking WebSocket (RFC 6455) client and server, plain and TLS.

RFC 6455 framing and masking, opening + closing handshakes, text + binary + ping/pong, and oversized-message guards. Plays alongside chumicro-http-server for combined HTTP + WS / HTTPS + WSS deployments. Built on chumicro-sockets and chumicro-timing.


Part of the ChuMicro family: small, focused Python libraries for microcontrollers and laptops. Browse all libraries.

Install

# CircuitPython (after `circup bundle-add ChuMicro/ChuMicro-Bundle-Experimental`)
circup install chumicro_websockets

# MicroPython
mpremote mip install github:ChuMicro/ChuMicro-Bundle-Experimental/chumicro_websockets

# CPython
pip install chumicro-websockets-experimental

For bundle setup, pre-compiled .mpy bundles, the experimental channel, and details on PyPI naming, see the chumicro INSTALL guide.

Quick example

Client:

from chumicro_websockets import WebSocketClient, WebSocketState
from chumicro_sockets.sockets_factory import connector_factory
from chumicro_timing import ticks_ms

client = WebSocketClient(transport_factory=connector_factory())
client.on_text = lambda text: print(f"got: {text}")
client.connect("ws://api.example.com/stream")

while client.state != WebSocketState.CLOSED:
    if client.check(ticks_ms()):
        client.handle(ticks_ms())

Server:

from chumicro_websockets import WebSocketServer
from chumicro_sockets import listener
from chumicro_timing import ticks_ms

def on_connection(connection):
    connection.on_text = lambda text: connection.send_text(f"echo: {text}")

server = WebSocketServer(
    listener=listener("0.0.0.0", 8765),
    on_connection=on_connection,
)

while True:
    if server.check(ticks_ms()):
        server.handle(ticks_ms())

What's included

Component Purpose
WebSocketClient Non-blocking RFC 6455 client; runner-shaped check(now_ms) / handle(now_ms).
WebSocketServer + Connection Standalone WebSocket server owning a listening socket; one Connection per accepted client.
WhenOversized Policy for inbound messages above max_message_bytes: DROP_SILENT, DROP_WITH_EVENT, DISCONNECT.
WebSocketState Lifecycle constants (CONNECTING / OPEN / CLOSING / CLOSED).
parse_ws_url(url) Split ws:// / wss:// URLs into (scheme, host, port, path).
make_websocket_key() / derive_accept_key(key) RFC 6455 §4.2.2 nonce + accept-token derivation.
WebSocketError + subclasses Exception hierarchy: protocol, handshake, URL, timeout, backpressure, state.
OPCODE_* / CLOSE_* constants RFC 6455 opcode and close-code values.

Wire-format primitives (FrameParser, encode_frame, the handshake parsers and encoders, the close-payload codec, validate_text_payload, the DEFAULT_* knob constants) live in chumicro_websockets._wire for advanced users and tests; the public top-level surface stays small to keep flash + RAM lean on the device.

Where this fits

Depends on chumicro-sockets and chumicro-timing. Pairs with chumicro-http-server for combined HTTP + WS / HTTPS + WSS deployments.

Platform support

Works on CPython, MicroPython, and CircuitPython.

Examples

Example What it shows
client.py WebSocket client on real CP/MP hardware: brings wifi up via the bundled helpers, connects to a configured echo server, prints every inbound message while a counter ticks alongside.
server.py WebSocket echo server on real CP/MP hardware: accepts inbound connections on the configured host/port and echoes every frame back.
receive_stream.py The next_message stream surface: a generator drains inbound messages linearly under runner.add_generator, with the session ticking alongside.

Contributing

Issues, bug reports, and pull requests are welcome, and so is "I ran it on this board and here's what happened", some of the most useful feedback a hardware project can get. Development happens in the ChuMicro repository, whose contributing guide covers setup and the test workflow.

Docs

📖 Stable docs · Experimental docs

Find this library

License

MIT

Metadata

Release files for chumicro-websockets-experimental 0.27.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chumicro-websockets-experimental 0.27.1
File Size Uploaded
chumicro_websockets_experimental-0.27.1.tar.gz 78.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chumicro-websockets-experimental 0.27.1
File Interpreter ABI Platform
chumicro_websockets_experimental-0.27.1-py3-none-any.whl Python 3 none any Details

Total release size: 110.3 kB

Release files / chumicro_websockets_experimental-0.27.1.tar.gz

Download URL chumicro_websockets_experimental-0.27.1.tar.gz
Size 78.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6839037bea8d14fe3024ea370a476ca062297e8b97a4ec470f2665c800f7f869
BLAKE2b-256 checksum
How to use checksums
7f89cce9d9a0e7a0a93e82e75ab5981cafbbcd93eae78c444f12fa40f5988089
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / chumicro_websockets_experimental-0.27.1-py3-none-any.whl

Download URL chumicro_websockets_experimental-0.27.1-py3-none-any.whl
Size 32.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1a5b4b25c55e9dd00b9170d17e0f45dc58437092ad41f9b3a73bc2599e8de852
BLAKE2b-256 checksum
How to use checksums
b887e66c5302828f3b84f793900e27553015bee738697f26c0c64a9b455484b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release history Release notifications | RSS feed

0.27.3

2 release files

0.27.2

2 release files

This release

0.27.1 This release

2 release files

0.25.0

2 release files

0.24.2

2 release files

0.24.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page