semvec/pypi-packages huggingface dataset:
django-mease 0.2.0
django-mease: Websocket integration made easy for Django
License: MIT

django-mease
============

Work in progress.

Websocket integration made easy for Django using Tornado, Redis PUB/SUB and easy to use callback registry mechanism.

See a full working example `here <https://github.com/florianpaquet/django-mease-example>`_

RTD INCOMING SHORTLY
--------------------

Installation
------------

Dependencies (Debian & Ubuntu)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``Mease`` comes with Redis PUB/SUB backend by default. If you want to use it, install these dependencies :

::

    sudo apt-get install redis-server python-dev
    pip install redis


Install django-mease
~~~~~~~~~~~~~~~~~~~~

::

    pip install django-mease


Add mease to your INSTALLED_APPS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python

    INSTALLED_APPS = (
      # List of your installed apps
      'djmease',
    )



Usage
-----

Start websocket server
~~~~~~~~~~~~~~~~~~~~~~

``python manage.py run_websocket_server``


Register callbacks
~~~~~~~~~~~~~~~~~~

Create ``mease_registry.py`` files to register your callbacks (the file must be located in an installed app):

.. code:: python

    from djmease import mease

    @mease.opener
    def open_websocket(client, clients_list):
        client.storage["name"] = "mymane"
        for c in clients_list:
            c.send("Someone joined")

    @mease.closer
    def close_websocket(client, clients_list):
        for c in clients_list:
            c.send("{name} left".format(name=client.storage["name"]))

    @mease.receiver
    def receive_websocket_message(client, clients_list, message):
        for c in clients_list:
            c.send(message)

    @mease.sender(routing=['websocket'])
    def send_websocket_message(channel, clients_list, myobj):
        for c in clients_list:
            c.send(myobj.myattr)


Publish
~~~~~~~

Use ``mease.publish`` to publish from anywhere in your code :

.. code:: python

    from djmease import mease

    mease.publish('websocket', "Hello world !")


This will call all sender functions registered on the 'websocket' routing.
mease 0.2.0
Mease: Twisted/Autobahn websocket server with an easy callback registry
License: MIT

#####
mease
#####

.. image:: https://badge.fury.io/py/mease.png
    :target: http://badge.fury.io/py/mease

.. image:: https://travis-ci.org/florianpaquet/mease.png?branch=master
    :target: https://travis-ci.org/florianpaquet/mease

Websocket server using Twisted/Autobahn with an easy to use callback registry mechanism

See `django-mease <https://github.com/florianpaquet/django-mease>`_, `django-mease-example <https://github.com/florianpaquet/django-mease-example>`_ or `flask-mease-example <https://github.com/florianpaquet/flask-mease-example>`_ for working examples.

************
Installation
************

Use pip to install the latest mease version : ::

    pip install mease

``mease`` comes with two backends :

Redis
=====

To use Redis backend, install these dependencies : ::

    sudo apt-get install redis-server
    pip install redis

Refer to the `Redis documentation <http://redis.io/documentation>`_ to configure your server.

RabbitMQ
========

To use RabbitMQ backend, install these dependencies : ::

    sudo apt-get install rabbitmq-server
    pip install kombu

Refer to the `RabbitMQ documentation <http://www.rabbitmq.com/documentation.html>`_ to configure your server.

**********
Quickstart
**********

Create a file where you can write your callbacks and register them :

.. code:: python

    from mease import Mease
    from mease.backends.redis import RedisBackend
    # OR from mease.backends.rabbitmq import RabbitMQBackend

    from uuid import uuid4

    mease = Mease(RedisBackend)

    @mease.opener
    def example_opener(client, clients_list):
        # Do stuff on client connection
        client.storage['uuid'] = str(uuid4())

    @mease.closer
    def example_closer(client, clients_list):
        # Do stuff on client disconnection
        print("Client {uuid} disconnected".format(uuid=client.storage.get('uuid')))

    @mease.receiver(json=True)
    def example_receiver(client, clients_list, message):
        # Do stuff on incoming client message
        pass

    @mease.sender(routing='mease.demo')
    def example_sender(routing, clients_list, my_tuple):
        # Do stuff on outgoing message
        pass

    if __name__ == '__main__':
        # Start websocket server
        mease.run_websocket_server()

Remember to run the websocket server from the ``mease`` instance where you registered your callbacks.

In your code, you can now call the mease ``publish`` method to send a message to websocket clients :

.. code:: python

    from mease import Mease
    from mease.backends.redis import RedisBackend

    mease = Mease(RedisBackend)

    # ...

    mease.publish('mease.demo', my_tuple=("Hello", "World"))

That's it ! You are now able to send messages from your web server to your websocket server in a cool way !