View : 253

06/05/2026 08:38am

EP.85 Building a WebSocket Server with GraphQL Subscriptions in Golang

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?

 

BenefitDescription
βœ… Send only the necessary dataClients can specify exactly which fields they want
πŸ”„ Real-time updatesReceive data immediately when changes happen
❌ No need for pollingAvoid calling the same REST API every few seconds
πŸ›  Flexible & maintainableSchema 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