GithubHelp home page GithubHelp logo

vitorluis / python-mercure Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 7.0 19 KB

Python library to publish messages to Mercure Hub!

Home Page: https://pypi.org/project/pymercure/

License: BSD 2-Clause "Simplified" License

Python 100.00%
mercure consuming-messages sse

python-mercure's Introduction

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/.well-known/mercure',
     '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/.well-known/mercure', ['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 [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.