Gorilla WebSocket Library for Go (Golang): Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Gorilla WebSocket: a powerful tool for real-time in Go



In modern web development, the need for real-time data exchange is becoming increasingly relevant. Whether it's chats, online games, server monitoring, or stock quotes — WebSocket is the de facto standard for two-way communication between a client and a server. For the Go language (Golang), one of the most popular and stable libraries for working with WebSocket is Gorilla WebSocket.



The gorilla/websocket library is part of the Gorilla Toolkit ecosystem — a set of high-quality packages for web development in Go. It provides a full set of functions for creating WebSocket servers and clients, supports all major protocols (RFC 6455), and is easy to use.



Key Features of Gorilla WebSocket



  • Full WebSocket protocol support — working with text and binary frames, message fragmentation, control frames (ping/pong).
  • Convenient API — a minimalist interface with ReadMessage and WriteMessage functions.
  • TLS/SSL support — secure WebSocket connections (wss://).
  • Flexible configuration — ability to set timeouts, buffer sizes, and check request origins.
  • HTTP compatibility — easily integrates with any Go HTTP server (net/http).
  • Context support — support for context.Context for canceling operations.


Installing the library



To install Gorilla WebSocket, run the following command in your terminal:



go get github.com/gorilla/websocket


After that, add the import to your Go file:



import "github.com/gorilla/websocket"


Basic WebSocket server example



Let's look at a simple echo server that accepts WebSocket connections and sends back the received messages.



package main

import ( "log" "net/http"

"github.com/gorilla/websocket")

var upgrader = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true // Allow all origins (for development) },}

func echoHandler(w http.ResponseWriter, r *http.Request) { conn, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("Upgrade failed:", err) return } defer conn.Close()

for { messageType, message, err := conn.ReadMessage() if err != nil { log.Println("Read error:", err) break } log.Printf("Received: %s", message)

if err := conn.WriteMessage(messageType, message); err != nil { log.Println("Write error:", err) break } }}

func main() { http.HandleFunc("/echo", echoHandler) log.Println("Server started on :8080") log.Fatal(http.ListenAndServe(":8080", nil))}


Detailed code explanation



  1. Upgrader — this is a structure that converts a regular HTTP request into a WebSocket connection. In the example, we allow requests from any origin (the CheckOrigin field). In production, you should check specific domains.
  2. Upgrade — the main method that takes http.ResponseWriter and *http.Request and returns *websocket.Conn.
  3. ReadMessage — a blocking call that waits for a new message from the client. It returns the message type (text or binary data) and the data itself.
  4. WriteMessage — sends a message back to the client. In this case, we simply return the same thing we received.


WebSocket client (Gorilla)



The library also allows

Recommendations