-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
connect to MQTT v5 broker (like EMQX) over Websocket #67
Comments
Hi, @fangchih. At this moment ws transport is experimital feature in gmqtt. You may help us to debug and test it by using gmqtt from support-ws-transport branch. As I say, this is experimital feature and in future we can to change methods and ways to use it; Here is a small exmaple how to init client with ws as transport: mqtt = Client("test")
mqtt.set_auth_credentials('TOKEN')
# init your handlers on_connect, on_message and etc
await mqtt.connect('ws://mqtt.flespi.io', keepalive=60)
mqtt.subscribe('#')
data_to_send = '0xDEADBEAF' * (512 * 102)
mqtt.publish('topic/test', data_to_send) |
Hi, @Mixser. I get the wrong result using the above example.
Below is my code. How can I solve this problem? Thank you. import asyncio
import os
import signal
import time
from gmqtt import Client
# gmqtt also compatibility with uvloop
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
STOP = asyncio.Event()
def on_connect(client, flags, rc, properties):
print('Connected')
def on_message(client, topic, payload, qos, properties):
print('RECV TOPIC: ', topic)
def on_disconnect(client, packet, exc=None):
print('Disconnected')
def on_subscribe(client, mid, qos):
print('SUBSCRIBED, mid: {}'.format(mid))
def ask_exit(*args):
STOP.set()
async def main(broker_host, token):
mqtt = Client("test")
mqtt.on_connect = on_connect
mqtt.on_message = on_message
mqtt.on_disconnect = on_disconnect
mqtt.on_subscribe = on_subscribe
# mqtt.set_auth_credentials(token, None)
await mqtt.connect(broker_host, keepalive=60)
mqtt.subscribe('#')
data_to_send = '0xDEADBEAF' * (512 * 102)
mqtt.publish('topic/test', data_to_send)
await STOP.wait()
await mqtt.disconnect()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGINT, ask_exit)
loop.add_signal_handler(signal.SIGTERM, ask_exit)
host = 'ws://mqtt.flespi.io'
token = os.environ.get('FLESPI_TOKEN')
loop.run_until_complete(main(host, token)) |
@jeffreykira WS support feature is not |
@Lenka42 Thank you for your reminder. After adjustment I got the following error.
|
@jeffreykira This happens because you didn't set valid credentials for the client, uncomment this line:
|
@Lenka42 Thanks for your replay. It works for me. |
Any news about this branch? When will it been merged to master? |
how to setup the connection string? thanks~
The text was updated successfully, but these errors were encountered: