Skip to main content

Microsockets

ASGI Websocket Server made with simplicity in mind.

Install

pip install microsockets

Make an app

import microsockets

app = microsockets.Application(
    # event_router, defaults to microsockets.events.EventRouter
    # room_manager, defaults to microsockets.rooms.RoomManager
)

Add an event handler

@app.on("join")
async def handle(ws):
    room = ws.payload["room"]
    user = ws.payload["user"]
    # Rooms managed by rooms.RoomManager, which can be subclassed/replaced
    await ws.join(room)
    # Emit event back to client, may pass a payload as second argument
    await ws.emit("joined")
    # Broadcast to all other users in the room. skip_self=False would send it to current client as well.
    await ws.broadcast("user joined", json.dumps(dict(user=user)), to=[room])

Add middleware

@app.hooks.before_on
async def load_payload(ws, func):
    ws.payload = json.loads(ws.payload)
    await func(ws)

Run with ASGI Server

pip install uvicorn
uvicorn example:app

Install JS client

npm install microsockets

Make client side handlers and emit events

import MicroSocket from microsockets;
const socket = MicroSocket("ws://127.0.0.1:8000");
// socket.on(event, handler): Add event handler
// socket.off(event, handler?): Remove event handler(s)
// socket.send(event, payload): Overridden WebSocket method, requires event. Payload defaults to "".

// MicroSocket returns a modified WebSocket, so the full WebSocket API is still available.
socket.onopen = function (e) {
    document.write("[open] Connection established <br />");
    document.write("Joining room <br />");
    socket.send("join", JSON.stringify({ room: "gamers", user: Math.random() }));
};

socket.on("joined", function (payload) {
    document.write(`[joined] Payload: ${payload} <br />`);
});

socket.on("user joined", function (payload) {
    document.write(`[user joined] Payload: ${payload} <br />`);
});

Run example.py and open example.html in multiple browser tabs to see this basic example.

Metadata

Release files for microsockets 1.0.0

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

Source distribution (sdist)

Source distribution for microsockets 1.0.0
File Size Uploaded Provenance Actions
microsockets-1.0.0.tar.gz 4.0 kB Without provenance Details for microsockets-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for microsockets 1.0.0
File Interpreter ABI Platform Actions
microsockets-1.0.0-py3-none-any.whl Python 3 none any Details for microsockets-1.0.0-py3-none-any.whl

Total release size: 9.8 kB

Release files / microsockets-1.0.0.tar.gz

Download URL microsockets-1.0.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cc5ee30d75bc737fb989c32d5370b217fdc5f336e96687526daad548d93211c1
BLAKE2b-256 checksum
How to use checksums
b35e739c5c0d1c40cfa6bf4cda50b4116094b5758e5c87766baa4d2374f21f5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

Release files / microsockets-1.0.0-py3-none-any.whl

Download URL microsockets-1.0.0-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0ce0024a017c3f2714526ab86652455a2e9c4ca94d452d8a0d6af49958b302e
BLAKE2b-256 checksum
How to use checksums
8f41375bea9af06caa924d0c7afe50a371ac8333f5167ebdf6441c072bd20e45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

Security for microsockets 1.0.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

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