-
Notifications
You must be signed in to change notification settings - Fork 555
Redis Subscriber
The cpp_redis::redis_subscriber
is meant to be used for PUB/SUB communication with the Redis server.
Please do not use cpp_redis::redis_client
to subscribe to some Redis channels as:
- the behavior is undefined
-
cpp_redis::redis_client
is not meant for that
void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379, const disconnection_handler& handler = nullptr)
Description
Connects to the Redis Server. The connection is done synchronously. Throws redis_error in the case of a failure or if client if already connected.
Also, sets the disconnection handler which is called whenever a disconnection has occurred.
Disconnection handler is an std::function<void(redis_subscriber&)>
.
Parameters
Type | Name | Description |
---|---|---|
const std::string& | host | Host to be connected to |
unsigned int | port | Port of the hot to be connected to |
const disconnection_handler& | handler | Callback to be called on client disconnection |
Return Value
None.
void disconnect(void)
Disconnects client from remote host. Throws redis_error if the client is not connected to any server.
bool is_connected(void)
Returns whether the client is connected or not.
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 in this channel.
subscribe_callback_t
is an std::function<void(const std::string& channel, const std::string& message)>
.
acknowledgement_callback_t
is an std::function<void(int64_t nb_channel_subscribed)>
.
The command is not effectively sent immediately, but stored inside an internal buffer until commit()
is called.
redis_subscriber& psubscribe(const std::string& pattern, const subscribe_callback_t& callback, const acknowledgement_callback_t& acknowledgement_callback = nullptr)
PSubscribes to the given pattern and:
- calls acknowledgement_callback once the server has acknowledged about the subscription.
- calls subscribe_callback each time a message is published in a channel matching the pattern.
subscribe_callback_t
is an std::function<void(const std::string& channel, const std::string& message)>
.
acknowledgement_callback_t
is an std::function<void(int64_t nb_channel_subscribed)>
.
The command is not effectively sent immediately, but stored inside an internal buffer until commit()
is called.
redis_subscriber& unsubscribe(const std::string& channel)
Unsubscribes from the given channel.
The command is not effectively sent immediately, but stored inside an internal buffer until commit()
is called.
redis_subscriber& punsubscribe(const std::string& pattern)
Unsubscribes from the given pattern.
The command is not effectively sent immediately, but stored inside an internal buffer until commit()
is called.
redis_subscriber& commit(void)
Sends all the commands that have been stored by calling send()
since the last commit()
call to the Redis server.
That is, pipelining is supported in a very simple and efficient way: sub.subscribe(...).psubscribe(...).unsubscribe(...).commit()
will send the 3 commands at once (instead of sending 3 network requests, one for each command, as it would have been done without pipelining).
Pipelined commands are always
removed from the buffer, even in case of error (for example, calling commit
while the client is not connected, something that throws an exception).
Need more information? Contact me.