Skip to main content

NATS messaging system client using Twisted #microservices

Project description

License MIT

NATS Documentation

HOW TO microservice communication.

How do I get my services to talk to each other with out registering each one in a directory?

How do I scale a back end service and allow front end services to use all of them quickly?

How do I have a fast, scalable, resilient communication service with out a lot of headaches?

A good answer to all of these is to use NATS. NATS is a masterpiece protocol designed with the cloud in mind. HTTP was good, but it was designed for servers sending single resources to clients on an uncontrolled network.

Install dependencies

I suggest creating a virtualenv.

$ make deps

Try the demo:

$ make prepare-example

Open two extra terminal windows and from one run:

$ ./example/

from the other run:

$ ./example/

Then from the first window run:



Make a subject subscriber.

for full context see

First, define a message handler for a subscription:

def on_happy_msg(nats_protocol, sid, subject, reply_to, payload):
    print "got message", sid, subject

Then in create the endpoint, nats protocol instance and connect them:

point = TCP4ClientEndpoint(reactor, "", 4222)

nats_protocol =
    on_connect=lambda np: np.sub("happy", "6", on_msg=on_happy_msg))

connecting = connectProtocol(point, nats_protocol)

When a message comes in for the subject “happy” on_happy_msg will be called.

Make a subject responder.

The only difference from a subscriber is that the on_msg function will publish a message to the reply_to subject of the message:

def respond_on_msg(nats_protocol, sid, subject, reply_to, payload):
    if reply_to:, "Roger, from {}!".format(responder_id))


nats_protocol =
    on_connect=lambda np: np.sub("get-response", "6", on_msg=respond_on_msg))

For full context see

Distribute the responder load.

To distribute the load: subscribe with a queue-group and any message will go to one of the responders:

nats_protocol =
    on_connect=lambda np: np.sub("get-response", "6",

However many processes you have subscribed to this subject and queue group will have the messages sent to only one of them. If there are four running, and 100 messages are sent on the “get-response” subject, each one should only have to respond to 25, distributing the work load.

For full context see and something that will make a bunch of requests on that subject.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for txnats, version 0.7.5
Filename, size File type Python version Upload date Hashes
Filename, size txnats-0.7.5.macosx-10.10-x86_64.exe (82.1 kB) File type Windows Installer Python version any Upload date Hashes View
Filename, size txnats-0.7.5-py2.7.egg (37.1 kB) File type Egg Python version 2.7 Upload date Hashes View
Filename, size txnats-0.7.5-py3.5.egg (38.0 kB) File type Egg Python version 3.5 Upload date Hashes View
Filename, size txnats-0.7.5.tar.gz (15.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page