Skip to content

How to make websocket requests from CLI

stainless steel sockets on closeup shot

We normally use cURL requests to easily encapsulate and share HTTP requests with teammates. To perform websocket requests from the CLI, we can make use of websocat, a CLI client for performing websocket requests.

Using websocat to perform websocket requests from the CLI.

Creating a websocket subscription and sending payload

# Create the connection
websocat -t -E tcp-l:127.0.0.1:6543 reuse-raw:ws://your.websocket.com --max-messages-rev 2&

WS_PID=$!

# Send payload, and echo the response
echo '{"id":"1","type": "stop"}' | nc ${LOCALHOST} ${LOCALPORT}

# Close the connection
kill ${WS_PID}

Command line result

Result of the command would look something like this:

The full source code for this is available on Gist GitHub.

You can also check out my other tech-related posts and tutorials.

New to sapalo.dev?

Hi there reader! My name is Darren and I write about technology, learning, and gaming for mid to senior level software engineers. I love learning and finding lessons (and mistakes) in things that I do.

Sharing what we learn enables us to encourage, uplift, and empower the next set of engineering problem solvers. Writing them down also reminds me to seal the lesson in and not forget them!

I have a series of mental models that helps you think and strategize for problems whether in engineering or in your personal life. I also share short stories about reflections of my life, studying the similarities of playing competitively both in games and in performance at work.

Here are some trending things I’ve been talking about lately:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.