RedisX v0.9
A simple, light-weight Redis database client
Loading...
Searching...
No Matches
redisx-sub.c File Reference

Functions

int redisxAddSubscriber (Redis *redis, const char *channelStem, RedisSubscriberCall f)
 
int redisxClearSubscribers (Redis *redis)
 
int redisxEndSubscription (Redis *redis)
 
int redisxNotify (Redis *redis, const char *channel, const char *message)
 
int redisxPublish (Redis *redis, const char *channel, const char *data, int length)
 
int redisxPublishAsync (Redis *redis, const char *channel, const char *data, int length)
 
int redisxRemoveSubscribers (Redis *redis, RedisSubscriberCall f)
 
int redisxSubscribe (Redis *redis, const char *pattern)
 
int redisxUnsubscribe (Redis *redis, const char *pattern)
 

Detailed Description

Date
Created on Aug 26, 2024
Author
Attila Kovacs

PUB/SUB functions for the RedisX library.

Function Documentation

◆ redisxAddSubscriber()

int redisxAddSubscriber ( Redis redis,
const char *  channelStem,
RedisSubscriberCall  f 
)

Add a targeted subscriber processing function to the list of functions that process Redis PUB/SUB responses. You will still have to subscribe the relevant PUB/SUB messages from redis separately, using redisxSubscribe() before any messages are delivered to this client. If the subscriber with the same callback function and channel stem is already added, this call simply return and will NOT create a duplicate enry. However, the same callback may be added multiple times with different channel stems (which pre-filter what messages each of the callbacks may get).

Parameters
redisPointer to a Redis instance.
channelStemIf NULL, the consumer will receive all Redis messages published to the given channel. Otherwise, the consumer will be notified only if the incoming channel begins with the specified stem.
fA function that consumes subscription messages.
See also
redisxRemoveSubscribers()
redisxSubscribe()

References x_error(), X_NULL, X_SUCCESS, x_warn(), xStringCopyOf(), and xvprintf.

◆ redisxClearSubscribers()

int redisxClearSubscribers ( Redis redis)

Stops the custom consumption of PUB/SUB messages from Redis.

Parameters
redisPointer to a Redis instance.
Returns
X_SUCCESS (0) if successful, or X_NULL if the redis instance is NULL.

References x_error(), X_NULL, and xvprintf.

◆ redisxEndSubscription()

int redisxEndSubscription ( Redis redis)

Unsubscribes from all channels, stops the subscription listener thread, and closes the subscription client connection.

Parameters
redisPointer to a Redis instance.
Returns
X_SUCCESS (0) if successful or else an error code (<0).
See also
redisxUnsubscribe()

References x_error(), X_NULL, and X_SUCCESS.

◆ redisxNotify()

int redisxNotify ( Redis redis,
const char *  channel,
const char *  message 
)

Sends a regular string terminated Redis PUB/SUB message on the specified channel. Same as redisxPublish() with the length argument set to the length of the string message. Redis must be connected before attempting to send messages.

Parameters
redisPointer to a Redis instance.
channelRedis PUB/SUB channel on which to notify
messageMessage to send.
Returns
X_SUCCESS if the message was successfullt sent. X_NO_INIT if the Redis library was not initialized via initRedis(). X_NO_SERVICE if there was a connection problem. PARSE_ERROR if the Redis response could not be confirmed.
See also
redisxPublish()
redisxPublishAsync()
redisxSubscribe()

References redisxPublish(), and X_SUCCESS.

◆ redisxPublish()

int redisxPublish ( Redis redis,
const char *  channel,
const char *  data,
int  length 
)

Sends a generic Redis PUB/SUB message on the specified channel. Redis must be connected before attempting to send messages. It will send the message over the pipeline client if it is avaiable, or else over the interactive client.

Parameters
redisPointer to a Redis instance.
channelRedis PUB/SUB channel on which to notify
dataData to send.
lengthBytes of data to send, or 0 to determine automatically with strlen().
Returns
X_SUCCESS if the message was successfullt sent. X_NO_INIT if the Redis library was not initialized via initRedis(). X_NO_SERVICE if there was a connection problem. PARSE_ERROR if the Redis response could not be confirmed.
See also
redisxNotify()
redisxPublishAsync()
redisxSubscribe()

References Redis::id, Redis::interactive, redisxLockConnected(), redisxPublishAsync(), redisxUnlockClient(), x_error(), X_NULL, X_SUCCESS, and xvprintf.

◆ redisxPublishAsync()

int redisxPublishAsync ( Redis redis,
const char *  channel,
const char *  data,
int  length 
)

Sends a Redis notification asynchronously using the Redis "PUBLISH" command. The caller should have an exclusive lock on the interactive Redis channel before calling this.

Parameters
redisPointer to a Redis instance.
channelRedis PUB/SUB channel on which to notify
dataMessage body data.
lengthBytes of message data to send, ot 0 to determine automatically with strlen().
Returns
X_SUCCESS (0) if successful, or else X_NULL if the redis instance is NULL X_NAME_INVALID if the PUB/SUB channel is null or empty or an error code (<0) returned by redisxSendArrayRequestAsync().
See also
redisxPublish()
redisxNotify()

References Redis::interactive, redisxSendArrayRequestAsync(), redisxSkipReplyAsync(), x_error(), X_NULL, and X_SUCCESS.

◆ redisxRemoveSubscribers()

int redisxRemoveSubscribers ( Redis redis,
RedisSubscriberCall  f 
)

Removes all instances of a subscribe consumer function from the current list of consumers. This calls only deactivates the specified processing callback function(s), without stopping the delivery of associated messages. To stop Redis sending messages that are no longer being processed, you should also call redisxUnsubscribe() as appropriate.

Parameters
redisPointer to a Redis instance.
fThe consumer function to remove from the list of active subscribers.
Returns
The number of instances of f() that have been removed from the list of subscribers.
See also
redisxAddSubscriber()
redisxClearSubscribers()
redisxUnsubscrive()

References x_error(), X_NULL, and xvprintf.

◆ redisxSubscribe()

int redisxSubscribe ( Redis redis,
const char *  pattern 
)

Subscribe to a specific Redis channel. The call will also start the subscription listener thread to processing incoming subscription messages. Subscribing only enabled the delivery of the messages to this client without any actions on these messages. In order to process the messages for your subscriptons, you will also want to call redisxAddSubscriber() to add your custom processor function(s).

Parameters
redisPointer to a Redis instance.
patternThe Channel pattern to subscribe to, e.g. 'acc1', or 'acc*'...
Returns
X_SUCCESS if successfully subscribed to the Redis distribution channel. X_NO_SERVICE if there is no active connection to the Redis server. X_NULL if the channel argument is NULL
See also
redisxAddSubscriber()
redisxUnsubscribe()
redisxNotify()
redisxPublish()
redisxPublishAsync()

References redisxLockConnected(), redisxSendRequestAsync(), redisxUnlockClient(), Redis::subscription, x_error(), X_NULL, and X_SUCCESS.

◆ redisxUnsubscribe()

int redisxUnsubscribe ( Redis redis,
const char *  pattern 
)

Unsubscribe from one or all Redis PUB/SUB channel(s). If there are no active subscriptions when Redis confirms the unsubscrive command, the subscription listener thread will also conclude automatically. Unsubscribing will stop delivery of mesasages for the affected channels but any associated processing callbacks remain registered, until redisxRemovesubscribers() is called to deactive them as appropriate.

Parameters
redisPointer to a Redis instance.
patternThe channel pattern, or NULL to unsubscribe all channels and patterns.
Returns
X_SUCCESS if successfully subscribed to the Redis distribution channel. X_NO_SERVICE if there is no active connection to the Redis server.
See also
redisxSubscribe()
redisxEndSubscribe()
redisxRemoveSubscribers()

References redisxLockConnected(), redisxSendRequestAsync(), redisxUnlockClient(), Redis::subscription, x_error(), X_NULL, and X_SUCCESS.