Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] Query related to publisher and subscriber using same client

Thanks Frank. I am using C++.
This information is really useful. It helps to know that same client can be used from multiple threads without using locks in application. In my case, there could be 3 threads - 2 publisher threads, 1 subscriber thread.

Kind Regards, Jayesh

On Wednesday, 14 October, 2020, 7:17:39 pm IST, Frank Pagliughi <fpagliughi@xxxxxxxxxxxxxx> wrote:

You don't mention which library you're looking at (C, C++, Python, Java, Go, etc), but it looks like you mean the C++ one from mqtt::async_client.

But for any of them, including the C++ library, you can certainly use the same client instance to both publish and subscribe to messages.

There aren't any simple examples of a client doing both publish and subscribe, but it's as easy as starting with a subscriber example and adding a call to cli.publish(...) anywhere in the code while you have a connection. The client is also thread-safe, so you can use a single client from multiple threads, such as one thread for publishing messages and another for processing subscriptions.

There is also a slightly more in-depth example of a Remote Procedure Call (RPC) client and server for MQTT v5 (rpc_math_cli.cpp and rpc_math_srvr.cpp). Both of those use a single connection for publishing and subscribing.


On 10/14/20 5:13 AM, jayesh kulkarni wrote:
Hi All,

A question - so far, i have seen sample examples wherein publisher and subscriber are separate classes having its own MQTT client (e.g. 1 mqtt::async_client _client for publishing and 1 for subscription).

Can we have publisher and subscriber both implemented in same class using single MQTT client (mqtt::async_client m_Client)?

Is there any sample example wherein this is done which i can refer ?

I am trying to do this by clubbing publisher and subscriber samples. I want to be sure about the way this is being done. Hence the request.

Thanks in advance.

Kind Regards, Jayesh

paho-dev mailing list
To unsubscribe from this list, visit

Back to the top