Skip to content
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

Open
fangchih opened this issue Oct 30, 2019 · 7 comments
Open

connect to MQTT v5 broker (like EMQX) over Websocket #67

fangchih opened this issue Oct 30, 2019 · 7 comments

Comments

@fangchih
Copy link

how to setup the connection string? thanks~

@Mixser
Copy link
Contributor

Mixser commented Oct 30, 2019

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)

@jeffreykira
Copy link

jeffreykira commented Dec 12, 2019

Hi, @Mixser. I get the wrong result using the above example.

Traceback (most recent call last):
  File "gmqtt_websocket.py", line 60, in <module>
    loop.run_until_complete(main(host, token))
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "gmqtt_websocket.py", line 43, in main
    await mqtt.connect(broker_host, keepalive=60)
  File "/Users/jing/virtualenv/mqtt_test/lib/python3.8/site-packages/gmqtt/client.py", line 143, in connect
    self._connection = await self._create_connection(
  File "/Users/jing/virtualenv/mqtt_test/lib/python3.8/site-packages/gmqtt/client.py", line 160, in _create_connection
    connection = await MQTTConnection.create_connection(host, port, ssl, clean_session, keepalive)
  File "/Users/jing/virtualenv/mqtt_test/lib/python3.8/site-packages/gmqtt/mqtt/connection.py", line 26, in create_connection
    transport, protocol = await loop.create_connection(MQTTProtocol, host, port, ssl=ssl)
  File "uvloop/loop.pyx", line 1914, in create_connection
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Task was destroyed but it is pending!
task: <Task pending name='Task-2' coro=<Client._resend_qos_messages() running at /Users/jing/virtualenv/mqtt_test/lib/python3.8/site-packages/gmqtt/client.py:96> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x10d5c2070>()]>>

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))

@Lenka42
Copy link
Collaborator

Lenka42 commented Dec 12, 2019

@jeffreykira WS support feature is not master yet, so in order to test it you should clone the repo and setup gmqtt from support-ws-transport branch

@jeffreykira
Copy link

@Lenka42 Thank you for your reminder. After adjustment I got the following error.

Traceback (most recent call last):
  File "gmqtt_websocket.py", line 56, in <module>
    loop.run_until_complete(main(host, token))
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "gmqtt_websocket.py", line 39, in main
    await mqtt.connect(broker_host, keepalive=60)
  File "/Users/jing/workspace/mqtt/gmqtt/gmqtt/client.py", line 152, in connect
    raise self._error
gmqtt.gmqtt.mqtt.handler.MQTTConnectError: code 134 (Connection Refused: Bad User Name or Password)

@Lenka42
Copy link
Collaborator

Lenka42 commented Dec 13, 2019

@jeffreykira This happens because you didn't set valid credentials for the client, uncomment this line:

# mqtt.set_auth_credentials(token, None)

@jeffreykira
Copy link

@Lenka42 Thanks for your replay. It works for me.

@honglei
Copy link

honglei commented Dec 12, 2020

Any news about this branch? When will it been merged to master?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants