Skip to main content

NATS.io messaging system client for the Twisted matrix

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/respond.py

from the other run:

$ ./example/sub_only.py

Then from the first window run:

./example/nats_demo.py

Usage

Make a subject subscriber.

for full context see sub_only.py:

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, "demo.nats.io", 4222)

nats_protocol = txnats.io.NatsProtocol(
    verbose=False,
    on_connect=lambda np: np.sub("happy", "6", on_msg=on_happy_msg))

connecting = connectProtocol(point, nats_protocol)
reactor.run()

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:
        nats_protocol.pub(reply_to, "Roger, from {}!".format(responder_id))

…:

nats_protocol = txnats.io.NatsProtocol(
    verbose=False,
    on_connect=lambda np: np.sub("get-response", "6", on_msg=respond_on_msg))

For full context see respond.py

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 = txnats.io.NatsProtocol(
    verbose=False,
    on_connect=lambda np: np.sub("get-response", "6",
                                 queue_group="excelsior",
                                 on_msg=respond_on_msg))

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 queue_respond.py and something that will make a bunch of requests on that subject. make_requests.py

Metadata

Release files for txnats 0.7.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for txnats 0.7.5
File Size Uploaded Provenance Actions
txnats-0.7.5.tar.gz 15.6 kB Without provenance Details for txnats-0.7.5.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for txnats 0.7.5
File Interpreter ABI Platform Actions
txnats-0.7.5.macosx-10.10-x86_64.exe Details for txnats-0.7.5.macosx-10.10-x86_64.exe
txnats-0.7.5-py3.5.egg Legacy Egg format - - Details for txnats-0.7.5-py3.5.egg
txnats-0.7.5-py2.7.egg Legacy Egg format - - Details for txnats-0.7.5-py2.7.egg

Total release size: 172.8 kB

Release files / txnats-0.7.5.tar.gz

Download URL txnats-0.7.5.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c5cbd0fa5d0da36c9ef5e9051e2cb85456a900e4e88114a37890f1a8e2bac22a
BLAKE2b-256 checksum
How to use checksums
91fdc8dacfdd34cdf368a30ec7fcf1f72bbfcafbdcd9466b3a4ff0bc837fd3da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / txnats-0.7.5.macosx-10.10-x86_64.exe

Download URL txnats-0.7.5.macosx-10.10-x86_64.exe
Size 82.1 kB
Tags Source
SHA-256 checksum
How to use checksums
babd767e097d3fe9e421268cbfcf9c696d51e58dba7872bac41f86a881bd5d59
BLAKE2b-256 checksum
How to use checksums
e1265b49a5e97febf2f730b1049a910366cb18d41b22eb78024912132697ec4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / txnats-0.7.5-py3.5.egg

Download URL txnats-0.7.5-py3.5.egg
Size 38.0 kB
Tags Egg
SHA-256 checksum
How to use checksums
90e84ee016ed771b55c1791cc51e17f6183644d38f8c170ae77afc1cbad279c3
BLAKE2b-256 checksum
How to use checksums
9b17e5482533d3b3de9f21ae847e78dee4064057b560c5aa11e6fc7970df823f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / txnats-0.7.5-py2.7.egg

Download URL txnats-0.7.5-py2.7.egg
Size 37.1 kB
Tags Egg
SHA-256 checksum
How to use checksums
67ed8cf455ec1df792bbffcd2a8174e4fae562efd7293eaca4a1afbc5b648e5c
BLAKE2b-256 checksum
How to use checksums
eb56e54bd8edadc85edb2666c473be6d324819fef48cc1d6756fa7bf73655e20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for txnats 0.7.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page