{"id":373,"date":"2026-05-10T09:00:00","date_gmt":"2026-05-10T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/"},"modified":"2026-10-10T21:21:17","modified_gmt":"2026-10-10T15:51:17","slug":"go-websockets-chat","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/","title":{"rendered":"Go WebSockets: Build a Real-Time Chat Server"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go WebSockets let a server and a browser talk to each other in both directions over one open connection. In this post, you will learn how WebSockets work and when to use them. Then we will build a small chat server where every message reaches every connected user instantly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before you start, it helps to know two earlier lessons. You built handlers in <a href=\"https:\/\/smitabharat.com\/blog\/go-http-server-beginners\/\">the Go HTTP server tutorial<\/a>, and you protected shared data in <a href=\"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/\">the Go sync package lesson<\/a>. So we use both ideas again here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Are Go WebSockets?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In short, a WebSocket is a long-lived, two-way connection between a client and a server. Normal HTTP works like sending letters: the browser asks, the server answers, and the conversation ends. A WebSocket, however, works like a phone call. In contrast, once the line is open, both sides can speak at any time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, the Go standard library does not include a WebSocket server. So most Go WebSockets projects use a small, well-tested package. Specifically, this lesson uses <code>github.com\/coder\/websocket<\/code>, which follows Go&#8217;s own style and supports <code>context<\/code> everywhere.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go WebSockets Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Some apps need data the moment it changes. In that case, waiting for the user to refresh the page is not good enough. For example, here are typical cases.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Chat apps<\/strong>: when one user sends a message, it must appear for everyone right away.<\/li>\n\n\n\n<li><strong>Live dashboards<\/strong>: order counts or server health update every second, so the screen must refresh itself.<\/li>\n\n\n\n<li><strong>Multiplayer games<\/strong>: player moves travel both ways many times per second.<\/li>\n\n\n\n<li><strong>Collaborative editors<\/strong>: two people type in the same document at the same time.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In short, Go WebSockets fit any feature where both sides need to send messages whenever they like.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Go WebSockets Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First, every WebSocket starts life as a normal HTTP request. Then the browser sends a GET request with an <code>Upgrade: websocket<\/code> header. Then, if the server agrees, it replies with status <code>101 Switching Protocols<\/code>. From that moment on, the same TCP connection carries WebSocket frames instead of HTTP.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-1-1024x640.png\" alt=\"Go WebSockets diagram showing the HTTP upgrade, 101 response and two-way messages\" class=\"wp-image-346\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The client asks to upgrade a normal HTTP request, the server answers 101 Switching Protocols, and then both sides send messages freely.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">After the upgrade, each message travels inside a small frame. A frame says whether the data is text or binary, so JSON usually goes as text. Also, either side can send a close frame with a status code to end the call politely.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Upgrade Request and Response<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This table shows the key headers in the handshake. Luckily, you rarely write them yourself, because the package handles them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Header<\/th><th>Sent by<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>Upgrade: websocket<\/code><\/td><td>Client<\/td><td>Asks to switch protocols<\/td><\/tr><tr><td><code>Connection: Upgrade<\/code><\/td><td>Client<\/td><td>Says this connection should change<\/td><\/tr><tr><td><code>Sec-WebSocket-Key<\/code><\/td><td>Client<\/td><td>A random value for the handshake<\/td><\/tr><tr><td><code>Sec-WebSocket-Version: 13<\/code><\/td><td>Client<\/td><td>The protocol version<\/td><\/tr><tr><td><code>Sec-WebSocket-Accept<\/code><\/td><td>Server<\/td><td>Proves the server understood the key<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Key Parts of a Go WebSockets Server<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In practice, a Go WebSockets server has a few clear jobs. So once you see them, the code becomes easy to follow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Accepting the Connection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To begin with, <code>websocket.Accept<\/code> upgrades the HTTP request and returns a <code>*websocket.Conn<\/code>. By default, it also checks the <code>Origin<\/code> header. As a result, other websites cannot open a socket to your server from a visitor&#8217;s browser.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reading Messages<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, <code>c.Read(ctx)<\/code> blocks until the next message arrives. Then it returns the message type, the bytes and an error. When the browser closes the tab, <code>Read<\/code> returns an error, and your loop ends.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Writing Messages<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, <code>c.Write(ctx, websocket.MessageText, data)<\/code> sends one message. Because it takes a context, you can give every write a timeout. So one slow client cannot freeze your server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Closing Cleanly<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>c.Close(websocket.StatusNormalClosure, \"bye\")<\/code> sends a close frame and waits briefly for the other side. On the other hand, <code>c.CloseNow()<\/code> simply drops the connection. For this reason, a common pattern is <code>defer c.CloseNow()<\/code> as a safety net.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go WebSockets vs Polling vs Server-Sent Events<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In fact, there are three common ways to get live data into a browser. For instance, the table below compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>WebSockets<\/th><th>HTTP polling<\/th><th>Server-Sent Events<\/th><\/tr><\/thead><tbody><tr><td>Direction<\/td><td>Both ways<\/td><td>Client asks again and again<\/td><td>Server to client only<\/td><\/tr><tr><td>Connection<\/td><td>One long-lived<\/td><td>Many short requests<\/td><td>One long-lived<\/td><\/tr><tr><td>Delay<\/td><td>Instant<\/td><td>Up to the poll interval<\/td><td>Instant<\/td><\/tr><tr><td>Server cost<\/td><td>Low per message<\/td><td>High, many wasted requests<\/td><td>Low per message<\/td><\/tr><tr><td>Works through plain HTTP tools<\/td><td>Needs upgrade support<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Best for<\/td><td>Chat, games, editors<\/td><td>Rare, simple checks<\/td><td>Feeds, notifications<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So when should you use which? Choose WebSockets when the browser must also send messages often. If only the server talks, Server-Sent Events are simpler, and we cover them in the next post.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hands-On: Build a Go WebSockets Chat Server<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s build a chat room with Go WebSockets. Specifically, every message from any user goes to all connected users, including the sender.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create the Project<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, create a module and add the package. Then the <code>go get<\/code> command records the version in <code>go.mod<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir chat &amp;&amp; cd chat\ngo mod init example.com\/chat\ngo get github.com\/coder\/websocket<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: The Hub That Tracks Connections<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the hub keeps a set of open connections. Many goroutines touch it at once, so a mutex protects the map.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Hub struct {\n\tmu    sync.Mutex\n\tconns map[*websocket.Conn]struct{}\n}\n\nfunc NewHub() *Hub {\n\treturn &amp;Hub{conns: make(map[*websocket.Conn]struct{})}\n}\n\nfunc (h *Hub) add(c *websocket.Conn) {\n\th.mu.Lock()\n\th.conns[c] = struct{}{}\n\th.mu.Unlock()\n}\n\nfunc (h *Hub) remove(c *websocket.Conn) {\n\th.mu.Lock()\n\tdelete(h.conns, c)\n\th.mu.Unlock()\n}\n\nfunc (h *Hub) broadcast(ctx context.Context, msg []byte) {\n\th.mu.Lock()\n\tdefer h.mu.Unlock()\n\tfor c := range h.conns {\n\t\twctx, cancel := context.WithTimeout(ctx, 5*time.Second)\n\t\terr := c.Write(wctx, websocket.MessageText, msg)\n\t\tcancel()\n\t\tif err != nil {\n\t\t\tc.CloseNow()\n\t\t\tdelete(h.conns, c)\n\t\t}\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Notice that the map value is an empty struct, because we only care about the keys. Also, each write gets its own five second timeout. Then, if a write fails, the hub drops that connection and moves on.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: The WebSocket Handler<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This handler upgrades the request and registers the connection. After that, it reads messages until the client leaves.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (h *Hub) serveWS(w http.ResponseWriter, r *http.Request) {\n\tc, err := websocket.Accept(w, r, nil)\n\tif err != nil {\n\t\tlog.Println(\"accept:\", err)\n\t\treturn\n\t}\n\tdefer c.CloseNow()\n\n\th.add(c)\n\tdefer h.remove(c)\n\tlog.Println(\"client connected\")\n\n\tctx := r.Context()\n\tfor {\n\t\t_, data, err := c.Read(ctx)\n\t\tif err != nil {\n\t\t\tlog.Println(\"client left:\", websocket.CloseStatus(err))\n\t\t\treturn\n\t\t}\n\t\th.broadcast(ctx, data)\n\t}\n}\n\nfunc main() {\n\thub := NewHub()\n\thttp.Handle(\"\/\", http.FileServer(http.Dir(\"static\")))\n\thttp.HandleFunc(\"\/ws\", hub.serveWS)\n\tlog.Println(\"chat server on http:\/\/localhost:8080\")\n\tlog.Fatal(http.ListenAndServe(\":8080\", nil))\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As with any HTTP handler, each browser tab gets its own goroutine running <code>serveWS<\/code>. Also, the two <code>defer<\/code> lines run when the loop ends, so the hub always forgets closed connections. <code>websocket.CloseStatus<\/code> returns the close code, or <code>-1<\/code> when the error was not a close frame.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-2-1024x640.png\" alt=\"Go WebSockets diagram showing a chat hub broadcasting messages to three browser tabs\" class=\"wp-image-347\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">One goroutine reads from each client, and the hub broadcasts every message to all open connections.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: A Tiny Browser Page<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, save this file as <code>static\/index.html<\/code>. Specifically, it opens a socket and prints incoming messages. Also, it sends whatever you type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input id=\"msg\" placeholder=\"Type a message\"&gt;\n&lt;button onclick=\"send()\"&gt;Send&lt;\/button&gt;\n&lt;ul id=\"log\"&gt;&lt;\/ul&gt;\n&lt;script&gt;\n  const ws = new WebSocket(\"ws:\/\/\" + location.host + \"\/ws\");\n  ws.onmessage = (e) =&gt; {\n    const li = document.createElement(\"li\");\n    li.textContent = e.data;\n    document.getElementById(\"log\").appendChild(li);\n  };\n  function send() {\n    const box = document.getElementById(\"msg\");\n    ws.send(box.value);\n    box.value = \"\";\n  }\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then open <code>http:\/\/localhost:8080<\/code> in two browser tabs. When you type &#8220;Hello from Asha&#8221; in one tab, both tabs show the message at once.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: A Go Client for Testing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Besides the browser, you can also talk to the server from Go. Indeed, this is handy for tests and command line tools.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func main() {\n\tctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\n\tdefer cancel()\n\n\tc, _, err := websocket.Dial(ctx, \"ws:\/\/localhost:8080\/ws\", nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer c.Close(websocket.StatusNormalClosure, \"bye\")\n\n\tif err := c.Write(ctx, websocket.MessageText, []byte(\"Hello from Asha\")); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t_, msg, err := c.Read(ctx)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Println(\"Received:\", string(msg))\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Because the hub also sends messages back to the sender, the client reads its own message. So with the server running, you will see this output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Received: Hello from Asha<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Meanwhile, the server log shows <code>client connected<\/code> and, after the client closes, <code>client left: StatusNormalClosure<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Scaling Go WebSockets to Many Servers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">However, our hub lives in the memory of one Go process. So if you run two servers behind a load balancer, users on server A never see messages from server B. This is the most common surprise when a chat app grows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead, the usual fix is a shared message bus. For example, each server publishes incoming chat messages to a channel in NATS or Redis. Then every server subscribes to that channel and forwards messages to its own local clients. As a result, all users see all messages, no matter which server they joined. We will build exactly this kind of messaging later in this series.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, load balancers must support the WebSocket upgrade and long-lived connections. Most modern ones do, but check their idle timeout. Otherwise, quiet connections may close after a minute.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Go WebSockets<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Sharing a map without a lock<\/strong>: two goroutines change <code>conns<\/code> together and the program crashes. So always guard shared state with a mutex.<\/li>\n\n\n\n<li><strong>Writing with no timeout<\/strong>: one frozen browser blocks every broadcast. Instead, give each write a context deadline.<\/li>\n\n\n\n<li><strong>Turning off the origin check<\/strong>: <code>InsecureSkipVerify<\/code> lets any website use your socket. Instead, use <code>OriginPatterns<\/code> for the domains you trust.<\/li>\n\n\n\n<li><strong>Forgetting the read limit<\/strong>: by default, reads are limited to 32 KB. Call <code>c.SetReadLimit<\/code> only when you truly need bigger messages.<\/li>\n\n\n\n<li><strong>Using plain ws in production<\/strong>: data travels as plain text. Serve the site over HTTPS, so the browser uses <code>wss:\/\/<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go WebSockets<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Keep one reading goroutine per connection<\/strong>, because the package expects a single reader.<\/li>\n\n\n\n<li>**Send JSON messages with a <code>type<\/code> field**, so the client can tell chat messages from system events.<\/li>\n\n\n\n<li><strong>Ping idle connections<\/strong>, since proxies often close sockets that stay silent for a minute.<\/li>\n\n\n\n<li><strong>Limit message size<\/strong>, because huge messages can exhaust server memory.<\/li>\n\n\n\n<li><strong>Plan for reconnects in the browser<\/strong>, because mobile networks drop connections often.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To practise Go WebSockets, add a username to every message. Send JSON like <code>{\"user\":\"Asha\",\"text\":\"Hi\"}<\/code> from the browser, decode it in Go and broadcast &#8220;Asha: Hi&#8221; to everyone.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hint: the <code>wsjson<\/code> package inside <code>coder\/websocket<\/code> reads and writes JSON values directly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These pages give the exact API details and the protocol rules, so check them when you need a specific option.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/github.com\/coder\/websocket\">coder\/websocket package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/WebSockets_API\">MDN: The WebSocket API<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/net\/http\">Go net\/http package documentation<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go WebSockets keep one connection open for two-way messages.<\/li>\n\n\n\n<li>First of all, every socket starts as an HTTP request that upgrades with status 101.<\/li>\n\n\n\n<li><code>websocket.Accept<\/code> upgrades, <code>Read<\/code> receives and <code>Write<\/code> sends.<\/li>\n\n\n\n<li>Meanwhile, a mutex-protected hub tracks connections and broadcasts messages.<\/li>\n\n\n\n<li>Give each write a timeout and keep the default origin check.<\/li>\n\n\n\n<li>Finally, use WebSockets for two-way features and SSE for one-way feeds.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go WebSockets are powerful. However, many features only need the server to push updates. Next, we will look at a simpler tool for that job: Server-Sent Events.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next lesson: <a href=\"https:\/\/smitabharat.com\/blog\/go-server-sent-events\/\">Go Server-Sent Events: Live Updates Over HTTP<\/a><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Go WebSockets let a server and a browser talk to each other in both directions over one open connection. In this post, you will&hellip;<\/p>\n","protected":false},"author":1,"featured_media":345,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[15],"tags":[11,23,67,21],"swapz_series":[],"class_list":["post-373","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-lang","tag-go-lang","tag-go-tutorial","tag-go-websockets","tag-golang-basics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go WebSockets: Build a Real-Time Chat Server<\/title>\n<meta name=\"description\" content=\"Learn Go WebSockets step by step: how the upgrade works, build a chat server with a hub, broadcast messages and avoid common mistakes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go WebSockets: Build a Real-Time Chat Server\" \/>\n<meta property=\"og:description\" content=\"Learn Go WebSockets step by step: how the upgrade works, build a chat server with a hub, broadcast messages and avoid common mistakes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-05-10T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-10T15:51:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go WebSockets: Build a Real-Time Chat Server\",\"datePublished\":\"2026-05-10T03:30:00+00:00\",\"dateModified\":\"2026-10-10T15:51:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/\"},\"wordCount\":1531,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-websockets-chat-featured.png\",\"keywords\":[\"Go Lang\",\"Go Tutorial\",\"Go WebSockets\",\"Golang Basics\"],\"articleSection\":[\"Go Lang\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/\",\"name\":\"Go WebSockets: Build a Real-Time Chat Server\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-websockets-chat-featured.png\",\"datePublished\":\"2026-05-10T03:30:00+00:00\",\"dateModified\":\"2026-10-10T15:51:17+00:00\",\"description\":\"Learn Go WebSockets step by step: how the upgrade works, build a chat server with a hub, broadcast messages and avoid common mistakes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-websockets-chat-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-websockets-chat-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go WebSockets featured image: cards for the 101 upgrade, the hub and broadcasting\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-websockets-chat\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go WebSockets: Build a Real-Time Chat Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go WebSockets: Build a Real-Time Chat Server","description":"Learn Go WebSockets step by step: how the upgrade works, build a chat server with a hub, broadcast messages and avoid common mistakes.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/","og_locale":"en_US","og_type":"article","og_title":"Go WebSockets: Build a Real-Time Chat Server","og_description":"Learn Go WebSockets step by step: how the upgrade works, build a chat server with a hub, broadcast messages and avoid common mistakes.","og_url":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-05-10T03:30:00+00:00","article_modified_time":"2026-10-10T15:51:17+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go WebSockets: Build a Real-Time Chat Server","datePublished":"2026-05-10T03:30:00+00:00","dateModified":"2026-10-10T15:51:17+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/"},"wordCount":1531,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-featured.png","keywords":["Go Lang","Go Tutorial","Go WebSockets","Golang Basics"],"articleSection":["Go Lang"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/","url":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/","name":"Go WebSockets: Build a Real-Time Chat Server","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-featured.png","datePublished":"2026-05-10T03:30:00+00:00","dateModified":"2026-10-10T15:51:17+00:00","description":"Learn Go WebSockets step by step: how the upgrade works, build a chat server with a hub, broadcast messages and avoid common mistakes.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-websockets-chat\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-websockets-chat-featured.png","width":2400,"height":1260,"caption":"Go WebSockets featured image: cards for the 101 upgrade, the hub and broadcasting"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-websockets-chat\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go WebSockets: Build a Real-Time Chat Server"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=373"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/373\/revisions"}],"predecessor-version":[{"id":383,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/373\/revisions\/383"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/345"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=373"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}