Boost.Redis: example/cpp20_subscriber.cpp Source File

Boost.Redis 1.84.0 A redis client library

Loading...
Searching...
No Matches
cpp20_subscriber.cpp
1/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
2 *
3 * Distributed under the Boost Software License, Version 1.0. (See
4 * accompanying file LICENSE.txt)
5 */
6
7#include <boost/redis/connection.hpp>
8#include <boost/redis/logger.hpp>
9#include <boost/asio/awaitable.hpp>
10#include <boost/asio/use_awaitable.hpp>
11#include <boost/asio/co_spawn.hpp>
12#include <boost/asio/detached.hpp>
13#include <boost/asio/consign.hpp>
14#include <boost/asio/redirect_error.hpp>
15#include <boost/asio/signal_set.hpp>
16#include <iostream>
17
18#if defined(BOOST_ASIO_HAS_CO_AWAIT)
19
20namespace asio = boost::asio;
21using namespace std::chrono_literals;
22using boost::redis::request;
23using boost::redis::generic_response;
24using boost::redis::consume_one;
25using boost::redis::logger;
26using boost::redis::config;
27using boost::redis::ignore;
28using boost::redis::error;
29using boost::system::error_code;
30using boost::redis::connection;
31using asio::signal_set;
32
33/* This example will subscribe and read pushes indefinitely.
34 *
35 * To test send messages with redis-cli
36 *
37 * $ redis-cli -3
38 * 127.0.0.1:6379> PUBLISH channel some-message
39 * (integer) 3
40 * 127.0.0.1:6379>
41 *
42 * To test reconnection try, for example, to close all clients currently
43 * connected to the Redis instance
44 *
45 * $ redis-cli
46 * > CLIENT kill TYPE pubsub
47 */
48
49// Receives server pushes.
50auto
51receiver(std::shared_ptr<connection> conn) -> asio::awaitable<void>
52{
53 request req;
54 req.push("SUBSCRIBE", "channel");
55
56 generic_response resp;
57 conn->set_receive_response(resp);
58
59 // Loop while reconnection is enabled
60 while (conn->will_reconnect()) {
61
62 // Reconnect to the channels.
63 co_await conn->async_exec(req, ignore);
64
65 // Loop reading Redis pushs messages.
66 for (error_code ec;;) {
67 // First tries to read any buffered pushes.
68 conn->receive(ec);
69 if (ec == error::sync_receive_push_failed) {
70 ec = {};
71 co_await conn->async_receive(asio::redirect_error(asio::use_awaitable, ec));
72 }
73
74 if (ec)
75 break; // Connection lost, break so we can reconnect to channels.
76
77 std::cout
78 << resp.value().at(1).value
79 << " " << resp.value().at(2).value
80 << " " << resp.value().at(3).value
81 << std::endl;
82
83 consume_one(resp);
84 }
85 }
86}
87
88auto co_main(config cfg) -> asio::awaitable<void>
89{
90 auto ex = co_await asio::this_coro::executor;
91 auto conn = std::make_shared<connection>(ex);
92 asio::co_spawn(ex, receiver(conn), asio::detached);
93 conn->async_run(cfg, {}, asio::consign(asio::detached, conn));
94
95 signal_set sig_set(ex, SIGINT, SIGTERM);
96 co_await sig_set.async_wait();
97
98 conn->cancel();
99}
100
101#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
boost::redis::connection
A basic_connection that type erases the executor.
Definition connection.hpp:1231
boost::redis::logger
Logger class.
Definition logger.hpp:27
boost::redis::request
Creates Redis requests.
Definition request.hpp:46
boost::redis::error
error
Generic errors.
Definition error.hpp:18
boost::redis::ignore
ignore_t ignore
Global ignore object.
boost::redis::generic_response
adapter::result< std::vector< resp3::node > > generic_response
A generic response to a request.
Definition response.hpp:35
boost::redis::config
Configure parameters used by the connection classes.
Definition config.hpp:30