srinskit / RachPy

Publish/subscribe model communication module for IoT applications, multiplayer games etc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RachPy - A Python client for Rach

Note: Only python3 is supported. Use pip3 and python3 if necessary.

Install

pip install rach --user

Usage

Sample code. Run using python test_1.py

# test_1.py

import time
from Rach import Rach

if __name__ == '__main__':
    rach = Rach("ws://localhost:8080", {'username': 'su', 'password': 'pass'})
    # Enable debug messages
    rach.enable_debug()
    one_two_sub = None


    def on_start():
        global one_two_sub
        # Subscribe
        one_two_sub = rach.add_sub('/one/two', lambda x: print(x), [])
        # Call a service
        rach.service_call('/remainder', [5, 2], lambda x, y: print(x, y), [6], lambda err, y: print(err, y), [3])
        rach.service_call('/client.public_id', [], lambda x: print(x), [], lambda err: print(err), [])


    rach.start(on_start)
    try:
        while True:
            time.sleep(20)
    except KeyboardInterrupt:
        rach.stop()
        print('Bye!')

Sample code. Run using python test_2.py

# test_2.py

import time
from Rach import Rach

if __name__ == '__main__':
    rach = Rach("ws://localhost:8080", {'username': 'su', 'password': 'pass'})
    # Enable debug messages
    rach.enable_debug()
    one_two_pub = None


    def on_start():
        global one_two_pub
        # Register publisher
        one_two_pub = rach.add_pub('/one/two/')
        # Call a service
        rach.service_call('/version', [], lambda x: print(x.result), [], lambda err: print(err), [])
        rach.service_call('/client.public_id', [], lambda x: print(x.result), [], lambda err: print(err), [])


    rach.start(on_start)
    try:
        while True:
            if one_two_pub is not None:
                # Publish data
                one_two_pub.pub(time.time())
            time.sleep(5)
    except KeyboardInterrupt:
        rach.stop()
        print('Bye!')

About

Publish/subscribe model communication module for IoT applications, multiplayer games etc

License:MIT License


Languages

Language:Python 100.0%