@jcoreio/redis-subscriber

subscribe specific callbacks to specific channels on the same redis client

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@jcoreio/redis-subscriber
001.0.65 years ago6 years agoMinified + gzip package size for @jcoreio/redis-subscriber in KB

Readme

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})