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

websocket/mqtt - feasible? #17

Open
mhaberler opened this issue Mar 15, 2024 · 4 comments
Open

websocket/mqtt - feasible? #17

mhaberler opened this issue Mar 15, 2024 · 4 comments

Comments

@mhaberler
Copy link

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
Copy link
Owner

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

@mhaberler
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

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.

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

2 participants