GB
  
You are currently viewing the United Kingdom version of the site.
Would you like to switch to your local site?
12 MIN READ TIME

WEBSOCKET

Create your first WebSocket service

Mihalis Tsoukalos explains how to use the Go programming language to work with the WebSocket protocol.

OUR EXPERT

Mihalis Tsoukalos is a systems engineer and a technical writer. He is the author of Go Systems Programming and Mastering Go. You can reach him at @mactsouk.

This tutorial covers the WebSocket protocol and how to work with it using the Go programming language. We’re going to develop a WebSocket client and a WebSocket server that can interact with each other. In the process, we are also going to learn the basics of Unix signal handling in Go and how to test a WebSocket server using the Websocat command line utility as well as JavaScript.

Go is an open source programming language and WebSocket is an open protocol. The same applies to HTML and JavaScript, as well as Linux. People make a living from open source projects, tools, technologies and ideas, so please recognise that and contribute in any way you can. Many thanks to all these people!

Gorilla sockets

To develop a small yet fully functional WebSocket server, we’re using the gorilla/websocket (https:// github.com/gorilla/websocket) module. The server implements the Echo service, which means that it automatically returns its input to the client. It also expects to get client input before sending data back.

Apart from gorilla/websocket, the golang.org/x/ net/websocket package offers another way of developing WebSocket clients and servers. However, according to its documentation, golang.org/x/net/websocket lacks some features and it is advised that you use https://godoc.org/github.com/gorilla/ websocket, the one used here, or https://godoc.org/nhooyr.io/websocket instead.

Here you can see the implementation of the wsHandler() function as well as the definition of the upgrader Go structure and the implementation of the rootHandler() handler function.

As gorilla/websocket is an external Go package and that by default all recent Go versions use modules, all source code should be put under ~/go/src to be compiled and executed using Go modules. If you have a Go project that uses gorilla/websocket and you want to download gorilla/websocket for the first time, you should execute go mod init and go mod tidy on each Go project directory that uses gorilla/websocket and Go takes care of the rest. Bear in mind that both commands should be executed only once to enable Go modules. If you want to start the process from scratch, you can delete go.mod and go.sum at any time and recreate them.

Unlock this article and much more with
You can enjoy:
Enjoy this edition in full
Instant access to 600+ titles
Thousands of back issues
No contract or commitment
Try for 99p
SUBSCRIBE NOW
30 day trial, then just £9.99 / month. Cancel anytime. New subscribers only.


Learn more
Pocketmags Plus
Pocketmags Plus

This article is from...


View Issues
Linux Format
April 2023
VIEW IN STORE

Other Articles in this Issue


LINUX FORMAT
SUBSCRIBE!
WELCOME
MEET THE TEAM
300 issues of Linux Format – can you believe it? What are your predictions for Linux or your favourite open source project over the next 300 issues?
Freed software
Saying “free software” has always caused confusion. Most
REGULARS AT A GLANCE
Newsdesk
THIS ISSUE: Li bre Office upgrade New Plasma due to hit screens Cool phase of Debian 12 Non-GNU Linux
POD BOD
Jon Masters is a kernel hacker who’s been
Kernel Watch
Jon Masters keeps an eye out for all the latest happenings in the Linux kernel, so you don’t have to.
Answers
Got a burning question about open source or the kernel? Whatever your level, email it to answers@linuxformat.com
Mailserver
WRITE TO US Do you have a burning
BACK ISSUES MISSED ONE?
ISSUE 299 March 2023 Product code: LXFDB0299 In
OpenShot
Version : 3.0.0 Web : www.openshot.org These
Rsync
Version : 3.2.7 Web : https://rsync.samba.org Linux
Aria2
Version : 1.36.0 Web: https://aria2.github.io No
Claws Mail
Version : 4.1.1 Web : www.claws-mail.org C
Akregator
Version : 5.22.1 Web : https://apps.kde.org/akregator If
Konqueror
Version : 22.12.1 Web : https://apps.kde.org/konqueror Unless
Pidgin
Version : 2.14.12 Web : https://pidgin.im A
FlightGear
Version : 2020.3.17 Web : www.flightgear.org F
SuperTuxKart
Version : 1.4 Web : https://supertuxkart.net As
Qemu
Version : 7.2.0 Web : www.qemu.org Q
CPU-X
Version : 4.5.2 Web : https://thetumultu ousunicornofdarkness.github.io/CPU-X
SAVE YOUR OLD PCS!
We help keep the lights on by running your old hardware for longer: easy upgrades and 32-bit installs galore!
LINUX FORMAT
The #1 open source mag Future Publishing Limited,
CLASSIC RETRO REVIEWS
REVIEWS
Corel Linux 1.0
Debian GNU/Linux 2.1
It may be a bit behind the times, but Debian is one of the best distributions on the market.
SUSE Linux 6.3
We test this long-established German company’s latest distribution effort. Can it hold off the competition?
Ubuntu 5.04
Andy Hudson hopes this is one hedgehog that won’t get squashed.
Linux-Mandrake 7
Simplicity and a wide range of applications make this a great distribution for all Linux users.
ROUNDUP
Key Linux distros
There are Linux distros aplenty, but Mayank Sharma is on the lookout for the ones that play a key role in the Linux-sphere.
Installation media and mechanisms
For anchoring them to your disks.
Customisability & administration
Mould them to your liking.
The end user experience
How does it all come together?
Reach and appeal
Who do they target?
Help and support
What do you do if you’re stuck?
Spins and derivatives
Can I pimp my ride?
Package management
Want to flesh out your installation?
The Verdict
Key Linux distros
ALSO CONSIDER
Two distros that have created an identity for
The Best OPEN SOURCE Software Ever!
In an attempt to trigger controversy, Michael Reed and Neil Mohr unequivocally state these are the greatest free software apps ever. Probably. We’re just trying to be helpful.
Top of the FOSS!
Some of the most notable projects in history.
THE OLDEST OF OPEN SOURCE…
Open a terminal and you have access to
GIMP 2.10.32
If you want to work with image processing, GIMP should be your first port of call.
Ardour 7.2
It’s the most fully featured open source digital audio workstation (DAW) on Linux.
Wireshark 4.0.2
Makes it easy to see the sensitive data travelling around your network.
LibreOffice 7.5
When it comes to traditional office duties, LibreOffice covers all bases.
Visual Studio Code 1.74
When it comes to open source development tools, the first thing you need is a programmer’s editor.
Kdenlive 22.12.1
Kdenlive can handle most video-editing tasks, and new features are being added all the time.
Dolphin 22.12
With a bit of configuration, Dolphin can scale up to a power user’s tool.
Cockpit 283
Monitor a server and do maintenance via a web interface? Yes please!
Terminal tools
Think GUIs are for wimps? More of a terminal type? Try these…
Pi USER
I’m so cool I run Raspberry Pi
The Coolest Projects showcase is up and running, and open to entries.
No more blues
Perk up your APIs.
Compute this!
Rack-mountable trouble.
THE RE TURN OF CAMJAM!
Brian Corteil is a maker, amateur robot designer
Waveshare Zero to Pi3
Transform your Pi Zero into a Pi 3, they promised Les Pounder, but it’s more like adding on go-faster stripes.
Master RPi.GPIO
Les Pounder goes back to the early days of the Raspberry Pi – and his career – with this classic library!
Control your Pi robot with Bluetooth
Loving the feeling of power, Les Pounder shows us how to remote-control your robot with Android devices and Bluetooth.
Free our SOFT WARE!
Taking anything for granted is dangerous, so Jonni Bidwell and Mike Saunders revisit how the free software movement got started to help free us from proprietary tyranny!
TOP OF THE FOSS!
In 2011, boy genius Mike Saunders met Stallman at the Institute of Engineering and Technology in London, where he put to him some of the questions you’d asked…
RMS: First, I want to tell you about
Find it faster!
Traversing hundreds of GBs of data and thousands of folders is a tall order, unless you’re Shashank Sharma, who knows just the tool to find files fast.
TUTORIALS
Jump to a federated social network
Nick Peers reveals how you can get up and running with this free, decentralised and non-profit alternative to Twitter.
Runing with the GNU Debian Hurd
Matt Holder goes against the Hurd to try the other GNU kernel and a ready-built operating system to run it all.
Universal layer text effects with GIMP
Posters use them, films and presentations are hard to imagine without them: text effects. Attract attention with Karsten Günther and GIMP.
Set up your terminal and email like it’s 1983
Jump in the hot terminal time machine with Mats Tage Axelsson who emails from the command line using the latest technology.
Clang-ing the kernel
Jonni Bidwell shows you how to configure a mainline kernel, then compile it, artisan-style, with LLVM/Clang.
IN DEPTH
Fantastic Mr Firefox
Nick Peers takes a trip down memory lane to reveal the story behind the rise – and slight fall – of Mozilla’s popular web browser.
CODING ACADEMY
APL: going strong after all these years
It might be almost 60 years old, but the APL programming language continues to have an enthusiastic following, as Mike Bedford reveals.
Write your own classic Pong game
Matt Holder takes a trip down memory lane and creates a Pong-style game, for all your retro gaming needs.
Chat
X
Pocketmags Support