websocketgraphqlfastapiuvicornariadne-graphql

GraphQL subscription from multiple browsers/tabs


I have an react frontend and a python backend (using ariadne==0.13.0, uvicorn==0.15.0, uvicorn[standard]==0.15.0, fastapi==0.68.1) communicationg over graphql subscriptions. Everything works fine as long as I do not reload the page or load the page in a new browser window from same IP. Then the page crashes and takes some time to recover - Depending on the websocket timeout configured in uvicorn. I am experiencing the same issue with both my frontend and the graphql playgorund. I understand that the different browsers or tabs are identified with the same IP, Port and protocol what possibly messes up the existing connection, but still it should be possible the use the page from different tabs as seen in: https://fastapi.tiangolo.com/advanced/websockets/

My code:

SCHEMA = load_schema_from_path("schema.graphql")
query = QueryType()
subscription = SubscriptionType()
app = FastAPI()
schema = make_executable_schema(SCHEMA, [query, subscription])
graphql_server = GraphQL(schema, debug=True)
app.add_route("/graphql", graphql_server)
app.add_websocket_route("/graphql", graphql_server)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["POST", "GET"],
    allow_headers=[
        "accept",
        "accept-language",
        "content-language",
        "content-type",
        "x-apollo-tracing",
    ],
)
app.debug = True
uvicorn.run(app, host='0.0.0.0', port=7996)

Solution

  • The default setup for uvicorn is single threaded and the method implementing the subscription was synchronous and blocking. I had to reimplement it in an async manner.