cryptoqween / cryptoqween.github.io

CryptoCompare API tutorials
194 stars 114 forks source link

Can't Connect to WebSocket #1

Closed Alan-Solitar closed 4 years ago

Alan-Solitar commented 7 years ago

Hello. I am trying to connect to the websocket using python.

I am using the python websocket-client module the following way.

socket = websocket.WebSocket() socket.connect('wss://streamer.cryptocompare.com')

However, when i try to connect, i get an error that the websocket is already closed.

Am I doing something wrong or is there an issue with the web socket?

kotvrt commented 7 years ago

I also ran into a whole lot of problems trying to connect using python. Having tested various libraries I assume that there's something wrong with the way server treats the requests.

I either get the error that the websocket is closed or that the connection has been forcefully closed by host.

@Alan-Solitar Please let me know if you manage to advance on this one.

aryeharmon commented 6 years ago

the websocket is not working

ghost commented 6 years ago

This is a weird issue. I found this on that topic: https://stackoverflow.com/questions/45192699/python-socketio-client-not-working

I have the same problem. This snapshot works like a charm:

var socket = require('socket.io-client')('https://streamer.cryptocompare.com/')
var subscription = ['5~CCCAGG~BTC~USD', '5~CCCAGG~ETH~USD']
socket.emit('SubAdd', {subs: subscription})
socket.on("m", function (message) {
    console.log(message)
})

But connection from python seems impossible.

socketIO = SocketIO('https://streamer.cryptocompare.com')
socketIO.on('connect', lambda: print("Connected to server!!!"))
socketIO.wait(seconds=1)
socketIO.emit({'subs': ['5~CCCAGG~BTC~USD', '5~CCCAGG~ETH~USD']})
socketIO.on('m', lambda m: print(m))
spookysys commented 6 years ago

Also cannot get the streaming api to work from python... have tried all kinds of websocket apis. An example would be great!

DhavalThkkar commented 6 years ago

Can anyone please find a solution. This is really needed

Jeiwan commented 6 years ago

You need to specify transport protocol like so:

wss://streamer.cryptocompare.com/socket.io/?transport=websocket
DhavalThkkar commented 6 years ago

@Jeiwan I can now connect to the WebSocket by using your address but there are some issues

import websocket as ws

subs = ['5~CCCAGG~BTC~USD','5~CCCAGG~ETH~USD']
socket = ws.create_connection('wss://streamer.cryptocompare.com/socket.io/?transport=websocket')
socket.send('SubAdd',{'subs': subs})

This gives the following error

socket.send('SubAdd',{'subs': subs})
Traceback (most recent call last):

  File "<ipython-input-54-26867dcffc38>", line 1, in <module>
    socket.send('SubAdd',{'subs': subs})

  File "/home/dhaval/anaconda3/lib/python3.6/site-packages/websocket/_core.py", line 234, in send
    return self.send_frame(frame)

  File "/home/dhaval/anaconda3/lib/python3.6/site-packages/websocket/_core.py", line 253, in send_frame
    data = frame.format()

  File "/home/dhaval/anaconda3/lib/python3.6/site-packages/websocket/_abnf.py", line 209, in format
    raise ValueError("Invalid OPCODE")

ValueError: Invalid OPCODE

Am I doing something wrong?

Jeiwan commented 6 years ago

@DhavalThkkar Socket.io uses WebSocket as a transport layer and implements its own protocol on top of it. So the best solution is to find a Socket.io implementation in your language, because otherwise you would have to implement the protocol yourself. General WebSocket clients and libraries won't work.

ksanderer commented 6 years ago

Is there any working python example? Can't figure out how to deal with cryptocompare WS api.

zyd-zander commented 6 years ago

@Achse Have you solved the problem?I had the same problem.I can't connect the socket.io server through socketIO_client library in python.

jobinbasani commented 6 years ago

Tried to connect from Android,

mSocket = IO.socket("https://streamer.cryptocompare.com/");

mSocket.emit("SubAdd", "5~CCCAGG~BTC~USD");

returns 401~BADFORMAT

What am I doing wrong?

agustarc commented 6 years ago

@jobinbasani Have you solved the problem? I had the same problem.

jobinbasani commented 6 years ago

@joongwon Nope, haven't solved it yet

volkandkaya commented 6 years ago

https://stackoverflow.com/questions/46569944/python-socketio-example-to-connect-to-cryptocompare last example solves it

slidenerd commented 6 years ago

any updates on this yet?

cbadhrinath commented 5 years ago

not able to connect with socket-io-java-client on Android! results in objects returned as "401-BADFORMAT"

cbadhrinath commented 5 years ago

Found the right way for Java client. The following should work:

JSONObject object = new JSONObject(); JSONArray arr = new JSONArray(); try { arr.put("11~BTC'"); object.put("subs", arr); socket.emit("SubAdd", object); } catch (JSONException e) { e.printStackTrace(); }

cryptoqween commented 4 years ago

Hi there, just wanted to let you know that this version of the streaming API is being sunset in the next couple of weeks as we are moving to version 2, that has a lot more features and better support. Please visit our documentation page which provides code examples in javascript, node, python and golang: https://min-api.cryptocompare.com/documentation/websockets.