Skip to content
Melvin Carvalho edited this page Oct 8, 2024 · 6 revisions

Websocat is a versatile command-line tool that enables you to communicate with WebSocket servers and clients with ease. It's perfect for beginners looking to experiment with WebSockets or developers who want to quickly test their WebSocket-based applications.

Installation

To get started with Websocat, first, you need to install it. You can find the installation instructions for your specific platform on the Websocat GitHub repository: https://github.com/vi/websocat

Example: Subscribing to a Nostr Relay

Once you have Websocat installed, you can start exploring its features. One of the most common use cases is to send a message to a WebSocket server and receive messages in response.

For example, let's say you want to subscribe to a Nostr relay. Nostr is a decentralized, event-based messaging system built on top of the Hypercore Protocol. You can use the following command with Websocat to subscribe to a Nostr relay:

echo '["REQ", "x", {}]' | websocat -n --ping-interval 20  ws://localhost:4445

In this command:

  • echo '["REQ", "x", {}]' prepares the subscription request message for the Nostr relay.
  • websocat -n --ping-interval 20 ws://localhost:3338/nostr establishes a WebSocket connection to the Nostr relay running on localhost at port 3338.
    • -n flag tells Websocat to use line-based I/O mode.
    • --ping-interval 20 flag sets the ping interval to 20 seconds to keep the connection alive.

Once you run this command, you will be subscribed to the Nostr relay, and you'll start receiving messages in your terminal. You can interact with the relay and experiment with different commands to learn more about Nostr and WebSockets.

Adding kinds and authors arrays

echo '["REQ", "x", {"kinds":[33334], "authors": ["64401c0577261886bff71b49f22fb35225268f27313fb5af7d5eda7934574746"]}]' | websocat -n --ping-interval 20  ws://localhost:3338 

script to get profile from a file of relays

for i in $( cat relays.txt ) ; do echo '["REQ", "x", {"kinds":[0], "authors": ["de7ecd1e2976a6adb2ffa5f4db81a7d812c8bb6698aa00dcf1e76adb55efd645"]}]' | timeout 2 websocat -n --ping-interval 20 $i | tee results.txt ; echo $i ; sleep 1 ; done

ping a relay

websocat -k -v wss://relay.damus.io/ --ping-interval 5
Clone this wiki locally