ZetCode

Go Gorilla WebSocket

last modified August 24, 2023

Go Gorilla WebSocket tutorial shows how to work with websockets using Gorilla WebSocket package.

$ go version
go version go1.18.1 linux/amd64

We use Go version 1.18.

WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection. WebSockets are used in highly interactive applications such as games, chats, or stock markets.

The Gorilla WebSocket package provides a complete and tested implementation of the WebSocket protocol.

$ go get github.com/gorilla/websocket

This command installs the package.

Gorilla WebSocket example

In the example, we create a simple websocket server. The client will be a browser that connects to the server with JS code.

main.go
package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/gorilla/websocket"
)

var upgrader = websocket.Upgrader{
    ReadBufferSize:  1024,
    WriteBufferSize: 1024,
}

func main() {

    http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
        conn, err := upgrader.Upgrade(w, r, nil)

        if err != nil {
            log.Fatal(err)
        }

        for {

            msgType, msg, err := conn.ReadMessage()
            if err != nil {
                return
            }

            fmt.Printf("%s sent: %s\n", conn.RemoteAddr(), string(msg))

            if err = conn.WriteMessage(msgType, msg); err != nil {
                return
            }
        }
    })

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, "index.html")
    })

    log.Println("Listening...")
    log.Fatal(http.ListenAndServe(":8080", nil))
}

The program sets up the websocket endpoint and a static index.html page.

var upgrader = websocket.Upgrader{
    ReadBufferSize:  1024,
    WriteBufferSize: 1024,
}

We define buffers for the upgrader. It specifies parameters for upgrading an HTTP connection to a WebSocket connection.

http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {

We register a handler function for the /echo endpoint.

conn, err := upgrader.Upgrade(w, r, nil)

We upgrade the HTTP server connection to the WebSocket protocol.

for {

    msgType, msg, err := conn.ReadMessage()
    if err != nil {
        return
    }

    fmt.Printf("%s sent: %s\n", conn.RemoteAddr(), string(msg))

    if err = conn.WriteMessage(msgType, msg); err != nil {
        return
    }
}

We continually listen for any incoming messages sent through that WebSocket connection. We read the message from the client with ReadMessage. The WriteMessage writes the message back to the client.

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "index.html")
})

We set up the static HTML home page, from which we connect to the server.

The connection to the websocket endpoint is made from the browser.

index.html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <input id="input" type="text">
    <button onclick="send()">Send</button>
    <pre id="output"></pre>

    <script>
        var input = document.getElementById("input");
        var output = document.getElementById("output");
        var socket = new WebSocket("ws://localhost:8080/echo");

        socket.onopen = () => {
            output.innerHTML += "connected\n";
        };

        socket.onmessage = (e) => {
            output.innerHTML += `{e.data}\n`;
        };

        function send() {
            socket.send(input.value);
            input.value = "";
        }
    </script>
</body>

</html>

In JS, we use the WebSocket class provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.

In this article we have worked with websockets using Gorilla WebSocket package.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Go tutorials.