You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can also not use the 'helper' methods at all and use `poloniex.PoloniexBase` which only has `returnMarketHist` and `__call__` to make rest api calls.
78
78
79
79
#### Websocket Usage:
80
-
To connect to the websocket api just create a child class of `PoloniexSocketed` like so:
80
+
To connect to the websocket api use the `PoloniexSocketed` class like so:
81
81
```python
82
82
import poloniex
83
83
import logging
84
+
from time import sleep
84
85
85
-
logging.basicConfig()
86
-
87
-
classMySocket(poloniex.PoloniexSocketed):
88
-
89
-
defon_heartbeat(self, msg):
90
-
"""
91
-
Triggers whenever we get a heartbeat message
92
-
"""
93
-
print(msg)
94
-
95
-
defon_volume(self, msg):
96
-
"""
97
-
Triggers whenever we get a 24hvolume message
98
-
"""
99
-
print(msg)
100
-
101
-
defon_ticker(self, msg):
102
-
"""
103
-
Triggers whenever we get a ticker message
104
-
"""
105
-
print(msg)
106
-
107
-
defon_market(self, msg):
108
-
"""
109
-
Triggers whenever we get a market ('currencyPair') message
110
-
"""
111
-
print(msg)
112
-
113
-
defon_account(self, msg):
114
-
"""
115
-
Triggers whenever we get an account message
116
-
"""
117
-
print(msg)
118
-
119
-
sock = MySocket()
120
86
# helps show what is going on
121
-
sock.logger.setLevel(logging.DEBUG)
122
-
# start the websocket thread and subscribe to '24hvolume'
123
-
sock.startws(subscribe=['24hvolume'])
87
+
logging.basicConfig()
88
+
poloniex.logger.setLevel(logging.DEBUG)
89
+
90
+
defon_volume(data):
91
+
print(data)
92
+
# make instance
93
+
sock = poloniex.PoloniexSocketed()
94
+
# start the websocket thread and subscribe to '24hvolume' setting the callback to 'on_volume'
95
+
sock.startws(subscribe={'24hvolume': on_volume})
124
96
# give the socket some time to init
125
-
poloniex.sleep(5)
126
-
# this won't work:
127
-
#sock.subscribe('ticker')
128
-
# use channel id to un/sub
129
-
sock.subscribe('1002')
130
-
poloniex.sleep(1)
131
-
# unsub from ticker
132
-
sock.unsubscribe('1002')
133
-
poloniex.sleep(4)
97
+
sleep(5)
98
+
# use the channel name str or id int to subscribe/unsubscribe
99
+
sock.subscribe(chan='ticker', callback=print)
100
+
sleep(1)
101
+
# unsub from ticker using id (str name can be use as well)
102
+
sock.unsubscribe(1002)
103
+
sleep(4)
104
+
# stop websocket
134
105
sock.stopws()
135
106
136
107
```
@@ -152,5 +123,12 @@ DEBUG:poloniex:Unsubscribed to ticker
152
123
DEBUG:poloniex:Websocket Closed
153
124
INFO:poloniex:Websocket thread stopped/joined
154
125
```
126
+
You can also subscribe and start the websocket thread when creating an instance of `PoloniexSocketed` by using the `subscribe` and `start` args:
0 commit comments