Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@fahreddinozcan
Copy link
Contributor

@fahreddinozcan fahreddinozcan commented Feb 18, 2025

This PR introduces HTTP based SUBSCRIBE and PSUBSCRIBE commands that uses SSE.

Regular subscription:

await redis.subscribe(["chat"])

Pattern subscription:

await redis.psubscribe(["chat:*"])

The example usage pattern is as below.

import { Redis } from "@upstash/redis"

// Initialize Redis client
const redis = new Redis({
  url: 'UPSTASH_REDIS_REST_URL',
  token: 'UPSTASH_REDIS_REST_TOKEN',
})

// Regular Subscribe
const subscriber = redis.subscribe(["channel1", "channel2"])

// Pattern Subscribe
const patternSubscriber = redis.psubscribe(["user:*", "chat:*"])

// Listen to regular messages
subscriber.on("message", (message) => {
  console.log("Received message:", message)
})

// Listen to pattern matches
patternSubscriber.on("pmessage", (message) => {
  console.log("Pattern matched message:", message)
})

// Listen with channel/pattern info
patternSubscriber.on("pmessageBuffer", ({ pattern, channel, message }) => {
  console.log(`Pattern ${pattern} matched on ${channel}:`, message)
})

// Listen to specific channel/pattern messages
subscriber.on("message:channel1", (message) => {
  console.log("Channel1 message:", message)
})
patternSubscriber.on("pmessage:user:*", ({ channel, message }) => {
  console.log(`User message on ${channel}:`, message)
})

// Publish messages
await redis.publish("channel1", { text: "Hello World!" })
await redis.publish("user:123", { text: "Hello User!" })

// Unsubscribe from specific channels/patterns
await subscriber.unsubscribe(["channel1"])
await patternSubscriber.unsubscribe(["user:*"])

// Unsubscribe from all
await subscriber.unsubscribe()
await patternSubscriber.unsubscribe()

// Check current subscriptions
const channels = subscriber.getSubscribedChannels()      // ["channel2"]
const patterns = patternSubscriber.getSubscribedChannels() // ["chat:*"]

@linear
Copy link

linear bot commented Feb 18, 2025

@fahreddinozcan fahreddinozcan changed the title introduce SUBSCRIBE command Introduce SUBSCRIBE Command Feb 18, 2025
@fahreddinozcan fahreddinozcan marked this pull request as ready for review February 20, 2025 09:11
@CahidArda
Copy link
Contributor

there are errors in ci

Run Example step of node:

import { EventEmitter } from "node:stream";

Start example of cloudflare:

ervice core:user:upstash-redis: Uncaught Error: No such module "node:stream".

@fahreddinozcan fahreddinozcan changed the title Introduce SUBSCRIBE Command Introduce SUBSCRIBE and PSUBSCRIBE Command Feb 20, 2025
@CahidArda CahidArda merged commit 9846a4b into main Mar 3, 2025
14 checks passed
@CahidArda CahidArda deleted the DX-1299 branch March 3, 2025 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants