Websockets in Go with Gorilla Websocket


A websocket “upgrades” a standard HTTP connection into a persistent socket capable of two-way communication. Websockets save the overhead of opening a new HTTP connection between client and server for each communication, and allow servers to push updates to clients rather than relying on clients to poll the server for updates.

The Gorilla websocket library.

The Conn type represents a WebSocket connection. A server application calls the Upgrader.Upgrade method from an HTTP request handler to get a *Conn:

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

func handler(w http.ResponseWriter, r *http.Request) {
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {

    // … Use conn to send and receive messages.