Skip to main content

A tyo-mq client library

Project description

tyo-mq-client-python

A TYO-MQ python client for TYO-MQ server.

TYO-MQ server

please refer to [TYO-MQ Server]{https://github.com/tyolab/tyo-mq}/server.js

In short, TYO-MQ is an implementation of pub/sub message distribution service using socket.io(webscoket).

Creating a messaging server

var MessageServer = require("tyo-mq").server;

var mq = new MessageServer();
mq.start();

Creating a message producer and consumer

from tyo_mq_client.message_queue import MessageQueue
from tyo_mq_client.socket import Socket
from tyo_mq_client.logger import Logger
from tyo_mq_client.constants import Constants

subscriber = None
producer = None
ready = [False, False]

mq = MessageQueue()

producer = mq.createPublisher("TYO Lab")

# A producer can be also a consumer
subscriber = producer

def on_message_published(message):
    print ("received message" + json.dumps(message))
    os._exit(1)

def check ():
    if (ready[0] and ready[1]):
        producer.produce({"message": "Hello World"})

def subscriber_on_connect () :
    Logger.log("Subscriber is connected")
    ready[0] = True
    subscriber.subscribe(producer.name, Constants.EVENT_DEFAULT, on_message_published, False)
    
def on_subscription (data) :
    print ("received subscription")
    producer.produce({"message1": "Hello World"})
    producer.on('test', on_message_published)
    producer.socket.emit('test')
    producer.produce({"message2": "Hello World"}, producer.eventDefault, Constants.METHOD_BROADCAST)

def producer_on_connect () :
    subscriber_on_connect() 

    Logger.log("Producer is connected")
    ready[1] = True
    producer.on_subscription_listener = on_subscription
    producer.on_subscriber_lost(on_subscriber_lost)

def on_subscriber_lost (data):
    message = json.dumps(data)
    Logger.log('Informed that connection with a subscriber (' + message["consumer"] + ') was lost')

producer.add_on_connect_listener(producer_on_connect)
producer.connect(-1)

Publish tyo-mq-client-python to pypi

python setup.py sdist
twine upload dist/*

Maintainer

Eric Tang @ TYO LAB

Project details


Download files

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

Source Distribution

tyo-mq-client-0.0.7.tar.gz (9.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page