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

Message Poller #109

Closed
elia-sysdesign opened this issue Dec 30, 2023 · 3 comments
Closed

Message Poller #109

elia-sysdesign opened this issue Dec 30, 2023 · 3 comments

Comments

@elia-sysdesign
Copy link

Hello,

Sorry for the intrusion.

Just wondering what would be the best way to implement a polling mechanism (after having subscribed to a topic) that would trigger a callback whenever a new message is received.
So far I only found examples that would receive a single message and then disconnect from the broker.
I need to implement an endpoint that would maintain a connection with the broker and that would permanently subscribe to a specific topic and keep receiving messages on said topic.

Thanks in advance.
Elia

@redboltz
Copy link
Owner

redboltz commented Dec 31, 2023

You can call the next recv() to get meeage().
Here is client_cli.cpp code:

// Recv loop
while (true) {
yield ep_.recv(*this);
if (pv) {
pv.visit(
am::overload {
[&](am::v3_1_1::publish_packet const& p) {
std::cout << color_recv;
std::cout << p << std::endl;
std::cout << " payload:";
for (auto const& e : p.payload()) {
std::cout << am::json_like_out(e);
}
std::cout << std::endl;
std::cout << color_none;
},
[&](am::v5::publish_packet const& p) {
std::cout << color_recv;
std::cout << p << std::endl;
std::cout << " payload:";
for (auto const& e : p.payload()) {
std::cout << am::json_like_out(e);
}
std::cout << std::endl;
std::cout << color_none;
},
[](auto const& p) {
std::cout << color_recv;
std::cout << p << std::endl;
std::cout << color_none;
}
}
);
}
else {
std::cout << color_recv;
std::cout
<< "recv error:"
<< pv.get<am::system_error>().what()
<< std::endl;
std::cout << color_none;
return;
}
}

It uses stackless coroutine. If you use callback approach, then call recv() the last part of recv() callback instead of the loop.

NOTE: It is not a polling, it is still event driven.

@redboltz
Copy link
Owner

redboltz commented Jan 5, 2024

If you still have the problem, feel free to reopen the issue.

@redboltz redboltz closed this as completed Jan 5, 2024
@elia-sysdesign
Copy link
Author

Thank you for the quick response.
I will give it a go and let you know.

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