Skip to content
This repository has been archived by the owner on Jul 7, 2023. It is now read-only.

jcoreio/redis-subscriber

Repository files navigation

redis-subscriber

Build Status Coverage Status semantic-release Commitizen friendly

Subscribe specific callbacks to specific channels on the same redis client

Usage

import redis from 'redis'
import RedisSubscriber from '@jcoreio/redis-subscriber'

const client = redis.createClient()
const subscriber = new RedisSubscriber(client)

Subscribing

subscriber.subscribe('foo', (channel, message) => ...)
subscriber.psubscribe('foo/*', (pattern, channel, message) => ...)

Unsubscribing

There are two ways to unsubscribe:

  • call unsubscribe or punsubscribe
const onMessage = (channel, message) => ...
const onPMessage = (pattern, channel, message) => ...
subscriber.subscribe('foo', onMessage)
subscriber.psubscribe('foo/*', onPMessage)
...
subscriber.unsubscribe('foo', onMessage)
subscriber.unsubscribe('foo/*', onPMessage)
  • call the function returned by subscribe or psubscribe
const unsubscribe = subscriber.subscribe('foo', onMessage)
unsubscribe()
const unsubscribe = subscriber.psubscribe('foo/*', onPMessage)
unsubscribe()

parseMessage option

Parse all messages as JSON before sending them to callbacks:

const subscriber = new RedisSubscriber(client, {parseMessage: JSON.parse})

About

subscribe specific callbacks to specific channels on the same redis client

Resources

License

Stars

Watchers

Forks

Packages

No packages published