Skip to main content

Typed wrapper for python-socketio with Pydantic validation and dependency injection.

Project description

ZnDraw SocketIO

This package provides an opinionated typed interface to the python-socketio library using pydantic models.

from zndraw_socketio import Wrapper
from pydantic import BaseModel
import socketio

sio = Wrapper(socketio.AsyncClient()) # can be server as well

Emit Pattern

class Ping(BaseModel):
    message: str

# kwargs are passed to socketio's emit method
# emits {"message": "Hello, World!"} to "ping"
await sio.emit(Ping(message="Hello, World!"), **kwargs)
# emits {"message": "Hello, World!"} to "my-ping"
await sio.emit("my-ping", Ping(message="Hello, World!"), **kwargs)
# standard sio behaviour
await sio.emit("event", {"payload": ...})

Call / RPC Pattern

class Pong(BaseModel):
    reply: str

# emits {"message": "Hello, World!"} to "ping" and receives Pong(reply=...) in return
response = await sio.call(Ping(message="Hello, World!"), response_model=Pong)
assert isinstance(response, Pong)
# emits {"message": "Hello, World!"} to "my-ping" and receives Pong(reply=...) in return
response = await sio.call("my-ping", Ping(message="Hello, World!"), response_model=Pong)
assert isinstance(response, Pong)
# standard sio behaviour
response = await sio.call("event", {"payload": ...})
# standard response obj, typically dict

Event Names

By default, the event name is the class name in snake_case. You can customize it by setting the event_name attribute.

class CustomEvent(Event):
    ...

assert CustomEvent.event_name == "custom_event"

you can override it like this:

from typing import ClassVar

class CustomEvent(Event):
    event_name: ClassVar[str] = "my_custom_event"

Union Return Types

You might want to return Response|ErrorResponse from an event handler.

[!NOTE] If your responses share fields, it is recommended to add a discriminator field to avoid ambiguity.

class ProblemDetail(BaseModel):
    """RFC 9457 Problem Details.

    https://www.rfc-editor.org/rfc/rfc9457.html
    """
    kind: Literal["error"] = "error" # The discriminator (nod needed in this example)
    type: str = "about:blank"
    title: str
    status: int
    detail: str | None = None
    instance: str | None = None

class Response(BaseModel):
    kind: Literal["response"] = "response" # The discriminator (not needed in this example)
    data: str

class ServerRequest(BaseModel):
    query: str


response_model = ... # do we need typing Annotated here?

response = await sio.call(ServerRequest(query="..."), response_model=response_model)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zndraw_socketio-0.1.1.tar.gz (183.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zndraw_socketio-0.1.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file zndraw_socketio-0.1.1.tar.gz.

File metadata

  • Download URL: zndraw_socketio-0.1.1.tar.gz
  • Upload date:
  • Size: 183.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for zndraw_socketio-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9979a300e167bdc3a1d9a2937a89a068971d23f50fb542dc4a892a28593c00dd
MD5 062ec6ebf7c70d449ccfae87e4c3d0fa
BLAKE2b-256 b2433ce8e724a7d3900d2b48ce344d495f74f7201d182e14a97b4cb86afd53a1

See more details on using hashes here.

File details

Details for the file zndraw_socketio-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: zndraw_socketio-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for zndraw_socketio-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 60530e9a6975c0afbd9760e4a3056b53c63580876c3f014f2d3942da20817d5a
MD5 c9e32bc67b95b4abbcfbaf09d18eecbc
BLAKE2b-256 bc88b6c35a13666388fc8dbc8fa4c978aa8f8a04142a43ed9c6a463be7846760

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page