This application shows how to use the websocket package to implement a simple web chat application.

Chat Example

This application shows how to use the websocket package to implement a simple web chat application.

Running the example

The example requires a working Go development environment. The Getting Started page describes how to install the development environment.

Once you have Go up and running, you can download, build and run the example using the following commands.

$ go get github.com/gorilla/websocket
$ cd `go list -f '{{.Dir}}' github.com/gorilla/websocket/examples/chat`
$ go run *.go

To use the chat example, open http://localhost:8080/ in your browser.

Server

The server application defines two types, Client and Hub. The server creates an instance of the Client type for each websocket connection. A Client acts as an intermediary between the websocket connection and a single instance of the Hub type. The Hub maintains a set of registered clients and broadcasts messages to the clients.

The application runs one goroutine for the Hub and two goroutines for each Client. The goroutines communicate with each other using channels. The Hub has channels for registering clients, unregistering clients and broadcasting messages. A Client has a buffered channel of outbound messages. One of the client's goroutines reads messages from this channel and writes the messages to the websocket. The other client goroutine reads messages from the websocket and sends them to the hub.

Hub

The code for the Hub type is in hub.go. The application's main function starts the hub's run method as a goroutine. Clients send requests to the hub using the register, unregister and broadcast channels.

The hub registers clients by adding the client pointer as a key in the clients map. The map value is always true.

The unregister code is a little more complicated. In addition to deleting the client pointer from the clients map, the hub closes the clients's send channel to signal the client that no more messages will be sent to the client.

The hub handles messages by looping over the registered clients and sending the message to the client's send channel. If the client's send buffer is full, then the hub assumes that the client is dead or stuck. In this case, the hub unregisters the client and closes the websocket.

Client

The code for the Client type is in client.go.

The serveWs function is registered by the application's main function as an HTTP handler. The handler upgrades the HTTP connection to the WebSocket protocol, creates a client, registers the client with the hub and schedules the client to be unregistered using a defer statement.

Next, the HTTP handler starts the client's writePump method as a goroutine. This method transfers messages from the client's send channel to the websocket connection. The writer method exits when the channel is closed by the hub or there's an error writing to the websocket connection.

Finally, the HTTP handler calls the client's readPump method. This method transfers inbound messages from the websocket to the hub.

WebSocket connections support one concurrent reader and one concurrent writer. The application ensures that these concurrency requirements are met by executing all reads from the readPump goroutine and all writes from the writePump goroutine.

To improve efficiency under high load, the writePump function coalesces pending chat messages in the send channel to a single WebSocket message. This reduces the number of system calls and the amount of data sent over the network.

Frontend

The frontend code is in home.html.

On document load, the script checks for websocket functionality in the browser. If websocket functionality is available, then the script opens a connection to the server and registers a callback to handle messages from the server. The callback appends the message to the chat log using the appendLog function.

To allow the user to manually scroll through the chat log without interruption from new messages, the appendLog function checks the scroll position before adding new content. If the chat log is scrolled to the bottom, then the function scrolls new content into view after adding the content. Otherwise, the scroll position is not changed.

The form handler writes the user input to the websocket and clears the input field.

Similar Resources

A simple MVC chat service.

A simple MVC chat service Installation 1. You need a go development environment setup before everything starts taking off. 2. Use git clone to clone t

Dec 7, 2022

A fast, well-tested and widely used WebSocket implementation for Go.

Gorilla WebSocket Gorilla WebSocket is a Go implementation of the WebSocket protocol. Documentation API Reference Chat example Command example Client

Jan 2, 2023

Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.

websocketd websocketd is a small command-line tool that will wrap an existing command-line interface program, and allow it to be accessed via a WebSoc

Dec 31, 2022

WebSocket Command Line Client written in Go

ws-cli WebSocket Command Line Client written in Go Installation go get github.com/kseo/ws-cli Usage $ ws-cli -url ws://echo.websocket.org connected (

Nov 12, 2021

proxy your traffic through CDN using websocket

go-cdn2proxy proxy your traffic through CDN using websocket what does it do example server client thanks what does it do you can use this as a library

Dec 7, 2022

Tiny WebSocket library for Go.

RFC6455 WebSocket implementation in Go.

Dec 28, 2022

Minimal and idiomatic WebSocket library for Go

websocket websocket is a minimal and idiomatic WebSocket library for Go. Install go get nhooyr.io/websocket Highlights Minimal and idiomatic API First

Dec 31, 2022

:notes: Minimalist websocket framework for Go

:notes: Minimalist websocket framework for Go

melody 🎶 Minimalist websocket framework for Go. Melody is websocket framework based on github.com/gorilla/websocket that abstracts away the tedious p

Dec 23, 2022

A modern, fast and scalable websocket framework with elegant API written in Go

A modern, fast and scalable websocket framework with elegant API written in Go

About neffos Neffos is a cross-platform real-time framework with expressive, elegant API written in Go. Neffos takes the pain out of development by ea

Dec 29, 2022
Related tags
Encrypted-websocket-chat - Encrypted websocket chat using golang

Encrypted websocket chat First version written in python This version should be

Sep 15, 2022
simpleChatInGo - This is a simple chat that i made for fun asnd learn more about websocket
simpleChatInGo - This is a simple chat that i made for fun asnd learn more about websocket

simpleChatInGo This is a simple chat that i made for fun asnd learn more about websocket deploy For deploy this you only need to run the command : $ d

Sep 21, 2022
Simple Chat App built with Go, Websocket, etc

Go - Simple Chat Usage: 1. Clone Repository git clone https://github.com/aesuhaendi/go-simple-chat.git 2. Running HTTP Server go run . If you are us

Oct 4, 2021
Awesome WebSocket CLient - an interactive command line client for testing websocket servers
Awesome WebSocket CLient - an interactive command line client for testing websocket servers

Awesome WebSocket CLient - an interactive command line client for testing websocket servers

Dec 30, 2022
Chat bots (& more) for Zoom by figuring out their websocket protocol
Chat bots (& more) for Zoom by figuring out their websocket protocol

zoomer - Bot library for Zoom meetings Good bot support is part of what makes Discord so nice to use. Unfortunately, the official Zoom API is basicall

Dec 14, 2022
Simle websocket chat on Golang

WebsocketChat Simle websocket chat on Golang Installation (with comiling binary files): cd projectDir/cmd/app/server - change current directory go bui

Nov 1, 2021
API that upgrades connection to use websocket. Contains server and client and testing how they communicate

Websocket Test API How to execute First run server using: make run-server. Then run many client instances with: make run-client. Then start typing in

Dec 25, 2021
Go-distributed-websocket - Distributed Web Socket with Golang and Redis
Go-distributed-websocket - Distributed Web Socket with Golang and Redis

go-distributed-websocket Distributed Web Socket with Golang and Redis Dependenci

Oct 13, 2022
Simple example for using Turbos Streams in Go with the Gorilla WebSocket toolkit.

Go Example for TurboStreams over WebSockets Simple example for using Turbos Streams in Go with the Gorilla WebSocket toolkit.

Dec 22, 2022
A simple server to convert ATK-IMU901 serial data into websocket stream

A simple server to convert ATK-IMU901 serial data into WebSocket stream.

Jan 31, 2022