alexCajas / EmbeddedMqttBroker

This is a Mqtt broker for embedded devices, developed in C++, FreeRTOS to use advanced multitasking capabilities, and arduino core. Tested in an Esp32 and esp8266.
https://github.com/alexCajas/EmbeddedMqttBroker
MIT License
67 stars 13 forks source link

websocket/mqtt - feasible? #17

Open mhaberler opened 4 months ago

mhaberler commented 4 months ago

do you see a way to make this code work over websockets so it's compatible with paho-mqtt js?

thanks in advance

Michael

alexCajas commented 4 months ago

Hi @mhaberler, I believe it is posible, let me get you the keys:

void ReaderMqttPacket::readMqttPacket(WiFiClient client){

1ª auxBuffer = First read all data from client, to extract WebSocket packet from tcp/ip buffer, mqtt packet is contained in WebSocket packet.

2ª mqttPacketBuffer = extract mqtt packet from auxBuffer.

3ª now replace calls to client.ReadBytes() and client.read() with mqttPacketBuffer, simulating these functions, according to the original readMqttPacket(WiFiClient client) method:

// original method to adapt: // 1º reading fixed header. //client.readBytes(fixedHeader,1);

// 2º reading and decoding remainingLengt of this mqtt packet from fixedHeader remainingLengt = readRemainLengtSize(client);

// 3ª reading remaining lengt bytes packet. remainingPacket = (uint8_t*) malloc(remainingLengt); client.readBytes(remainingPacket,remainingLengt);
}

Notice that you need to change ReaderMqttPacket::readRemainLengtSize(WiFiClient client) method to, changing client for mqttPacketBuffer.

Lastly, change mqtt port from 1883 to 80 to listen webSockets connections, when you creating MqttBroker object. I hope this helps!.

mhaberler commented 4 months ago

thanks

I will give it a stab and report here, would appreciate if you can look over my shoulder

any suggestions for the websockets part of the code? several libraries out there looking applicable

this should become be a subclass of MqttBroker, right?

-m

mhaberler commented 4 months ago

I've taken an intermediate step: put a websockets-to-tcp proxy in front of the broker:

https://github.com/mhaberler/EmbeddedMqttBroker-example/blob/websockets/src/example/main.cpp#L125

seems to work fine

alexCajas commented 4 months ago

I was looking you example, I don't know ProxyWebSocketsServer.h, but I assume that it is similar to other web sockets libraries:

I will glad to know about your progress.