Skip to content
Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
publisher
.gitignore Created the consumer class and basic consuming it's working Aug 6, 2019
AUTHORS.rst
LICENSE
README.md
__init__.py
consumer.py
exceptions.py
message.py
requirements.txt
setup.cfg
setup.py

README.md

Python Mercure (pymercure)

The goal of this library is to provide a quick way to publish and consume messages on Mercure. If you don't know what Mercure is, take a look here: (https://github.com/dunglas/mercure).

This library is currently under development, so if you find any bug or chance of improvement, please open an issue to us. :)

Installing the library

The library is available on PyPi, so you can install using pip:

pip3 install pymercure

Publishing Messages

As mentioned before, the goal is to provide a quick way to publish messages. And to do so, it's provided the Sync and Async classes.

Sync publisher

import json
from pymercure.publisher.sync import SyncPublisher
from pymercure.message import Message

data = json.dumps({'status': 'test'})
msg = Message(['mytopicname'], data)
publisher = SyncPublisher(
     'http://127.0.0.1:3000/hub',
     'your.Token.Here'
)
publisher.publish(msg)

Async publisher

import json
from pymercure.publisher.async import AsyncPublisher
from pymercure.message import Message

data = json.dumps({'status': 'test'})
msg = Message(['mytopicname'], data)
publisher = AsyncPublisher(
     'http://127.0.0.1:3000/hub',
     'your.Token.Here'
)
publisher.publish(msg)

In the async case, the request will be done using the gevent library.

Consuming messages

To consume messages it's also pretty straight forward. as the consumer runs in a new thread you don't have to worry about it, you just need to pass a callback function to it:

from pymercure.consumer import Consumer

def callback(message):
    print(message.data)


c = Consumer('http://127.0.0.1:3000/hub', ['mytopicname'], callback)
c.start_consumption()

In your callback you will always receive the Message object, with the message data and metadata.

Credits

Created and maintained by Vitor Villar vitor.luis98@gmail.com

You can’t perform that action at this time.