Skip to main content

ASGI wrapper for combining FastAPI and Socket.IO in one app

Project description

asyncutilsx

CodSpeed

ASGI wrapper for combining FastAPI and Socket.IO in one app.

Author: Akshat kotpalliwar (alias IntegerAlex)
SPDX-License-Identifier: LGPL-2.1-only

Minimal and pure: one function, no side effects.

Why asyncplus() / asyncutilsx?

Scenario: real-time chat app with REST API

# ❌ With mount() — problematic
app = FastAPI()
app.mount("/socket.io", socket_app)
# - Auth middleware may break Socket.IO handshake
# - CORS middleware may interfere
# - Extra latency on every Socket.IO message

# ✅ With asyncplus — clean
asgi_app = asyncplus(app, sio)
# - Socket.IO gets raw ASGI requests
# - FastAPI gets HTTP requests
# - Each handles its own concerns

FastAPI’s app.mount("/path", other_asgi) works, but you must serve Socket.IO on a subpath and deal with that path everywhere (client, CORS, proxies). asyncplus gives you a single ASGI app: one entry point for the server (e.g. uvicorn), no mount path, same origin for API and Socket.IO. Plug and play—no middleware, no timeouts or circuit breakers added; you keep full control of the ASGI apps you pass in.

Install

pip install asyncutilsx

Usage

from fastapi import FastAPI
from socketio import AsyncServer
from asyncutilsx import asyncplus

app = FastAPI()
sio = AsyncServer(async_mode="asgi")

@sio.event
async def connect(sid, environ):
    print("connect", sid)

asgi_app = asyncplus(app, sio)
# Run with: uvicorn asgi:asgi_app
  • HTTP (except /socket.io/*) → FastAPI
  • HTTP /socket.io/* → Socket.IO (polling)
  • WebSocket → Socket.IO

Optional: asyncplus(app, sio, socketio_path="/custom/", debug_hook=..., socketio_fallback_on_error=False, timeout=30.0).

For custom routing (e.g. SSE, gRPC), use the pure router(routes, default_app) function: pass a sequence of (predicate, app) pairs; first match wins; default_app used when none match.

See docs/user_guide.md for common patterns and examples.

Development

pip install -e ".[dev]"
pytest

Benchmarks

Performance benchmarks are continuously monitored with CodSpeed. To run benchmarks locally:

pytest benchmarks/ --codspeed

License

LGPL-2.1-only

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

asyncutilsx-0.2.0.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

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

asyncutilsx-0.2.0-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file asyncutilsx-0.2.0.tar.gz.

File metadata

  • Download URL: asyncutilsx-0.2.0.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for asyncutilsx-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bbabf053e9b9c3f56523bd3c02f766ac6e5c06f028d61e5faff8f3a8d94e505b
MD5 54e7fb5837709bfed255ec8be22c5868
BLAKE2b-256 781ba621f3728ddf7065b5cfe26f622f63b8bd44114a5f6b236c1636fc69145e

See more details on using hashes here.

File details

Details for the file asyncutilsx-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: asyncutilsx-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for asyncutilsx-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 189da8e48f17ef5ee5cb62839ad4b859bb14bafab714fcf5ded03ee9e73268f0
MD5 1f1b1a2ede62d3265950773d2f7cc7fa
BLAKE2b-256 dd44e68b4205e368d3268f12be31b2e45efaa934286a3cb892b8d33d04d72f54

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