pythonsocketswampautobahnpoloniex

Reading Messages on Poloniex Trollbox with Python autbahn or other socket module?


Poloniex doesn't return every message to my socket. I read the messages with the following code and sometimes I get continuous message numbers, but sometimes there are like 10 messages missing:

from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner
from asyncio import coroutine

class PoloniexComponent(ApplicationSession):
    def onConnect(self):
        self.join(self.config.realm)

    @coroutine
    def onJoin(self, details):
        def onTrollbox(*args):

            print("type: ", args[0])
            print("message_number: ", args[1])
            print("user_name: ", args[2])
            print("message: ", args[3])
            print("reputation: ", args[4])

        try:
            yield from self.subscribe(onTrollbox, 'trollbox')
        except Exception as e:
            print("Could not subscribe to topic:", e)

runner = ApplicationRunner("wss://api.poloniex.com", "realm1")
runner.run(PoloniexComponent)

Does anybody know a better solution? I tried this one, but it doesn't work at all:

from websocket import create_connection
ws = create_connection("wss://api.poloniex.com")
ws.send("trollbox")
result = ws.recv()
print "Received '%s'" % result
ws.close()

Solution

  • Here is the solution:

    These missing messages might happen sometimes with the WAMP API. It is due to inherent scalability problems with the routing software, and Poloniex is working on a pure WebSockets API (currently used by the web interface, but lacking documentation) to replace it. The url for the new websocket server is wss://api2.poloniex.com:443 and to connect to trollbox messages you would need to send the message: '{"command" : "subscribe", "channel" : 1001}'.

    Here is an example code, it is much easier to work with:

    from websocket import create_connection
    import json
    
    ws = create_connection("wss://api2.poloniex.com:443")
    ws.send('{"command" : "subscribe", "channel" : 1001}')
    
    while True:
        result = ws.recv()
        json_result = json.loads(result)
        if len(json_result) >= 3:
            print(json_result)
    
    ws.close()