Socketio asyncio NATS manage can't get subscribe message
See original GitHub issueI write client manager for python-socketio on NATS, following the example of a redis manager. This manager send publish but not get subscribe messages, what am I doing wrong?
NATS (docker latest) Python (docker python:alpine)
import asyncio
import pickle
try:
import nats
except ImportError:
nats = None
from socketio.asyncio_pubsub_manager import AsyncPubSubManager
class AsyncNatsManager(AsyncPubSubManager): # pragma: no cover
"""NATS based client manager for asyncio servers.
This class implements a NATS backend for event sharing across multiple
processes. Only kept here as one more example of how to build a custom
backend, since the kombu backend is perfectly adequate to support a NATS
message queue.
To use a NATS backend, initialize the :class:`Server` instance as
follows::
server = socketio.Server(client_manager=socketio.AsyncNatsManager(
'nats://hostname:port'))
:param url: The connection URL for the NATS server. For a default NATS
store running on the same host, use ``nats://``.
:param channel: The channel name on which the server sends and receives
notifications. Must be the same in all the servers.
:param write_only: If set ot ``True``, only initialize to emit events. The
default of ``False`` initializes the class for emitting
and receiving.
"""
name = 'asyncionats'
def __init__(self, servers=None, channel='socketio',
write_only=False):
if servers is None:
servers = ["nats://nats:4222"]
if nats is None:
raise RuntimeError('NATS package is not installed '
'(Run "pip install asyncio-nats-client" in your '
'virtualenv).')
self.servers = servers
self.queue = asyncio.Queue()
self.producer = None
self.consumer = None
self.sid = None
super().__init__(channel=channel, write_only=write_only)
async def _publish(self, data):
if self.producer is None:
self.producer = await nats.connect(servers=self.servers)
return await self.producer.publish(self.channel, pickle.dumps(data))
async def _listen(self):
print(self)
if self.consumer is None:
self.consumer = await nats.connect(servers=self.servers)
self.sid = await self.consumer.subscribe('socketio', cb=self.message_handler)
return await self.queue.get()
async def message_handler(self, msg):
subject = msg.subject
reply = msg.reply
data = msg.data.decode()
print(f"Received a message on '{subject} {reply}': {data}")
await self.queue.put(data)
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Socketio asyncio NATS manage can't get subscribe message
I write client manager for python-socketio on NATS, following the example of a redis manager. This manager send publish but not get ......
Read more >How do I subscribe to a NATS subject in Python and keep ...
You can find a long running service example as well: ... import asyncio from nats.aio.client import Client as NATS async def run(loop): nc ......
Read more >Asynchronous Subscriptions - NATS Docs
Asynchronous subscriptions use callbacks of some form to notify an application when a message arrives. These subscriptions are usually easier to work with, ......
Read more >Modules - NATS Docs
If a callback isn't provided, messages can be retrieved via an asynchronous iterator on the returned subscription object. Return type. Subscription. asyncnats.
Read more >NATS WebSockets and React - YouTube
Join Synadia Engineer, Alberto Ricart, for this more advanced tutorial - Getting Started with NATS WebSockets and ReactNATS WebSocket Docs ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
OK some updates here and have now a wip example of python socket.io + sanic + asyncio working together more or less: https://gist.github.com/wallyqs/c8d16cf2fa5aa2e4f4e49b714f96575d Not very familiar with the python socket.io framework so feedback welcome, but so far it does not look like there are any blockers in using these tools together, (would be nice to have an implementation of the
class AsyncNatsManager(AsyncPubSubManager):
and a more complete example though).I will check this one today.