Pub/Sub
Lux supports Redis-compatible publish/subscribe messaging. Clients can subscribe to named channels or glob-style patterns, and any client can publish messages to a channel. Messages are delivered to all matching subscribers in realtime. Pub/Sub in Lux works with any existing Redis client library.
SUBSCRIBE
Subscribe to one or more named channels. The connection enters subscriber mode and will receive messages published to any of the subscribed channels.
SUBSCRIBE channel [channel ...]127.0.0.1:6379> SUBSCRIBE notifications alerts
1) "subscribe"
2) "notifications"
3) (integer) 1
1) "subscribe"
2) "alerts"
3) (integer) 2UNSUBSCRIBE
Unsubscribe from one or more channels. If called with no arguments, unsubscribes from all channels.
UNSUBSCRIBE [channel ...]PSUBSCRIBE
Subscribe to channels matching one or more glob-style patterns. * matches any
sequence of characters, ? matches a single
character, and [abc] matches any
character in the set.
PSUBSCRIBE pattern [pattern ...]127.0.0.1:6379> PSUBSCRIBE events:*
1) "psubscribe"
2) "events:*"
3) (integer) 1PUNSUBSCRIBE
Unsubscribe from one or more patterns. If called with no arguments, unsubscribes from all patterns.
PUNSUBSCRIBE [pattern ...]PUBLISH
Publish a message to a channel. Returns the number of clients that received the message. This includes both clients subscribed directly to the channel via SUBSCRIBE and clients whose PSUBSCRIBE patterns match the channel name.
PUBLISH channel message127.0.0.1:6379> PUBLISH notifications '{"type":"order","id":1234}'
(integer) 3Message Formats
Messages arrive in different formats depending on whether the subscription was made with SUBSCRIBE or PSUBSCRIBE.
Channel subscription (SUBSCRIBE)
["message", channel, payload]1) "message"
2) "notifications"
3) "{"type":"order","id":1234}"Pattern subscription (PSUBSCRIBE)
["pmessage", pattern, channel, payload]1) "pmessage"
2) "events:*"
3) "events:orders"
4) "{"type":"order","id":1234}"Example: Basic Pub/Sub
A typical pattern uses two connections: one for subscribing and one for publishing. The subscriber connection enters a special mode where it can only receive messages and manage subscriptions.
Client A (subscriber)
127.0.0.1:6379> SUBSCRIBE chat:general 1) "subscribe" 2) "chat:general" 3) (integer) 1 ... waiting for messages ... 1) "message" 2) "chat:general" 3) "hello from client B"
Client B (publisher)
127.0.0.1:6379> PUBLISH chat:general "hello from client B" (integer) 1
KSUB vs Pub/Sub
Lux has two realtime messaging systems that complement each other. Choosing the right one depends on whether you are reacting to data changes or sending explicit messages.
| KSUB | Pub/Sub | |
|---|---|---|
| Trigger | Implicit: fires on any key mutation (SET, HSET, DEL, etc.) | Explicit: only fires when a client calls PUBLISH |
| Payload | Pattern, key name, and operation type | Arbitrary message string |
| Best for | Reacting to state changes, cache invalidation, live dashboards | Chat, notifications, inter-service messaging, broadcasting |
| Data awareness | Knows which key changed and how | No awareness of stored data |
Use KSUB when you want to be notified about data changes without modifying the code that writes the data. Use Pub/Sub when you need to send messages between clients that are not tied to key mutations.