Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to use python-socker.io with fast api

# server.py
from typing import Any

import uvicorn
from fastapi import FastAPI

import socketio

sio: Any = socketio.AsyncServer(async_mode="asgi")
socket_app = socketio.ASGIApp(sio)
app = FastAPI()


@app.get("/test")
async def test():
    print("test")
    return "WORKS"


app.mount("/", socket_app)  # Here we mount socket app to main fastapi app


@sio.on("connect")
async def connect(sid, env):
    print("on connect")


@sio.on("direct")
async def direct(sid, msg):
    print(f"direct {msg}")
    await sio.emit("event_name", msg, room=sid)  # we can send message to specific sid


@sio.on("broadcast")
async def broadcast(sid, msg):
    print(f"broadcast {msg}")
    await sio.emit("event_name", msg)  # or send to everyone


@sio.on("disconnect")
async def disconnect(sid):
    print("on disconnect")


if __name__ == "__main__":
    kwargs = {"host": "0.0.0.0", "port": 5000}
    kwargs.update({"debug": True, "reload": True})
    uvicorn.run("server:app", **kwargs)
Comment

PREVIOUS NEXT
Code Example
Python :: def identity_block(X, f, filters, training=True, initializer=random_uniform): 
Python :: how to send more than one variables to python using xlwings 
Python :: Loop per n (batch) 
Python :: num1=int(self.t1.get()) 
Python :: python directed graph 
Python :: Replace u00a0 
Python :: pairwise swap in data structure in python 
Python :: how to reverse a dictionary in python 
Python :: python code to save data with multiple sheet in excel 
Python :: histogram plot seaborn 
Python :: python which packages depend on package 
Python :: python wait for executable to finish before perceeding 
Python :: binarizer pyspark 
Python :: min_max_scaler.fit_transform 
Python :: read file in python 
Python :: simplejwt in django setup 
Python :: xmlrpc get all posts 
Python :: print e 
Python :: django validate_comma_separated_integer_list 
Python :: Reset Python Dictionary to 0 Zero. Empty existing Python Dictionary 
Python :: jupyter notebook print string with variables 
Python :: pandas return indices that match 
Python :: create animation from sequence of image python 
Python :: how to add sum of range in python 
Python :: python structure like c 
Python :: how to resume request downloads 
Python :: pd datetime 
Python :: plotly colors 
Python :: python encryption program 
Python :: render() django 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =