61

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
Putnik
  • 4,531
  • 5
  • 31
  • 51
J. Doe
  • 815
  • 2
  • 8
  • 15

5 Answers5

80

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"
Travis Clarke
  • 5,065
  • 5
  • 27
  • 36
37

Well, you can try to mimic the required headers to get some response using curl:

Also, there are other ways to communicate with a WebSocket server, e.g.

Pavel
  • 7,168
  • 2
  • 28
  • 41
18

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.

not2qubit
  • 11,992
  • 7
  • 83
  • 112
Vi.
  • 33,936
  • 16
  • 92
  • 141
  • You forgot to mention that you need to enter the correct JSON request, so I added it. – not2qubit Nov 10 '19 at 14:23
  • `websocat` got me running instantly, even on MacOS! `brew install websocat` then just give the socket server address on the command line and you have a conversation! Awesome! – NeilG May 08 '21 at 12:36
3

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

igonejack
  • 2,091
  • 17
  • 26
2

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.

Hiren Makwana
  • 460
  • 4
  • 10