Publish/Subscribe

Use Redis as a complete real-time messaging broker

What is Pub/Sub?

https://dingyuliang.me/wp-content/uploads/2018/02/redis-pubsub-768x407.png

We have introduced the capability for your CFML code to now leverage Redis Publish and Subscribe constructs. This will allow your CFML code to have native messaging via Redis.

Documentation

Redis Pub/Sub implements the Publish/Subscribe messaging paradigm. This decoupling of publishers and subscribers can allow for greater scalability and a more dynamic network topology.

  1. Subscribers express interest in one or more channels (literal channels or pattern channels).

  2. Publishers send messages into channels.

  3. Redis will push these messages into different subscribers which have matched the channel's message.

Pub/Sub Functions

In order to leverage this pattern you will use the following two functions:

Function
Description

RedisPublish( channel, message, cacheName )

Publish a message into Redis into a specific channel. Returns how many subscribers are listening to the message and channel.

RedisSubscribe( subscriber, channels, cacheName )

Subscribe to a channel for messages using a closure/lambda or a listener CFC. Returns a future and a subscriber.

Publish Example

The return of the redisPublish() function is numeric indicating how many subscribers received the message.

Subscribe Example

The redisSubscribe() function returns a struct with two keys in it:

  • future - A completable future that is running your subscriber.

  • subscriber - The Java Redis subscriber object you can use to inspect the subscription or unsubscribe from the calls via the unsubscribe() method.

Subscriber Listener CFC

If you will be passing a CFC to the redisSubscribe() methods then you will need to implement some or all of the following functions.

Last updated

Was this helpful?