This library helps you with interacting WebSocket through Bash scripting language, it adds features for creating, connecting, sending and receiving text from WebSocket server!
Table of Contents
Shocket dependencies:
websocat
from Websocat.nc
from OpenBSD variant of netcat, GNU variant might also works.
You can install Shocket via bpkg
:
bpkg install komothecat/wshocket
or copy-and-paste it:
git clone https://github.com/komothecat/wshocket
cp wshocket/wshocket.sh .
Shocket does not use async event or anything, just wshocket_new
, wshocket_connect
and wshocket_close
. wshocket.sh # or `source wshocket.sh`
wshocket_new my_ws wss://ws.ifelse.io # create `my_ws` variable with uri
echo "inside my_ws"
declare -p my_ws
if wshocket_connect my_ws conn_err; then # connect to the websocket echo server
wshocket_receive my_ws >/dev/null # suppress message from server
wshocket_send my_ws 'Hello from Shocket'
wshocket_receive my_ws #=> Hello from Shocket
else echo "$conn_err"; exit 1; fi
wshocket_close my_ws
For tracking error, append
wshocket_*
function argument, e.gwshocket_connect ws err
. This will create a variable namederr
in your environment.
Usage: wshocket_new <variable-name> <uri>
Create an associative array with name based on param variable-name
with URI uri
# Creates `ws_var` variable
wshocket_new ws_var wss://ws.example.com
declare -p ws_var
Usage: wshocket_connect <variable>
Start listening to the WebSocket server
wshocket_new ws_var wss://ws.example.com
wshocket_connect ws_var # Connecting and listening to `ws.example.com`
Usage: wshocket_send <variable> <msg>
Send a message to WebSocket server
wshocket_connect ws_var
wshocket_send ws_var "Hello!" # Send "Hello!" to server
Usage: wshocket_receive <variable>
Receive message from WebSocket server and sent it to STDOUT
wshocket_connect ws_var
wshocket_send ws_var "Hello!"
wshocket_receive ws_var # Sent "Hello!" to STDOUT
# or
MSG="$(wshocket_receive ws_var)" # Sent "Hello!" to $MSG
Usage: wshocket_close <variable>
Close connection between client and server.
wshocket_connect ws_var
wshocket_send ws_var "Hello!"
wshocket_receive ws_var
wshocket_close ws_var
# Closes connection between client and server.
# Variable that is created with `wshocket_new` will still remain,
# use `unset` to clear it