caiofilus / moleculer-client Goto Github PK
View Code? Open in Web Editor NEWSimple Client in python to communicate with MoleculerJs Microservices using NATS
License: MIT License
Simple Client in python to communicate with MoleculerJs Microservices using NATS
License: MIT License
Hi @CaioFilus ,
tried to bring the moleculer-client example work with this ServiceBroker
Following the example I got
In [1]: from moleculer_client import MoleculerClient
In [4]: moleculer = MoleculerClient(node_id='python-node', moleculer_nodeID="master", url='nats://127.0.0.1:4222')
In [5]: print(moleculer.discover())
---------------------------------------------------------------------------
timeout Traceback (most recent call last)
<ipython-input-5-ea42592b8850> in <module>
----> 1 print(moleculer.discover())
/opt/miniconda3/envs/moleculerPy/lib/python3.6/site-packages/moleculer_client/moleculer_client.py in discover(self, broadcast)
81 else:
82 self.nc.publish(MoleculerCommands.DISCOVER.format(self.node), payload=json.dumps(body).encode())
---> 83 self.nc.wait(count=1)
84 return result['payload']
85
/opt/miniconda3/envs/moleculerPy/lib/python3.6/site-packages/pynats/client.py in wait(self, count)
217 total = 0
218 while True:
--> 219 command, result = self._recv(MSG_RE, PING_RE, OK_RE)
220 if command is MSG_RE:
221 self._handle_message(result)
/opt/miniconda3/envs/moleculerPy/lib/python3.6/site-packages/pynats/client.py in _recv(self, *commands)
259
260 def _recv(self, *commands: Pattern[bytes]) -> Tuple[Pattern[bytes], Match[bytes]]:
--> 261 line = self._readline()
262
263 command = self._get_command(line)
/opt/miniconda3/envs/moleculerPy/lib/python3.6/site-packages/pynats/client.py in _readline(self, size)
275
276 while True:
--> 277 line = self._socket_file.readline()
278 if not line:
279 raise NATSReadSocketError()
/opt/miniconda3/envs/moleculerPy/lib/python3.6/socket.py in readinto(self, b)
584 while True:
585 try:
--> 586 return self._sock.recv_into(b)
587 except timeout:
588 self._timeout_occurred = True
timeout: timed out
It seems the self.nc.wait(count=1) causes this error.
Meanwhile, using nats-python==0.7.0 to interact with the nats broker works well
In [6]: from pynats import NATSClient
...:
...: with NATSClient(url='nats://127.0.0.1:4222', socket_timeout=5) as client:
...: # Connect
...: client.connect()
...:
...: # Subscribe
...: def callback(msg):
...: print(f"Received a message with subject {msg.subject}: {msg}")
...:
...: client.subscribe(subject="MOL.DISCOVER.master", callback=callback)
...:
...: # Publish a message
...: client.publish(subject="MOL.DISCOVER.master", payload=b"test-payload")
...:
...: # wait for 1 message
...: client.wait(count=1)
...:
Received a message with subject MOL.DISCOVER.master: NATSMessage(sid=0, subject='MOL.DISCOVER.master', reply='', payload=b'test-payload')
Here the last line client.wait(count=1) does not cause this error.
Thanks for helping me address this error.
Hi Caio,
I am currently implementing this moleculer client in python environment, so I wonder how to obtain the node-id=python-node which is required to work:
moleculer = MoleculerClient(node_id='python-node', moleculer_nodeID="engineering-tools-pc-644", url='nats://localhost:4222')
The other parameters a doing good and I guess this the only one missing.
May you pleasae help me thru...
Thanks
Eduardo
Hi Caio, how're you doing, hope well,
I have been successful in communicating Moleculer as a client for python. all works fine when NATS transporter is running over the same computer than both. the problem is when I tried to reach NATS over other machine.
Have you got any suggestions on how to make it sorted?
Thanks one more time.
Eduardo
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.