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

Implement Request/Response flow in MQTT #4668

Closed
dilanSachi opened this issue Jul 19, 2023 · 3 comments · Fixed by ballerina-platform/module-ballerina-mqtt#9
Closed

Implement Request/Response flow in MQTT #4668

dilanSachi opened this issue Jul 19, 2023 · 3 comments · Fixed by ballerina-platform/module-ballerina-mqtt#9
Assignees
Labels
module/mqtt Issues related mqtt module Team/PCM Protocol connector packages related issues Type/Task

Comments

@dilanSachi
Copy link
Contributor

Description:
$subject

@dilanSachi dilanSachi added Type/Task Team/PCM Protocol connector packages related issues module/mqtt Issues related mqtt module labels Jul 19, 2023
@dilanSachi dilanSachi self-assigned this Jul 19, 2023
@dilanSachi
Copy link
Contributor Author

In this feature, a mqtt subscriber has the option of responding to any message sent from a publisher. The publisher sends a metadata as response_topic in its message which the subscriber will respond to. Publisher listens to the response_topic and will get the response sent by the subscriber. In order to specifically identify the response messages, theres a correlation_id that can be set in the message which the subscriber will return.

There is also a concept of sending response_topic_info in the CONNACK packet. CONNACK is sent by the broker when a client is connected to it. From this, response_topic_info, publisher can build a response_topic such that there will be no auth issues when publishing to topics (assume response_topic_info -> rtopic. Publisher can build a response topic as /rtopic/123. Broker provides write access to all /rtopic/#).

@dilanSachi
Copy link
Contributor Author

@dilanSachi
Copy link
Contributor Author

In this,
Publisher -> send and listen
Subscriber -> listen and reply

Publisher side
In java, there’s an api to publish. And a separate callback to subscribe and listen.

Option - 1
We can add a return type to the publish api so that publisher->publish will block until response is received.
However user cannot do anything until a response is received.

Nats also have a similar structure like this. However the java api of nats (send) returns the response directly rather than to a callback. It doesn't have a concept of correlation id where it's user's responsibility to handle the appropriate response to specific publish.

If we try to implement it similar to this, it will block users' operation flow.

Option - 2
Have a separate api to receive messages.
publisher->receive
This will return a stream(stream<mqtt:Message, error>)(like grpc bidirectional streaming).
Publisher can process messages one by one in the receiving order (same as java client).

Subscriber side
In the subscriber side, we can provide an api for the caller to send the response to the response topic.

    remote function onMessage(mqtt:Message message, mqtt:Caller caller) returns error? {
        log:printInfo(check string:fromBytes(message.payload));
        check caller->publish("This is the response");
    }

Caller will automatically grab the response topic and publish to that topic.
The onComplete method is there to get notified when the receiver receives the message and acknowledges it.

@shafreenAnfar WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/mqtt Issues related mqtt module Team/PCM Protocol connector packages related issues Type/Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant