Good resources to learn how to use websocket push api in python
Tags: websocketHow to connect to poloniex.com websocket api using a python library
The problem:
I am trying to connect to wss://api.poloniex.com and subscribe to ticker. I can’t find any working example in python. I have tried to use autobahn/twisted and websocket-client 0.32.0.
The purpose of this is to get real time ticker data and store it in a mysql database.
The solution:
What you are trying to accomplish can be done by using WAMP, specifically by using the WAMP modules of the autobahn library (that you are already trying to use).
After following their docs, I managed to set-up a simple example using autobahn and asyncio. The following example subscribes to the ‘ticker’ feed and prints the received values:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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() |
We have install the necessary python packages:
|
1 |
pip install autobahn |
|
1 |
pip install autobahn[twisted] |
or
|
1 |
pip install autobahn[twisted,accelerate,compress,serialization] |
References:
https://autobahn.readthedocs.io/en/latest/installation.html
https://github.com/zhumzhu/autobahn-python
https://autobahn.readthedocs.io/en/latest/websocket/programming.html#running-a-server
https://github.com/s4w3d0ff/python-poloniex
https://github.com/s4w3d0ff/python-poloniex/blob/master/examples/ticker/queuedTicker.py
http://stackoverflow.com/questions/32154121/how-to-connect-to-poloniex-com-websocket-api-using-a-python-library











