← index #15995PR #941
Off-topic · high · value 2.629
QUERY · ISSUE

docs: websocket

openby LaySoftopened 2024-10-10updated 2024-10-13
docs

Documentation URL

https://docs.micropython.org/

Description

Where is the built in websocket module documentation?

Code of Conduct

Yes, I agree

CANDIDATE · PULL REQUEST

aiohttp: fix header problem

mergedby jomasnashopened 2024-12-02updated 2025-04-12

Latest version of aiohttp:
When making connection to a Websocket, the header argument is ignored.

The consequence is that you cannot make a connection to most online MQTT broker's over websocket
because they need the header entry: "Sec-WebSocket-Protocol":"mqtt" in the handshake of
the upgrade protocol.

See this small example code:
It connects to a MQTT broker and then sends the CONNECT mqtt packet.
Then it should get a reply of opcode:2, data: b' \x02\x00\x00' where 'data' is a CONNACK mqtt package
Because of the missing header entry "Sec-WebSocket-Protocol":"mqtt" most brokers will refuse the connection or
refuse to accept MQTT packets.

import aiohttp
import asyncio

async def connect():
    url = "ws://test.mosquitto.org:8080"
    headers = {"Sec-WebSocket-Protocol":"mqtt"}
    connect_pkt = bytearray(b'\x10\x10\x00\x04MQTT\x04\x02\x00x\x00\x04fz54')
    
    async with aiohttp.ClientSession(headers=headers).ws_connect(url) as ws:
            print("Connected")
            await ws.send_bytes(connect_pkt)
            opcode, data = await ws.ws.receive()
            print(f"opcode:{opcode}, data{data}")

asyncio.run(connect())

2 comments
Carglglz · 2024-12-05

I've just tested this and it works as expected, hopefully it is merged for the next release, thanks! 👍🏼

dpgeorge · 2025-04-12

Thanks for updating. I tested the example code above and it works with this PR.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied