Package: pyspoke
pyspokeA python library supporting pubsub and remote procedure calls on Linux.OverviewThis package provides a python library for passing JSON messages between processes. Clients publish messages to named channels and subscribe to channels to receive those messages. Each client connects to a single server that acts as the message broker. Features include:Hierarchical channel names with support for wildcard subscriptionsRemote procedure calls built on top of the publish/subscribe capabilityPersistent messages that are recorded by the server and sent to clients that later subscribe to the channelBridging servers so that they and their clients behave like a single networkCustomizable transport protocol - default is TCP socketsAsynchronous client/server code using standard asyncio moduleSynchronous wrapper functions for publish and rpc callsInstallationRequires Python 3.7 or greaterFromPyPIInstall using pip:python3 -m pip install pyspokeFrom latest sourceFirst install build dependencies:python3 -m pip install buildBuilding the distribution:git clone https://gitlab.com/samflam/pyspoke.git
cd pyspoke
makeTo install, you canpip installthe built wheel indistor simply runmake installTestingFrom the top level, do:make testExamplesBasic publisher and subscriberFirst we run the server that acts as a message broker, receiving published messages and sending them to subscribed clients:"Server code"importasyncioimportspokeserver=spoke.pubsub.server.Server()try:asyncio.run(server.run())exceptKeyboardInterrupt:passNext we need a subscriber that will listen for messages on the given channel(s) and execute a callback function when a message is received. In this case we listen for messages on thefoochannel and just print them out:"Subscriber"importasyncioimportspokeasyncdefhandle_foo(msg):print(f"Got message on foo channel:{msg.body}")asyncdefmain():client=spoke.pubsub.client.Client()awaitclient.run()awaitclient.subscribe("foo",handle_foo)awaitspoke.wait()asyncio.run(main())Finally we publish a message on thefoochannel. This example uses the synchonous wrapper function, which is simpler to use, but must establish a new connection each time it is called:"Publisher (using simple synchronous call)"importspokespoke.publish("foo",5)Connection optionsBy default, clients assume the server is atlocalhost:7181; the server binds to0.0.0.0:7181.
The defaults may be changed:# server and client-subscriber are configured inside a map, the named argument conn_opts:server=spoke.pubsub.server.Server(conn_opts={"host":"localhost","port":4444,"reuse":True})server=spoke.pubsub.server.Server(conn_opts={"reuse":True})client=spoke.pubsub.client.Client(conn_opts={"host":"spoke","port":8888})client=spoke.pubsub.client.Client(conn_opts={"host":"spoke"})# the publisher is different; it takes host and port themselves as named arguments:spoke.publish('world','Hello!',host="spoke",port=8888)spoke.publish('dazai','BSD',port=8888)Command line interfaceThis package provides serveral command line scripts for common tasks. For help on any of them, run with the flag-h:spoke-server- runs a server that acts as the message brokerspoke-echo- subscribes to the given channels and prints any messages that it receivesspoke-publish- publish a message on the given channelspoke-call- do a remote procedure call on the given channel and print the resultspoke-bridge- connect two spoke servers so that they and their clients transparently behave like a single network