Skip to content

Commit acfc33c

Browse files
authored
[EXC-1305] add auto re connection and re subscription logic to sockets (#77)
1 parent c504b67 commit acfc33c

File tree

5 files changed

+80
-40
lines changed

5 files changed

+80
-40
lines changed

examples/10.sockets_read_only_client.py renamed to examples/10.1.socket_readonly.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,36 @@ async def main():
1919
readOnlyclient = FireflyClient(True, Networks[TEST_NETWORK])
2020
await readOnlyclient.init(True,response)
2121

22+
23+
async def my_callback():
24+
print("Subscribing To Rooms")
25+
# subscribe to global event updates for BTC market
26+
status = await readOnlyclient.socket.subscribe_global_updates_by_symbol(MARKET_SYMBOLS.BTC)
27+
print("Subscribed to global BTC events: {}".format(status))
28+
29+
# subscribe to local user events
30+
status = await readOnlyclient.socket.subscribe_user_update_by_token()
31+
print("Subscribed to user events: {}".format(status))
32+
33+
# triggered when order book updates
34+
print("Listening to exchange health updates")
35+
await readOnlyclient.socket.listen(SOCKET_EVENTS.EXCHANGE_HEALTH.value, callback)
36+
37+
# triggered when status of any user order updates
38+
print("Listening to user order updates")
39+
await readOnlyclient.socket.listen(SOCKET_EVENTS.ORDER_UPDATE.value, callback)
40+
41+
42+
43+
await readOnlyclient.socket.listen("connect",my_callback)
44+
45+
46+
2247
# must open socket before subscribing
2348
print("Making socket connection to firefly exchange")
2449
await readOnlyclient.socket.open()
2550

26-
# subscribe to global event updates for BTC market
27-
status = await readOnlyclient.socket.subscribe_global_updates_by_symbol(MARKET_SYMBOLS.BTC)
28-
print("Subscribed to global BTC events: {}".format(status))
29-
30-
# subscribe to local user events
31-
status = await readOnlyclient.socket.subscribe_user_update_by_token()
32-
print("Subscribed to user events: {}".format(status))
33-
34-
# triggered when order book updates
35-
print("Listening to exchange health updates")
36-
await readOnlyclient.socket.listen(SOCKET_EVENTS.EXCHANGE_HEALTH.value, callback)
37-
38-
# triggered when status of any user order updates
39-
print("Listening to user order updates")
40-
await readOnlyclient.socket.listen(SOCKET_EVENTS.ORDER_UPDATE.value, callback)
41-
51+
4252
# SOCKET_EVENTS contains all events that can be listened to
4353

4454
# logs event name and data for all markets and users that are subscribed.
@@ -49,19 +59,20 @@ async def main():
4959
while not event_received and time.time() < end_time:
5060
time.sleep(1)
5161

52-
# unsubscribe from global events
62+
# # unsubscribe from global events
5363
status = await readOnlyclient.socket.unsubscribe_global_updates_by_symbol(MARKET_SYMBOLS.BTC)
5464
print("Unsubscribed from global BTC events: {}".format(status))
5565

5666
status = await readOnlyclient.socket.unsubscribe_user_update_by_token()
5767
print("Unsubscribed from user events: {}".format(status))
5868

5969

60-
# close socket connection
70+
# # close socket connection
6171
print("Closing sockets!")
6272
await readOnlyclient.socket.close()
6373

64-
await client.close_connections()
74+
await readOnlyclient.apis.close_session()
75+
6576

6677

6778
if __name__ == "__main__":

examples/10.sockets.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,36 @@ async def main():
1616
client = FireflyClient(True, Networks[TEST_NETWORK], TEST_ACCT_KEY)
1717
await client.init(True)
1818

19+
20+
async def my_callback():
21+
print("Subscribing To Rooms")
22+
# subscribe to global event updates for BTC market
23+
status = await client.socket.subscribe_global_updates_by_symbol(MARKET_SYMBOLS.BTC)
24+
print("Subscribed to global BTC events: {}".format(status))
25+
26+
# subscribe to local user events
27+
status = await client.socket.subscribe_user_update_by_token()
28+
print("Subscribed to user events: {}".format(status))
29+
30+
# triggered when order book updates
31+
print("Listening to exchange health updates")
32+
await client.socket.listen(SOCKET_EVENTS.EXCHANGE_HEALTH.value, callback)
33+
34+
# triggered when status of any user order updates
35+
print("Listening to user order updates")
36+
await client.socket.listen(SOCKET_EVENTS.ORDER_UPDATE.value, callback)
37+
38+
39+
40+
await client.socket.listen("connect",my_callback)
41+
42+
43+
1944
# must open socket before subscribing
2045
print("Making socket connection to firefly exchange")
2146
await client.socket.open()
2247

23-
# subscribe to global event updates for BTC market
24-
status = await client.socket.subscribe_global_updates_by_symbol(MARKET_SYMBOLS.BTC)
25-
print("Subscribed to global BTC events: {}".format(status))
26-
27-
# subscribe to local user events
28-
status = await client.socket.subscribe_user_update_by_token()
29-
print("Subscribed to user events: {}".format(status))
30-
31-
# triggered when order book updates
32-
print("Listening to exchange health updates")
33-
await client.socket.listen(SOCKET_EVENTS.EXCHANGE_HEALTH.value, callback)
34-
35-
# triggered when status of any user order updates
36-
print("Listening to user order updates")
37-
await client.socket.listen(SOCKET_EVENTS.ORDER_UPDATE.value, callback)
38-
48+
3949
# SOCKET_EVENTS contains all events that can be listened to
4050

4151
# logs event name and data for all markets and users that are subscribed.
@@ -46,19 +56,19 @@ async def main():
4656
while not event_received and time.time() < end_time:
4757
time.sleep(1)
4858

49-
# unsubscribe from global events
59+
# # unsubscribe from global events
5060
status = await client.socket.unsubscribe_global_updates_by_symbol(MARKET_SYMBOLS.BTC)
5161
print("Unsubscribed from global BTC events: {}".format(status))
5262

5363
status = await client.socket.unsubscribe_user_update_by_token()
5464
print("Unsubscribed from user events: {}".format(status))
5565

5666

57-
# close socket connection
67+
# # close socket connection
5868
print("Closing sockets!")
5969
await client.socket.close()
6070

61-
await client.close_connections()
71+
await client.apis.close_session()
6272

6373

6474

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "firefly_exchange_client"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
description = "Library to interact with firefly exchange protocol including its off-chain api-gateway and on-chain contracts"
55
readme = "README.md"
66
requires-python = ">=3.8"

src/firefly_exchange_client/sockets.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import socketio
2+
import time
23
from .enumerations import MARKET_SYMBOLS, SOCKET_EVENTS
3-
4+
import asyncio
45
sio = socketio.Client()
56

67

@@ -73,6 +74,24 @@ def listener(event, data):
7374
except:
7475
pass
7576
return
77+
78+
@sio.event
79+
def connect():
80+
print("Connected To Socket Server")
81+
# add 10 seconds sleep to allow connection to be established before callbacks for connections are executed
82+
if 'connect' in Sockets.callbacks:
83+
# Execute the callback using asyncio.run() if available
84+
time.sleep(10)
85+
asyncio.run(Sockets.callbacks['connect']())
86+
87+
88+
@sio.event
89+
def disconnect():
90+
print('Disconnected From Socket Server')
91+
if 'disconnect' in Sockets.callbacks:
92+
# Execute the callback using asyncio.run() if available
93+
asyncio.run(Sockets.callbacks['disconnect']())
94+
7695

7796
async def listen(self, event, callback):
7897
"""

0 commit comments

Comments
 (0)