0

I want to build a WAMP(the Web Application Messaging Protocol) client to subscribe ticker of poloniex. There are some useful infomation in poloniex's API document like following:

The best way to get public data updates on markets is via the push API,
which pushes live ticker, order book, trade, and Trollbox updates over 
WebSockets using the WAMP protocol. In order to use the push API, 
connect to wss://api.poloniex.com and subscribe to the desired feed. 
In order to receive ticker updates, subscribe to "ticker". 

But they didn't say how to use python to subscribe it.Then I try to search in google, I coundn't anything help.

Can anyone show me how to build a WAMP client to subscibe the ticker of poloniex? Thanks!

----------- update -------------- I found code following what seems do what I want:

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 onTicker(*args):
            print("Ticker event received:", args)

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


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


if __name__ == "__main__":
    main()

But it show error following:

Traceback (most recent call last):
  File "wamp.py", line 5, in <module>
    from autobahn.asyncio.wamp import ApplicationSession
  File "/usr/lib/python2.7/site-packages/autobahn/asyncio/__init__.py", line 36, in <module>
    from autobahn.asyncio.websocket import \
  File "/usr/lib/python2.7/site-packages/autobahn/asyncio/websocket.py", line 32, in <module>
    txaio.use_asyncio()
  File "/usr/lib/python2.7/site-packages/txaio/__init__.py", line 122, in use_asyncio
    from txaio import aio
  File "/usr/lib/python2.7/site-packages/txaio/aio.py", line 47, in <module>
    import asyncio
  File "/usr/lib/python2.7/site-packages/asyncio/__init__.py", line 9, in <module>
    from . import selectors
  File "/usr/lib/python2.7/site-packages/asyncio/selectors.py", line 39
    "{!r}".format(fileobj)) from None
                               ^
SyntaxError: invalid syntax

I found some clue that seems some module only work correctly in python3. How disappointed!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Archer Hu
  • 37
  • 11
  • Search for web socket subscription. I came across this package - https://pypi.python.org/pypi/websocket-client. You might be searching for something too specific with poloniex. – PressingOnAlways Aug 16 '17 at 02:51
  • @PressingOnAlways I am not sure the different between websocket-client and WAMP. Is it same when I use to subscribe from WAMP server? – Archer Hu Aug 16 '17 at 04:24
  • Read http://wamp-proto.org/. https://github.com/crossbario/autobahn-python. http://wamp-proto.org/implementations/ https://github.com/noisyboiler/wampy. You need to improve your googling skills if you want to be a serious developer! – PressingOnAlways Aug 16 '17 at 05:53

1 Answers1

1

Finally I found the answer following, it can do what I want:

import websocket
import thread
import time
import json

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    print("ONOPEN")
    def run(*args):
        ws.send(json.dumps({'command':'subscribe','channel':1001}))
        ws.send(json.dumps({'command':'subscribe','channel':1002}))
        ws.send(json.dumps({'command':'subscribe','channel':1003}))
        ws.send(json.dumps({'command':'subscribe','channel':'BTC_XMR'}))
        while True:
            time.sleep(1)
        ws.close()
        print("thread terminating...")
    thread.start_new_thread(run, ())


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://api2.poloniex.com/",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

Thanks for author who named Ricky Han!

Archer Hu
  • 37
  • 11