explodinglabs / jsonrpcserver

Process incoming JSON-RPC requests in Python
MIT License
186 stars 40 forks source link

MQTT transport support #97

Closed nicola-lunghi closed 4 years ago

nicola-lunghi commented 4 years ago

Hi, I need to implement json-rpc over mqtt Is possible to implement a custom transport layer for this?

Thanks Nick

bcb commented 4 years ago

Hi Nicola, something like this should work (keep in mind I haven't used mqtt, this is completely untested):

import paho.mqtt.client as mqtt
from jsonrpcserver import method, dispatch

@method
async def ping():
    return "pong"

def on_connect(client, userdata, flags, rc):
    client.subscribe("topic/test")

def on_message(client, userdata, msg):
    response = dispatch(msg.payload.decode())
    # Send str(response) here, not sure how to do it in mqtt

client = mqtt.Client()
client.connect("THE_IP_ADDRESS_OF_OUR_BROKER", 1883, 60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()
nicola-lunghi commented 4 years ago

Hi thanks I'll test it out and then maybe add an example to the example dir. Cheers Nick

On Tue, 12 Nov 2019, 00:33 Beau Barker, notifications@github.com wrote:

Hi Nicola, something like this should work (keep in mind I haven't used mqtt, this is completely untested):

import paho.mqtt.client as mqttfrom jsonrpcserver import method, dispatch def on_connect(client, userdata, flags, rc): client.subscribe("topic/test") def on_message(client, userdata, msg): response = dispatch(msg.payload.decode())

Send str(response) here, not sure how to do it in mqtt

client = mqtt.Client() client.connect("THE_IP_ADDRESS_OF_OUR_BROKER", 1883, 60) client.on_connect = on_connect client.on_message = on_message client.loop_forever()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bcb/jsonrpcserver/issues/97?email_source=notifications&email_token=AGB6YTHPBY3AFYWNBQS22SDQTH2VXA5CNFSM4JLUILV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEDYTVFY#issuecomment-552680087, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGB6YTCQO3VHKNVBN3VN5QTQTH2VXANCNFSM4JLUILVQ .