Urgent.News

What's breaking now, across hundreds of outlets.

Tech

GraphQL Subscriptions in Production: Real-Time Without the Tears

The first time I shipped a GraphQL subscriptions endpoint, it worked perfectly on localhost with one user. The second time, with two users on two different servers, nothing happened. I stared at the logs for an hour before realizing that the in-process pub/sub I'd cribbed from a tutorial didn't fan out across instances. Each server was happily publishing to itself and nobody else. That's GraphQL…

In this article, the author discusses the challenges and solutions for implementing GraphQL subscriptions in production environments. The key points include:

1. Initial difficulty in getting GraphQL subscriptions to work across multiple instances, as the in-process pub/sub used in tutorials doesn't fan out across different servers. Each server only publishes to itself, leading to no notifications for clients on other instances.

2. GraphQL subscriptions require a few additional considerations compared to other real-time update methods like polling, SSE, or plain WebSocket connections:

- They offer bidirectional communication between the client and server.

- They provide a single client library, type system, and auth story if you're already using GraphQL for your API.

- However, they add complexity in terms of WebSocket connections, auth over WebSocket, connection lifecycle, backpressure, and load balancer considerations.

- Subscriptions may be overkill if your real-time requirement is just a 30-second update window, as polling could be simpler, cheaper, and more robust.

3. The recommended architecture for implementing GraphQL subscriptions involves:

- Running a GraphQL server (e.g., Strawberry + FastAPI) that handles queries, mutations, and subscriptions.

- Using Redis for cross-instance pub/sub to enable notifications when mutations or background workers need to send updates to clients connected to different FastAPI instances.

- Frontend clients can use Apollo Client or urql with the graphql-ws package to connect to the WebSocket endpoint.

4. The author emphasizes the importance of understanding the underlying WebSocket protocol and the specific challenges it presents when scaling across multiple servers. Debugging tools like wscat can be helpful for troubleshooting WebSocket connections.

5. The article concludes with a project setup guide, including environment configuration, Redis client setup, and project layout for the FastAPI GraphQL server with subscriptions.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Before You Upscale Anything, Find Out Which of the Three Softnesses You Have

Every "my photo is blurry, should I upscale it" question hides three different problems, and an upscaler only fixes one of them.

  • Three types of softness affect images: uniform softness, blocky/ringing edges, and missing details
  • Determine softness by comparing upscale against JPEG-decoded source and PNG of same content

Screen the Hyperliquid leaderboard before you copy a trader, in Python

The Hyperliquid leaderboard is sorted by pnl, and pnl is the worst single number to pick a trader by. A wallet can sit at the top after one lucky month with a 60% drawdown on the way, or with every…

  • Hyperliquid leaderboard not sorted by profit alone
  • Python script filters top traders based on drawdown, win rate, holding duration, concentration
  • Filtered list reveals concise traders worth individual examination

More from Thursday 26 November →