How to read websocket response in linux shell
LinuxCurlWebsocketLinux Problem Overview
Writing a bash script to connect to GDAX's Websocket Feed at wss://ws-feed.gdax.com but curl doesn't seem to support this as I get
curl "wss://ws-feed.gdax.com"
curl: (1) Protocol "wss" not supported or disabled in libcurl
Linux Solutions
Solution 1 - Linux
Assuming you have node
installed, I would give wscat
a shot; it is simple, intuitive, and powerful. Otherwise, @Pavel's answer has an abundance of venerable websocket client alternatives.
# install
npm install -g wscat
# use
wscat -c "wss://ws-feed.gdax.com"
Solution 2 - Linux
Well, you can try to mimic the required headers to get some response using curl:
- https://gist.github.com/htp/fbce19069187ec1cc486b594104f01d0 or
- https://stackoverflow.com/questions/40907888/linux-bash-how-to-open-a-websocket-connection-as-client
Also, there are other ways to communicate with a WebSocket server, e.g.
Solution 3 - Linux
I'd like to add my own tool for this: websocat.
Example session with the service in question:
$ rlwrap websocat wss://ws-feed.gdax.com
# Now enter this line (without the #) for the required JSON request:
# {"type":"subscribe","channels": [{ "name": "heartbeat", "product_ids": ["BTC-USD"] }]}
{"type":"subscriptions","channels":[{"name":"heartbeat","product_ids":["BTC-USD"]}]}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312079752,"time":"2018-07-12T22:32:42.655000Z"}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312079800,"time":"2018-07-12T22:32:43.656000Z"}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312079834,"time":"2018-07-12T22:32:44.656000Z"}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312079945,"time":"2018-07-12T22:32:45.656000Z"}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312079990,"time":"2018-07-12T22:32:46.657000Z"}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312080042,"time":"2018-07-12T22:32:47.657000Z"}
{"type":"heartbeat","last_trade_id":46274576,"product_id":"BTC-USD","sequence":6312080169,"time":"2018-07-12T22:32:48.657000Z"}
# To stop the feed, type this line:
{"type":"unsubscribe","channels": [{ "name": "heartbeat", "product_ids": ["BTC-USD"] }]}
{"type":"subscriptions","channels":[]}
Besides a websocket client, websocat supports WebSocket server and other modes and is aimed to integrate websockets into "UNIX" world in general.
Solution 4 - Linux
ws
start connection by http
protocol, you have to change ws
to http
and some extra headers like this:
curl --include \
--no-buffer \
--header "Connection: Upgrade" \
--header "Upgrade: websocket" \
--header "Host: example.com:80" \
--header "Origin: http://example.com:80" \
--header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
--header "Sec-WebSocket-Version: 13" \
"https://ws-feed.gdax.com"
https://gist.github.com/htp/fbce19069187ec1cc486b594104f01d0
Solution 5 - Linux
You can connect to your server using Telnet also
telnet 120.22.37.128 6870
Once you connect you can send request in Json format
{"requestType":"hi"}
Here 6870 is port port and to open port you need to run
sudo ufw allow 6870
And to check pm2 logs you required to use below command
pm2 logs 0
Hope this hepls.