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
ReadMessageandWriteMessagefunctions. - 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.Contextfor canceling operations.
Installing the library
To install Gorilla WebSocket, run the following command in your terminal:
go get github.com/gorilla/websocketAfter 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
- 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
CheckOriginfield). In production, you should check specific domains. - Upgrade — the main method that takes
http.ResponseWriterand*http.Requestand returns*websocket.Conn. - 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.
- 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
Also in library
Hilt/Dagger for Kotlin: Complete Guide with Examples
Machine Learning in Kotlin: Overview of Libraries for Data Science and ML
JSON Processing in Java: Jackson and Gson Libraries for Working with Data
Working with JWT Tokens in TypeScript: jsonwebtoken Library and Complete Guide