Go Gorilla WebSocket
last modified April 11, 2024
In this article we show how to work with websockets using Gorilla WebSocket package.
$ go version go version go1.22.2 linux/amd64
We use Go version 1.22.2.
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.
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.
<!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.
Source
Gorilla Websocket - Github page
In this article we have worked with websockets using Gorilla WebSocket package.
Author
List all Go tutorials.