0Pricing
Go Academy · Lesson

WebSocket Basics

Real-time bidirectional comms.

WebSocket Basics is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Real-Time, Two-Way Communication

HTTP is request-response: the client asks, the server answers, the connection (logically) ends. For chat, live dashboards, and games you need a persistent, bidirectional channel. That is what WebSockets provide.

The Upgrade Handshake

A WebSocket connection starts as a normal HTTP request with special headers:

  • Upgrade: websocket
  • Connection: Upgrade

The server responds 101 Switching Protocols, and the TCP socket is reused for full-duplex frames.

Why Not Just Polling

Before WebSockets, apps used polling (repeated requests) or long-polling. Both waste bandwidth and add latency. WebSockets keep one open connection, so either side can send instantly.

Frames, Not Requests

Once upgraded, data travels as lightweight frames rather than HTTP messages. Frames can be text or binary, and there are control frames for ping/pong and close.

Full Duplex

Unlike HTTP, both peers can send at any time without waiting for a reply. A server can push a notification the moment something changes, and the client can send input concurrently.

ws vs wss

Like http/https, WebSockets have two schemes:

  • ws:// plaintext
  • wss:// over TLS (use this in production)

Standard Library Support

Go's standard library has no WebSocket implementation in net/http alone. The community uses github.com/gorilla/websocket or nhooyr.io/websocket. We cover gorilla next lesson.

import "github.com/gorilla/websocket"

A Mental Model

Think of a WebSocket as a phone call:

  • Dialing = the HTTP upgrade handshake
  • The open line = the persistent connection
  • Talking both ways = full-duplex frames
  • Hanging up = the close frame

Message Types

Two data frame types matter most:

  • Text frames carry UTF-8 (often JSON)
  • Binary frames carry raw bytes (protobuf, images)

Control frames (ping, pong, close) manage the connection itself.

Where WebSockets Shine

Great fits:

  • Chat and presence
  • Live trading or sports scores
  • Collaborative editing
  • Multiplayer game state

For one-way server-to-client only, Server-Sent Events may be simpler.

Runnable: Framing the Idea

WebSocket code needs an external library, so here is a standard-library analogy: a function that classifies a message as text or binary, illustrating frame types.

package main

import (
    "fmt"
    "unicode/utf8"
)

func frameType(data []byte) string {
    if utf8.Valid(data) {
        return "text"
    }
    return "binary"
}

func main() {
    fmt.Println(frameType([]byte("hello")))
    fmt.Println(frameType([]byte{0xff, 0x00, 0x01}))
}

Quick Check

Test your understanding of WebSocket basics.

Recap

You learned WebSocket fundamentals:

  • They provide persistent, full-duplex communication
  • They begin with an HTTP upgrade handshake (101 response)
  • Data flows as text/binary frames; control frames manage the link
  • Use wss:// in production; Go relies on libraries like gorilla/websocket

Frequently asked questions

Is the “WebSocket Basics” lesson free?

Yes — the full text of “WebSocket Basics” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “WebSocket Basics”?

Real-time bidirectional comms. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “WebSocket Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. WebSocket Basics
  2. Using gorilla/websocket
  3. Broadcasting Messages
  4. Connection Management
← Back to Go Academy