11

Hobby coder here working on a weekend project.

I wish to access a publicly available API as present here: https://api.coinsecure.in/ It provides us with BitCoin trade data - the API is via websockets which i am not familiar with.

The Websocket URI is wss://coinsecure.in/websocket And the method i wished to test is : {"method": "recentbuytrades"}

I am able to access the WebScocket API using the "websocket-client" in Python as listed here: https://pypi.python.org/pypi/websocket-client/

But unfortunately I am unable to figure out how to retrieve the data for the particular method - {"method": "recentbuytrades"}

Would be very grateful for any guidance that you could provide on extracting the data for this particular method.

Best, Ryan

[EDIT] Current code I am using is this:

from websocket import create_connection
ws = create_connection("wss://coinsecure.in/websocket")
result =  ws.recv()
print ("Received '%s'" % result)
ws.close()
5
  • Can you post the code you are currently using to connect to the websocket? Commented Jan 30, 2016 at 18:36
  • I have edited my query and added the code that I am currently using Commented Jan 30, 2016 at 18:42
  • 1
    Just do a ws.send({"method": "recentbuytrades"}) and implemeted on_message(...) to receive the response of this message. They must show some examples. For sure, ws is a Connection instance, so have a look to the methods you can override. Commented Jan 30, 2016 at 18:44
  • 1
    tornado also has a clean websocket client implementation, as well as other useful helpers. So if you want to make your life easy, use tornado instead. I used both but I prefer tornado for such things. Commented Jan 30, 2016 at 18:53
  • oh ok sure. Never used Tornado before. Will try it out. Thanks for the help :) Commented Jan 30, 2016 at 18:55

1 Answer 1

11

Try this:

from websocket import create_connection
ws = create_connection("wss://coinsecure.in/websocket")
ws.send('{"method": "recentbuytrades"}')

while True:
  result =  ws.recv()
  print ("Received '%s'" % result)

ws.close()

Note the ws.send() method, which tells the API what you want. Next, the while True infinite loop - WebSockets are indefinite connections; information is often sent over them more than once. You'll get a bunch of information (a 'frame') here from the server (looks like JSON), handle it, and then wait for the next bunch to come.

It also looks like the API will send you data you don't necessarily want. You may want to throw the frame out if it doesn't contain the recentbuytrades key.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.