All Projects → nats-io → nats.py2

nats-io / nats.py2

Licence: Apache-2.0 license
A Tornado based Python 2 client for NATS

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to nats.py2

diffido
Watch web pages for changes
Stars: ✭ 19 (-69.35%)
Mutual labels:  tornado
rs-nuid
NATS unique identifiers
Stars: ✭ 19 (-69.35%)
Mutual labels:  nats
tornado-websocket-client-example
Websocket client application example built on top of Tornado.
Stars: ✭ 34 (-45.16%)
Mutual labels:  tornado
cleanapi
Pretty tornado wrapper for making lightweight REST API services
Stars: ✭ 26 (-58.06%)
Mutual labels:  tornado
th2c
Tornado HTTP/2 Client
Stars: ✭ 79 (+27.42%)
Mutual labels:  tornado
gobench
A benchmark framework based on Golang
Stars: ✭ 50 (-19.35%)
Mutual labels:  nats
graphql-nats-subscriptions
A graphql subscriptions implementation using nats and apollo's graphql-subscriptions
Stars: ✭ 27 (-56.45%)
Mutual labels:  nats
moleculer-java
Java implementation of the Moleculer microservices framework
Stars: ✭ 39 (-37.1%)
Mutual labels:  nats
go-nats-examples
Single repository for go-nats example code. This includes all documentation examples and any common message pattern examples.
Stars: ✭ 99 (+59.68%)
Mutual labels:  nats
your-connection-deserves-a-name
Examples and code to assign a name to your MongoDB, MySQL, NATS, Oracle, PostgreSQL, RabbitMQ, and redis connection.
Stars: ✭ 26 (-58.06%)
Mutual labels:  nats
nats-box
A container with NATS utilities
Stars: ✭ 55 (-11.29%)
Mutual labels:  nats
PyCMS-Tornado
基于python开发的一套内容管理系统
Stars: ✭ 71 (+14.52%)
Mutual labels:  tornado
dokku-nats
a nats plugin for dokku
Stars: ✭ 21 (-66.13%)
Mutual labels:  nats
saisoku
Saisoku is a Python module that helps you build complex pipelines of batch file/directory transfer/sync jobs.
Stars: ✭ 40 (-35.48%)
Mutual labels:  tornado
apispec-webframeworks
Web framework plugins for apispec (formally in apispec.ext).
Stars: ✭ 25 (-59.68%)
Mutual labels:  tornado
tornado-alf
Tornado Oauth 2 client
Stars: ✭ 17 (-72.58%)
Mutual labels:  tornado
py-healthcheck
Write simple healthcheck functions for your Flask or Tornado apps.
Stars: ✭ 92 (+48.39%)
Mutual labels:  tornado
tornado-aws
A low-level Amazon Web Services API client for Tornado
Stars: ✭ 12 (-80.65%)
Mutual labels:  tornado
django-hurricane
Hurricane is an initiative to fit Django perfectly with Kubernetes.
Stars: ✭ 53 (-14.52%)
Mutual labels:  tornado
fixed-wing-sim
Matlab implementation to simulate the non-linear dynamics of a fixed-wing unmanned areal glider. Includes tools to calculate aerodynamic coefficients using a vortex lattice method implementation, and to extract longitudinal and lateral linear systems around the trimmed gliding state.
Stars: ✭ 72 (+16.13%)
Mutual labels:  tornado

NATS - Tornado based Python 2 Client

A Python async client for the NATS messaging system.

License Apache 2.0 Build Status pypi

Supported platforms

Should be compatible with Python 2.7 using Tornado 4.2+ (less than 6.0).

For Python 3, check nats.py

Getting Started

pip install nats-client

Basic Usage

import tornado.ioloop
import tornado.gen
import time
from nats.io.client import Client as NATS

@tornado.gen.coroutine
def main():
    nc = NATS()

    # Establish connection to the server.
    yield nc.connect("nats://demo.nats.io:4222")

    @tornado.gen.coroutine
    def message_handler(msg):
        subject = msg.subject
        data = msg.data
        print("[Received on '{}'] : {}".format(subject, data.decode()))

    # Simple async subscriber
    sid = yield nc.subscribe("foo", cb=message_handler)

    # Stop receiving after 2 messages.
    yield nc.auto_unsubscribe(sid, 2)
    yield nc.publish("foo", b'Hello')
    yield nc.publish("foo", b'World')
    yield nc.publish("foo", b'!!!!!')

    # Request/Response
    @tornado.gen.coroutine
    def help_request_handler(msg):
        print("[Received on '{}']: {}".format(msg.subject, msg.data))
        yield nc.publish(msg.reply, "OK, I can help!")

    # Susbcription using distributed queue named 'workers'
    sid = yield nc.subscribe("help", "workers", help_request_handler)

    try:
        # Send a request and expect a single response
        # and trigger timeout if not faster than 200 ms.
        msg = yield nc.request("help", b"Hi, need help!", timeout=0.2)
        print("[Response]: %s" % msg.data)
    except tornado.gen.TimeoutError:
        print("Timeout!")

    # Remove interest in subscription.
    yield nc.unsubscribe(sid)

    # Terminate connection to NATS.
    yield nc.close()

if __name__ == '__main__':
    tornado.ioloop.IOLoop.current().run_sync(main)

Clustered Usage

import tornado.ioloop
import tornado.gen
from datetime import timedelta
from nats.io import Client as NATS
from nats.io.errors import ErrConnectionClosed

@tornado.gen.coroutine
def main():
    nc = NATS()

    # Set pool servers in the cluster and give a name to the client
    # each with its own auth credentials.
    options = {
        "servers": [
            "nats://secret1:[email protected]:4222",
            "nats://secret2:[email protected]:4223",
            "nats://secret3:[email protected]:4224"
            ]
        }

    # Error callback takes the error type as param.
    def error_cb(e):
        print("Error! ", e)

    def close_cb():
        print("Connection was closed!")

    def disconnected_cb():
        print("Disconnected!")

    def reconnected_cb():
        print("Reconnected!")

    # Set callback to be dispatched whenever we get
    # protocol error message from the server.
    options["error_cb"] = error_cb

    # Called when we are not connected anymore to the NATS cluster.
    options["closed_cb"] = close_cb

    # Called whenever we become disconnected from a NATS server.
    options["disconnected_cb"] = disconnected_cb

    # Called when we connect to a node in the NATS cluster again.
    options["reconnected_cb"] = reconnected_cb

    yield nc.connect(**options)

    @tornado.gen.coroutine
    def subscriber(msg):
        yield nc.publish("pong", "pong:{0}".format(msg.data))

    yield nc.subscribe("ping", "", subscriber)

    for i in range(0, 100):
        yield nc.publish("ping", "ping:{0}".format(i))
        yield tornado.gen.sleep(0.1)

    yield nc.close()

    try:
        yield nc.publish("ping", "ping")
    except ErrConnectionClosed:
        print("No longer connected to NATS cluster.")

if __name__ == '__main__':
    tornado.ioloop.IOLoop.current().run_sync(main)

Wildcard Subscriptions

import tornado.ioloop
import tornado.gen
import time
from nats.io import Client as NATS

@tornado.gen.coroutine
def main():
    nc = NATS()

    yield nc.connect("demo.nats.io")

    @tornado.gen.coroutine
    def subscriber(msg):
        print("Msg received on [{0}]: {1}".format(msg.subject, msg.data))

    yield nc.subscribe("foo.*.baz", "", subscriber)
    yield nc.subscribe("foo.bar.*", "", subscriber)
    yield nc.subscribe("foo.>", "", subscriber)
    yield nc.subscribe(">", "", subscriber)

    # Matches all of above
    yield nc.publish("foo.bar.baz", b"Hello World")
    yield tornado.gen.sleep(1)

if __name__ == '__main__':
    tornado.ioloop.IOLoop.current().run_sync(main)

Advanced Usage

import tornado.ioloop
import tornado.gen
from nats.io import Client as NATS
from nats.io.errors import ErrNoServers

@tornado.gen.coroutine
def main():
    nc = NATS()

    try:
        # Setting explicit list of servers in a cluster and
        # max reconnect retries.
        servers = [
            "nats://127.0.0.1:4222",
            "nats://127.0.0.1:4223",
            "nats://127.0.0.1:4224"
            ]
        yield nc.connect(max_reconnect_attempts=2, servers=servers)
    except ErrNoServers:
        print("No servers available!")
        return

    @tornado.gen.coroutine
    def message_handler(msg):
        subject = msg.subject
        reply = msg.reply
        data = msg.data.decode()
        for i in range(0, 20):
            yield nc.publish(reply, "i={i}".format(i=i).encode())

    yield nc.subscribe("help.>", cb=message_handler)

    @tornado.gen.coroutine
    def request_handler(msg):
        subject = msg.subject
        reply = msg.reply
        data = msg.data.decode()
        print("Received a message on '{subject} {reply}': {data}".format(
            subject=subject, reply=reply, data=data))

    # Signal the server to stop sending messages after we got 10 already.
    yield nc.request(
        "help.please", b'help', expected=10, cb=request_handler)

    # Flush connection to server, returns when all messages have been processed.
    # It raises a timeout if roundtrip takes longer than 1 second.
    yield nc.flush()

    # Drain gracefully closes the connection, allowing all subscribers to
    # handle any pending messages inflight that the server may have sent.
    yield nc.drain()

    # Drain works async in the background.
    yield tornado.gen.sleep(1)

if __name__ == '__main__':
    tornado.ioloop.IOLoop.instance().run_sync(main)

TLS

Advanced customizations options for setting up a secure connection can be done by including them on connect:

# Establish secure connection to the server, tls options parameterize
# the wrap_socket available from ssl python package.
options = {
    "servers": ["nats://127.0.0.1:4444"],
    "tls": {
        "cert_reqs": ssl.CERT_REQUIRED,
        "ca_certs": "./configs/certs/ca.pem",
        "keyfile":  "./configs/certs/client-key.pem",
        "certfile": "./configs/certs/client-cert.pem"
      }
    }
yield nc.connect(**options)

The client will also automatically create a TLS context with defaults in case it detects that it should connect securely against the server:

yield nc.connect("tls://demo.nats.io:4443")

Examples

In this repo there are also included a couple of simple utilities for subscribing and publishing messages to NATS:

    # Make a subscription to 'hello'
    $ python examples/nats-sub hello

    Subscribed to 'hello'
    [Received: hello] world

    # Send a message to hello
    $ python examples/nats-pub hello -d "world"

License

Unless otherwise noted, the NATS source files are distributed under the Apache Version 2.0 license found in the LICENSE file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].