Lux Docs

Build on Lux without guessing.

Lux Cloud + OSS core

Product docs for the Lux runtime: tables, cache, vectors, realtime, time series, auth, HTTP, SDK, CLI, and self-hosting.

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.

Syntax
SUBSCRIBE channel [channel ...]
Example
127.0.0.1:6379> SUBSCRIBE notifications alerts
1) "subscribe"
2) "notifications"
3) (integer) 1
1) "subscribe"
2) "alerts"
3) (integer) 2

UNSUBSCRIBE

Unsubscribe from one or more channels. If called with no arguments, unsubscribes from all channels.

Syntax
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.

Syntax
PSUBSCRIBE pattern [pattern ...]
Example
127.0.0.1:6379> PSUBSCRIBE events:*
1) "psubscribe"
2) "events:*"
3) (integer) 1

PUNSUBSCRIBE

Unsubscribe from one or more patterns. If called with no arguments, unsubscribes from all patterns.

Syntax
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.

Syntax
PUBLISH channel message
Example
127.0.0.1:6379> PUBLISH notifications '{"type":"order","id":1234}'
(integer) 3

Message Formats

Messages arrive in different formats depending on whether the subscription was made with SUBSCRIBE or PSUBSCRIBE.

Channel subscription (SUBSCRIBE)

Channel format
["message", channel, payload]
Delivered message
1) "message"
2) "notifications"
3) "{"type":"order","id":1234}"

Pattern subscription (PSUBSCRIBE)

Pattern format
["pmessage", pattern, channel, payload]
Delivered pattern message
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.

KSUBPub/Sub
TriggerImplicit: fires on any key mutation (SET, HSET, DEL, etc.)Explicit: only fires when a client calls PUBLISH
PayloadPattern, key name, and operation typeArbitrary message string
Best forReacting to state changes, cache invalidation, live dashboardsChat, notifications, inter-service messaging, broadcasting
Data awarenessKnows which key changed and howNo 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.