-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.py
35 lines (27 loc) · 1 KB
/
subscriber.py
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
28
29
30
31
32
33
34
35
import asyncio
from nats.aio.client import Client as NATS
from nats.aio.errors import ErrConnectionClosed, ErrTimeout
async def subscribe_message():
# Creating NATS client
nc = NATS()
try:
# Connecting to NATS server
print("Connecting to NATS server...")
await nc.connect("nats://192.168.33.10:4222")
print("Connected to NATS server")
async def message_handler(msg):
print("Received message:")
print(msg.data.decode())
# Subscribing to the subject
print("Subscribing to the subject 'my_subject'")
await nc.subscribe("my_subject", cb = message_handler)
# Keeping the script running
while True:
await asyncio.sleep(1)
except (ErrConnectionClosed, ErrTimeout) as e:
print("Error: Failed to subscribe to the subject. Connection closed or timeout.")
print(str(e))
finally:
# Disconnecting from NATS server
await nc.close()
asyncio.run(subscribe_message())