| cpp_redis 4.0.0 cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client, with support for synchronous operations and pipelining.

Classes | Public Types | Public Member Functions | List of all members
cpp_redis::subscriber Class Reference

`#include <subscriber.hpp>`

## Public Types
enum | connect_state { dropped, start, sleeping, ok, failed, lookup_failed, stopped }
typedef std::function< void(const std::string &host, std::size_t port, connect_state status)> | connect_callback_t
typedef std::function< void(reply &)> | reply_callback_t
typedef std::function< void(const std::string &, const std::string &)> | subscribe_callback_t
typedef std::function< void(int64_t)> | acknowledgement_callback_t

## Public Member Functions
| subscriber (void)
| ctor
| subscriber (const std::shared_ptr< network::tcp_client_iface > &tcp_client)
| ~subscriber (void)
| dtor
| subscriber (const subscriber &)=delete
| copy ctor
subscriber & | operator= (const subscriber &)=delete
| assignment operator
void | connect (const std::string &host="127.0.0.1", std::size_t port=6379, const connect_callback_t &connect_callback=nullptr, std::uint32_t timeout_msecs=0, std::int32_t max_reconnects=0, std::uint32_t reconnect_interval_msecs=0)
void | connect (const std::string &name, const connect_callback_t &connect_callback=nullptr, std::uint32_t timeout_msecs=0, std::int32_t max_reconnects=0, std::uint32_t reconnect_interval_msecs=0)
bool | is_connected (void) const
void | disconnect (bool wait_for_removal=false)
bool | is_reconnecting (void) const
void | cancel_reconnect (void)
subscriber & | auth (const std::string &password, const reply_callback_t &reply_callback=nullptr)
subscriber & | subscribe (const std::string &channel, const subscribe_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr)
subscriber & | psubscribe (const std::string &pattern, const subscribe_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr)
subscriber & | unsubscribe (const std::string &channel)
subscriber & | punsubscribe (const std::string &pattern)
subscriber & | commit (void)
void | add_sentinel (const std::string &host, std::size_t port, std::uint32_t timeout_msecs=0)
const sentinel & | get_sentinel (void) const
sentinel & | get_sentinel (void)
void | clear_sentinels (void)

## Detailed Description

The cpp_redis::subscriber is meant to be used for PUB/SUB communication with the Redis server. Please do not use cpp_redis::client to subscribe to some Redis channels as:

- the behavior is undefined
- cpp_redis::client is not meant for that

## Member Typedef Documentation

## ◆ acknowledgement_callback_t

typedef std::function<void(int64_t)> cpp_redis::subscriber::acknowledgement_callback_t

acknowledgement callback called whenever a subscribe completes takes as parameter the int returned by the redis server (usually the number of channels you are subscribed to)

## ◆ connect_callback_t

typedef std::function<void(const std::string& host, std::size_t port, connect_state status)> cpp_redis::subscriber::connect_callback_t

connect handler, called whenever a new connection even occurred

## ◆ reply_callback_t

typedef std::function<void(reply&)> cpp_redis::subscriber::reply_callback_t

reply callback called whenever a reply is received takes as parameter the received reply

## ◆ subscribe_callback_t

typedef std::function<void(const std::string&, const std::string&)> cpp_redis::subscriber::subscribe_callback_t

subscribe callback, called whenever a new message is published on a subscribed channel takes as parameter the channel and the message

## Member Enumeration Documentation

## ◆ connect_state

enum cpp_redis::subscriber::connect_state
| strong

high availability (re)connection states

- dropped: connection has dropped
- start: attemp of connection has started
- sleeping: sleep between two attemps
- ok: connected
- failed: failed to connect
- lookup failed: failed to retrieve master sentinel
- stopped: stop to try to reconnect

## Constructor & Destructor Documentation

## ◆ subscriber()

cpp_redis::subscriber::subscriber | ( | const std::shared_ptr< network::tcp_client_iface > & | tcp_client | ) |
| explicit

custom ctor to specify custom tcp_client

Parameters

  tcp_client | tcp client to be used for network communications

## Member Function Documentation

## ◆ add_sentinel()

void cpp_redis::subscriber::add_sentinel | ( | const std::string & | host,
|  | std::size_t | port,
|  | std::uint32_t | timeout_msecs = `0`
| ) |  |

add a sentinel definition. Required for connect() or get_master_addr_by_name() when autoconnect is enabled.

Parameters

  host | sentinel host
  port | sentinel port
  timeout_msecs | maximum time to connect

## ◆ auth()

subscriber& cpp_redis::subscriber::auth | ( | const std::string & | password,
|  | const reply_callback_t & | reply_callback = `nullptr`
| ) |  |

ability to authenticate on the redis server if necessary this method should not be called repeatedly as the storage of reply_callback is NOT threadsafe (only one reply callback is stored for the subscriber client) calling repeatedly auth() is undefined concerning the execution of the associated callbacks

Parameters

  password | password to be used for authentication
  reply_callback | callback to be called on auth completion (nullable)

Returns
  current instance

## ◆ cancel_reconnect()

void cpp_redis::subscriber::cancel_reconnect | ( | void |  | ) |

stop any reconnect in progress

## ◆ clear_sentinels()

void cpp_redis::subscriber::clear_sentinels | ( | void |  | ) |

clear all existing sentinels.

## ◆ commit()

subscriber& cpp_redis::subscriber::commit | ( | void |  | ) |

commit pipelined transaction that is, send to the network all commands pipelined by calling send() / subscribe() / ...

Returns
  current instance

## ◆ connect() [1/2]

void cpp_redis::subscriber::connect | ( | const std::string & | host = `"127.0.0.1"`,
|  | std::size_t | port = `6379`,
|  | const connect_callback_t & | connect_callback = `nullptr`,
|  | std::uint32_t | timeout_msecs = `0`,
|  | std::int32_t | max_reconnects = `0`,
|  | std::uint32_t | reconnect_interval_msecs = `0`
| ) |  |

Connect to redis server

Parameters

  host | host to be connected to
  port | port to be connected to
  connect_callback | connect handler to be called on connect events (may be null)
  timeout_msecs | maximum time to connect
  max_reconnects | maximum attemps of reconnection if connection dropped
  reconnect_interval_msecs | time between two attemps of reconnection

## ◆ connect() [2/2]

void cpp_redis::subscriber::connect | ( | const std::string & | name,
|  | const connect_callback_t & | connect_callback = `nullptr`,
|  | std::uint32_t | timeout_msecs = `0`,
|  | std::int32_t | max_reconnects = `0`,
|  | std::uint32_t | reconnect_interval_msecs = `0`
| ) |  |

Connect to redis server

Parameters

  name | sentinel name
  connect_callback | connect handler to be called on connect events (may be null)
  timeout_msecs | maximum time to connect
  max_reconnects | maximum attemps of reconnection if connection dropped
  reconnect_interval_msecs | time between two attemps of reconnection

## ◆ disconnect()

void cpp_redis::subscriber::disconnect | ( | bool | wait_for_removal = `false` | ) |

disconnect from redis server

Parameters

  wait_for_removal | when sets to true, disconnect blocks until the underlying TCP client has been effectively removed from the io_service and that all the underlying callbacks have completed.

## ◆ get_sentinel() [1/2]

const sentinel& cpp_redis::subscriber::get_sentinel | ( | void |  | ) | const

retrieve sentinel for current client

Returns
  sentinel associated to current client

## ◆ get_sentinel() [2/2]

sentinel& cpp_redis::subscriber::get_sentinel | ( | void |  | ) |

retrieve sentinel for current client non-const version

Returns
  sentinel associated to current client

## ◆ is_connected()

bool cpp_redis::subscriber::is_connected | ( | void |  | ) | const

Returns
  whether we are connected to the redis server

## ◆ is_reconnecting()

bool cpp_redis::subscriber::is_reconnecting | ( | void |  | ) | const

Returns
  whether an attemp to reconnect is in progress

## ◆ psubscribe()

subscriber& cpp_redis::subscriber::psubscribe | ( | const std::string & | pattern,
|  | const subscribe_callback_t & | callback,
|  | const acknowledgement_callback_t & | acknowledgement_callback = `nullptr`
| ) |  |

PSubscribes to the given channel and:

- calls acknowledgement_callback once the server has acknowledged about the subscription.
- calls subscribe_callback each time a message is published on this channel. The command is not effectively sent immediately but stored in an internal buffer until commit() is called.

Parameters

  pattern | pattern to psubscribe
  callback | callback to be called whenever a message is received for this pattern
  acknowledgement_callback | callback to be called on subscription completion (nullable)

Returns
  current instance

## ◆ punsubscribe()

subscriber& cpp_redis::subscriber::punsubscribe | ( | const std::string & | pattern | ) |

punsubscribe from the given pattern The command is not effectively sent immediately, but stored inside an internal buffer until commit() is called.

Parameters

  pattern | pattern to punsubscribe from

Returns
  current instance

## ◆ subscribe()

subscriber& cpp_redis::subscriber::subscribe | ( | const std::string & | channel,
|  | const subscribe_callback_t & | callback,
|  | const acknowledgement_callback_t & | acknowledgement_callback = `nullptr`
| ) |  |

Subscribes to the given channel and:

- calls acknowledgement_callback once the server has acknowledged about the subscription.
- calls subscribe_callback each time a message is published on this channel. The command is not effectively sent immediately but stored in an internal buffer until commit() is called.

Parameters

  channel | channel to subscribe
  callback | callback to be called whenever a message is received for this channel
  acknowledgement_callback | callback to be called on subscription completion (nullable)

Returns
  current instance

## ◆ unsubscribe()

subscriber& cpp_redis::subscriber::unsubscribe | ( | const std::string & | channel | ) |

unsubscribe from the given channel The command is not effectively sent immediately, but stored inside an internal buffer until commit() is called.

Parameters

  channel | channel to unsubscribe from

Returns
  current instance

The documentation for this class was generated from the following file:

- includes/cpp_redis/core/subscriber.hpp