Code Monkey home page Code Monkey logo

Comments (1)

ksimicevic avatar ksimicevic commented on May 17, 2024

Hello!

Essentially, you want to set a new mqtt_client::async_receive callback after invoking the previous one. With callbacks, it would look something like this:

namespace asio = boost::asio;

using stream_type = asio::ip::tcp::socket;
using client_type = async_mqtt5::mqtt_client<stream_type>;

void receive_message(client_type& client) {
	client.async_receive([&client](
		async_mqtt5::error_code ec, std::string topic,
		std::string payload, async_mqtt5::publish_props
	) {
                // session and our subscriptions are lost
		if (ec == async_mqtt5::client::error::session_expired) {
			// resubscribe...
		} else if (ec) {
			// break...
		} else {
			// process message
			receive_message(client);
		}
	});
}

int main(int argc, char* argv[]) {
	asio::io_context ioc;

        // using signals to exit
	asio::signal_set signals(ioc, SIGINT, SIGTERM, SIGBREAK);
	signals.async_wait(
		[&ioc](boost::system::error_code ec, int sig) {
			ioc.stop();
		}
	);

	client_type client(ioc, "");

        // configure the client, subscribe and check if the subscription is successfully established

        receive_message(client);

        ioc.run();
}

However, if you are able to, I would recommend using coroutines for this case, as it leads to simpler and cleaner code. Our receiver example would be slightly modified to achieve the desired behaviour.

#include <boost/asio/as_tuple.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/asio/use_awaitable.hpp>

#include <boost/asio/ip/tcp.hpp>

#include <async_mqtt5.hpp>

namespace asio = boost::asio;
constexpr auto use_nothrow_awaitable = asio::as_tuple(asio::use_awaitable);

asio::awaitable<void> client_receiver(asio::io_context& ioc) {
	async_mqtt5::mqtt_client<asio::ip::tcp::socket> client(ioc, "");

        // configure client
	client.credentials("tester", "", "")
		.brokers("mqtt-broker", 1883)
		.run();

       // subscribe and check if the subscription is successful
	auto [ec, sub_codes, sub_props] = co_await client.async_subscribe(...);

       // if we failed to subscribe, co_return here

	for(;;) {
		auto [rec, topic, payload, publish_props] = co_await client.async_receive(use_nothrow_awaitable);

		if (rec == async_mqtt5::client::error::session_expired) {
			// resubscribe
		} else if (rec) {
			break;
		} else {
			// process message
		}
	}

	co_return;
}

int main() {
	asio::io_context ioc;

        // using signals to exit
	asio::signal_set signals(ioc, SIGINT, SIGTERM);
	signals.async_wait(
		[&ioc](boost::system::error_code ec, int sig) {
			ioc.stop();
		}
	);

	co_spawn(ioc, client_receiver(ioc), asio::detached);

	ioc.run();
}

If you have any further questions, feel free to ask!

I hope this helps,
Korina

from async-mqtt5.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.