Realtime WebSocket Events for Python — Django, Flask & FastAPI
Trigger real-time WebSocket events from Django, Flask, and FastAPI. Zero-dependency server SDK using only the Python standard library — no WebSocket server to run or manage.
pip install apinator-server Server npm install @apinator/client Client Sending real-time WebSocket notifications from a Python backend doesn't require running a separate WebSocket server or process. Apinator's Python server SDK lets you publish events from Django views, Flask routes, or FastAPI endpoints with a single API call. The persistent WebSocket connections live on Apinator's infrastructure — your Python application just triggers events when something happens.
Quick Start
How to add realtime WebSocket events to Django, Flask, or FastAPI
Add realtime features to your Python app in a few steps.
See your first event (no backend)
Before writing any Python: open apinator.io/try/?key=YOUR_APP_KEY&cluster=us, or run this snippet in any web page, then click the Send test event button on your app's Overview page in the dashboard. my-channel is a public channel, so nothing needs a server or an auth endpoint yet. Open the dashboard
// Any web page or frontend (npm install @apinator/client)
import { Apinator } from '@apinator/client'
const client = new Apinator({ appKey: 'YOUR_APP_KEY', cluster: 'us' }).connect()
client.subscribe('my-channel').bind('my-event', (data) => {
console.log('Received:', data)
}) Install the server SDK
Add the Python server SDK. Zero dependencies — uses only the standard library.
pip install apinator-server Initialize the client
Create an Apinator client with your app credentials. Works with any Python framework.
from apinator import Apinator
client = Apinator(
app_id="your-app-id",
key="your-key",
secret="your-secret",
cluster="us"
) Trigger WebSocket events from your views
Publish real-time WebSocket events from any Python backend — Django views, Flask routes, FastAPI endpoints, or Celery tasks.
client.trigger(
"new-order",
json.dumps({"total": "$49.99"}),
channel="private-orders"
) Authenticate private channels
Add an endpoint that authenticates channel subscriptions for your frontend clients.
auth = client.authenticate_channel(
socket_id, channel_name
)
# Returns {"auth": "key:signature"} Why Python
Built for Python developers
Zero Dependencies
Uses only Python stdlib — hashlib, hmac, urllib, json. No requests, no httpx, no bloat.
Works Everywhere
Django, Flask, FastAPI, Tornado, or plain scripts. Any Python 3.10+ environment.
Type Hints
Full type annotations throughout the SDK, picked up by Pyright and editor autocomplete.
Channel Introspection
List active channels and read subscriber counts with get_channels() and get_channel() for dashboards and monitoring.
How to add WebSocket notifications to a Python backend
Python web frameworks like Django, Flask, and FastAPI are built around the request/response cycle — they don't maintain persistent connections out of the box. Adding WebSocket support traditionally required integrating additional libraries (Django Channels, python-socketio, websockets) and running separate worker processes alongside your existing web server, each with their own configuration and operational overhead.
Apinator takes a different approach. Your Python backend doesn't manage any WebSocket connections. Instead, you make a simple HTTPS API call using Apinator's SDK to publish an event — from a Django view, a Celery task, a FastAPI background task, or any other Python code. Apinator's servers maintain the persistent WebSocket connections to your browser clients and deliver the event in real-time, typically in under 100ms.
The Python SDK has zero dependencies. It uses only Python's standard library — hmac, hashlib, urllib.request — to sign requests and publish events. This means no additions to requirements.txt, no dependency conflicts, and no compatibility issues across Python 3.10+ environments regardless of the framework.
Full Example
Full Example: Realtime WebSocket Dashboard with Django
Complete WebSocket example: Django views for channel authentication and event publishing, plus a JavaScript frontend that receives real-time updates.
import json
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from apinator import Apinator
from django.conf import settings
client = Apinator(
app_id=settings.REALTIME_APP_ID,
key=settings.REALTIME_KEY,
secret=settings.REALTIME_SECRET,
cluster=settings.REALTIME_CLUSTER,
)
@csrf_exempt
@require_POST
def realtime_auth(request):
body = json.loads(request.body)
auth = client.authenticate_channel(
body["socket_id"],
body["channel_name"]
)
return JsonResponse(auth)
@require_POST
def send_update(request):
data = json.loads(request.body)
client.trigger(
"dashboard-update",
json.dumps(data),
channel="private-dashboard"
)
return JsonResponse({"ok": True})Related Docs
Read the core WebSocket guides
Related Use Cases
Explore realtime use cases
From the blog
Building a Realtime Dashboard with Apinator
Learn how to build a live analytics dashboard that updates in real time using Apinator WebSockets, with less than 20 lines of client code.
What Is a Pub/Sub Channel?
Pub/sub channels are the core abstraction in realtime systems — they let publishers send messages to many subscribers without knowing who's listening. Here's how they work.
WebSocket Protocol Explained
A practical guide to the WebSocket protocol — how connections are established, how frames are structured, what opcodes do, and what all of this means when you're building realtime apps.
Start building realtime Python apps
Completely free. No credit card. No catch.