EmbeddedMqttBroker icon indicating copy to clipboard operation
EmbeddedMqttBroker copied to clipboard

websocket/mqtt - feasible?

Open mhaberler opened this issue 1 year ago • 4 comments

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

thanks in advance

Michael

mhaberler avatar Mar 15 '24 15:03 mhaberler

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

  • You need to change ReaderMqttPacket::readMqttPacket ( WiFiClient client) Method
  • Consider client such as the buffer where are all data sent via tcp/ip socket, so:

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:

  • client.ReadBytes(buffer,n bytes), store first n bytes in buffer, so you need to read first n bytes from mqttPacketBuffer and store them in the respective buffer, for example, in fixedHeader.

  • client.read(), read the next 1 byte from buffer, so you need to read the next 1 byte of mqttPacketBuffer.

// 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!.

alexCajas avatar Mar 15 '24 18:03 alexCajas

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 avatar Mar 15 '24 21:03 mhaberler

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

mhaberler avatar Mar 19 '24 15:03 mhaberler

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

  • EmbeddedMqttBorker use a map "std::map<int,MqttClient*> clients" to store the mqtt (tcp) clients, and it use his id in the vector to identify the client and link his subscribed topics with the tcp client in a tree strucutre, but webSocketServer store his own vector with the tcp connected clients, so std::map<int,MqttClient*> clients it is no longer necesary, but the id to use the TopicTree class it is necessary.
  • EmbeddedMqttBorker listen to new clients in NewClientListenerTask class, use it to use your web socket server
  • Use webSocketEvent() to get the id of the webSocket client to use it in TopicTree structure.
  • You need to change MqttClient class, this class use the tcp connection to listen and send mqtt messages, but now you need to use webSocektServer along webSocketClient id, to do the work of this class. Last TCPListenerTask it is no longer necessary because the listen to tcp sockets is handle by webSocketServer.

I will glad to know about your progress.

alexCajas avatar Mar 20 '24 23:03 alexCajas