1. Overview

WebSocket is a transmission protocol that enables real-time and full-duplex communication between a client and a server over a single, long-lived TCP connection.

Unlike HTTP, WebSocket enables both the server and the client to initiate communication and exchange data anytime. As a detailed comparison shows, it avoids the need for frequent RESTful HTTP requests/responses.

In this tutorial, we’ll see how to test sending and receiving WebSocket messages in a Linux terminal.

2. Free WebSocket Test Servers

There are free test servers that we can use to experiment with a WebSocket client, such as:

Both provide a GUI for testing via a browser. The former sends a timestamp to the client every second but doesn’t respond to client messages. The latter is a standard echo service that sends a copy of each received message back to the client. Both make sense in testing.

3. wscat

wscat is a WebSocket client with a lot of options. We can install it with npm, the package manager for Node.js, which, in turn, is available through our distribution package manager. For instance, on Debian-based distros, we can install it with apt:

$ sudo apt install npm
$ sudo npm install -g wscat

Let’s try it, keeping in mind that the messages preceded by a less-than symbol (<) are those received, while the ones preceded by a greater-than symbol (>) are those sent:

$ wscat -c wss://ws.ifelse.io
Connected (press CTRL+C to quit)
< Request served by 4338e324
> Hello Baeldung!
< Hello Baeldung!

It works as expected, echoing our message. Let’s also take a look at the other test server:

$ wscat -c wss://echo-websocket.hoppscotch.io
Connected (press CTRL+C to quit)
< 17:51:05 GMT+0000 (Coordinated Universal Time)
< 17:51:06 GMT+0000 (Coordinated Universal Time)
> Hello Baeldung!
< 17:51:07 GMT+0000 (Coordinated Universal Time)
< 17:51:08 GMT+0000 (Coordinated Universal Time)

Again, the behavior is as expected: We receive one timestamp per second without echoing.

4. websocat

websocat can act as a WebSocket server as well as a client. It offers many features, such as auto-reconnect, integration with Nginx, and executing an external program, making it communicate to WebSocket.

There are pre-built binaries for Linux and installation instructions for the most common cases. When websocat isn’t available in the package manager, like in Debian-based distros, we can use a pre-built version:

$ sudo wget -O /usr/local/bin/websocat \
2023-02-19 19:36:44 (271 KB/s) - ‘/usr/local/bin/websocat’ saved [5115224/5115224]
$ sudo chmod a+x /usr/local/bin/websocat
$ websocat --version
websocat 1.11.0

Let’s test the two servers again:

$ websocat wss://ws.ifelse.io
Request served by 4338e324
Hello Baeldung!
Hello Baeldung!
$ websocat wss://echo-websocket.hoppscotch.io
18:50:05 GMT+0000 (Coordinated Universal Time)
18:50:06 GMT+0000 (Coordinated Universal Time)
18:50:07 GMT+0000 (Coordinated Universal Time)

The output is as expected. However, compared to wscat, we don’t have the < and > symbols. We can see more usage examples in the official documentation in README.md and moreexamples.md.

5. wssh3

wssh3 is another small command-line utility that can act as a WebSocket client or server. Its installation is challenging because it requires many steps. However, the only actual prerequisite is Python 3.6 or higher.

Let’s repurpose the official installation instructions to make them work with any recent Ubuntu-based distribution. Here are the steps we can follow:

$ sudo apt install libevent-dev
$ sudo apt install python3
$ python3 --version
Python 3.10.6
$ sudo apt install python3-pip
$ sudo pip3 install --upgrade pip
$ pip3 install gevent
$ git clone https://github.com/Tectract/wssh3.git
$ cd wssh3/ws4py_modified
~/wssh3/ws4py_modified$ sudo python3 setup.py install
~/wssh3/ws4py_modified$ cd..
~/wssh3$ sudo python3 setup.py install
~/wssh3$ cd
$ wssh3 -h
usage: wssh3 [-h] [-l] [-m {text,binary,auto}] [-n] [-q secs] [-v] URL

Let’s now repeat the tests we’ve already seen:

$ wssh3 -n wss://ws.ifelse.io
Request served by 4338e324
Hello Baeldung!
Hello Baeldung!
$ wssh3 -n wss://echo-websocket.hoppscotch.io
20:36:11 GMT+0000 (Coordinated Universal Time)
20:36:12 GMT+0000 (Coordinated Universal Time)
20:36:13 GMT+0000 (Coordinated Universal Time)

Again, the result is as expected.

6. Conclusion

In this article, we looked at some command-line tools for sending and receiving WebSocket messages in a Linux shell.

We omitted curl, although it’s one of the most popular tools for debugging client-server connections, because, at the moment, it doesn’t support WebSocket connections natively.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.