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.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
microsockets-1.0.0.tar.gz
(4.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file microsockets-1.0.0.tar.gz.
File metadata
- Download URL: microsockets-1.0.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc5ee30d75bc737fb989c32d5370b217fdc5f336e96687526daad548d93211c1
|
|
| MD5 |
6a87f44a043d8f2f2aca19508c37173d
|
|
| BLAKE2b-256 |
b35e739c5c0d1c40cfa6bf4cda50b4116094b5758e5c87766baa4d2374f21f5a
|
File details
Details for the file microsockets-1.0.0-py3-none-any.whl.
File metadata
- Download URL: microsockets-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0ce0024a017c3f2714526ab86652455a2e9c4ca94d452d8a0d6af49958b302e
|
|
| MD5 |
3b86aca13e564b8f553c292320016e30
|
|
| BLAKE2b-256 |
8f41375bea9af06caa924d0c7afe50a371ac8333f5167ebdf6441c072bd20e45
|