06/05/2026 08:38am

EP.85 Building a WebSocket Server with GraphQL Subscriptions in Golang
#WebSocket
#Go
#Golang
#GraphQL Subscriptions
#Real-time Application
In this episode, weβll explore GraphQL Subscriptions β a modern approach to building real-time applications using WebSocket that offers flexible control over data structure and avoids the common issue of over-fetching data seen with traditional REST APIs.
Ideal use cases include:
- π’ Real-time Chat Systems
- π Notifications
- π Live Dashboards
π€ Why Use GraphQL Subscriptions?
| Benefit | Description |
|---|---|
| β Send only the necessary data | Clients can specify exactly which fields they want |
| π Real-time updates | Receive data immediately when changes happen |
| β No need for polling | Avoid calling the same REST API every few seconds |
| π Flexible & maintainable | Schema changes are easier to manage and deploy |
π§ͺ GraphQL Subscriptions with WebSocket in Go
Hereβs a basic server setup using gqlgen, a popular Go library for building GraphQL servers.
package main
import (
"log"
"net/http"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/yourusername/yourproject/graph"
"github.com/yourusername/yourproject/graph/generated"
)
func main() {
srv := handler.NewDefaultServer(
generated.NewExecutableSchema(
generated.Config{Resolvers: &graph.Resolver{}},
),
)
http.Handle("/", playground.Handler("GraphQL Playground", "/query"))
http.Handle("/query", srv)
log.Println("π Server running at http://localhost:8080/")
log.Fatal(http.ListenAndServe(":8080", nil))
}
β Note: This example uses gqlgen, one of the most efficient tools to create GraphQL servers in Go.
π¨ Sample Subscription Query
subscription {
messageAdded {
id
content
sender
}
}
Whenever a new message is added (e.g., a user sends a chat), all clients subscribed to messageAdded will receive the new data instantly via WebSocket.
βοΈ Connecting to WebSocket (Frontend with Apollo Client)
You can use tools like Apollo Client, which natively supports GraphQL subscriptions:
import { WebSocketLink } from "@apollo/client/link/ws";
import { ApolloClient, InMemoryCache } from "@apollo/client";
const wsLink = new WebSocketLink({
uri: `ws://localhost:8080/query`,
options: {
reconnect: true
}
});
const client = new ApolloClient({
link: wsLink,
cache: new InMemoryCache()
});
π‘ Apollo handles connection_init, ping/pong, and automatic reconnect out of the box.
π§ Summary
Using GraphQL Subscriptions with WebSocket is a powerful approach for building real-time applications with better control over data flow and structure. Benefits include:
- Fine-grained payload control
- No repetitive polling
- Easy schema adjustments
- Seamless integration with Go via
gqlgen
π Give it a Try!
Try applying this to build:
- β A real-time group chat
- β Order or system notifications
- β Live reporting dashboard
π Next EP
EP.86: Managing WebSocket Connections with Redis Pub/Sub
Weβll learn how to scale your WebSocket Server across multiple instances using Redis Pub/Sub to keep all users in sync β no matter which server they're connected to!
Read more
π΅ Facebook: Superdev School (Superdev)
πΈ Instagram: superdevschool
π¬ TikTok: superdevschool
π Website: www.superdev.school