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