Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

How to add middleware #1452

Unanswered
a1nsworth asked this question in Q&A
Apr 11, 2025 · 1 comments · 4 replies
Discussion options

I`m using lib dishka and it provide Di for application, so i need way to pass some value to environ.

For example, dishka for FastApi

def setup_dishka(container: AsyncContainer | Container, app: FastAPI) -> None:
    if isinstance(container, AsyncContainer):
        app.add_middleware(ContainerMiddleware)
    else:
        app.add_middleware(SyncContainerMiddleware)
    app.state.dishka_container = container

I`m passing container to app and to request fastapi

I need some ways like it

You must be logged in to vote

Replies: 1 comment · 4 replies

Comment options

I'm not familiar with dishka so I cannot give you specific instructions, but Socket.IO comes with an ASGIApp adapter:

import socketio

sio = socketio.AsyncServer(async_mode='asgi')
app = socketio.ASGIApp(sio)

If you have an ASGI middleware that does what you need then you can just connect it to app.

You must be logged in to vote
4 replies
@a1nsworth
Comment options

Okay, but FastAPI provides a Request object for each request. Will Socket.IO provide it? Because, i`ll retrieve request: Request from args

@miguelgrinberg
Comment options

Socket.IO is normally used with WebSocket, so there are no requests. If you have the client's sid you can obtain the environ dictionary that was used in the initial connection using the get_environ() method of the server.

@a1nsworth
Comment options

Is it possible to set global values to environ for each connection or for all connection ?

@miguelgrinberg
Comment options

As I said above, there is an adapter to make a Socket.IO server work as a ASGI application. You can modify incoming requests in a middleware if you like, but as I also mentioned, keep in mind Socket.IO uses WebSocket, so your middleware will have to be able to understand WebSocket in addition to HTTP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.