{"library":"node-redis-pubsub","title":"Node Redis PubSub (NRP)","description":"Node Redis Pubsub (NRP) is a JavaScript library designed to simplify the use of Redis's Pub/Sub functionality within Node.js applications. It abstracts away the complex raw Redis Pub/Sub API, providing a more intuitive, event emitter-like interface. NRP is particularly valuable for inter-application communication, allowing different Node.js instances or even other services to share data via a central Redis server, a capability not offered by Node's built-in EventEmitter. The library is currently at version 5.0.0 and demonstrates active maintenance, including recent updates to address non-JSON payload handling and significant API refactorings for subscription management. While its release cadence isn't explicitly defined, the project shows ongoing development and recent shifts in maintainership. Key differentiators include its `scope` option to prevent message collisions between different NRP instances, support for reusing existing Redis client connections, and robust error handling.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-redis-pubsub"],"cli":null},"imports":["import NRP from 'node-redis-pubsub';","const NRP = require('node-redis-pubsub');","import type { RedisClientOptions } from 'redis'; // for configuration types"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const NRP = require('node-redis-pubsub');\n\n// Configure NRP, connecting to a local Redis instance or using an environment variable\n// Ensure REDIS_URL is set or a local Redis server is running on 6379\nconst config = process.env.REDIS_URL ? { url: process.env.REDIS_URL } : {\n  port: 6379, // Default Redis port\n  host: '127.0.0.1',\n  scope: 'my-app-scope' // Isolate messages for this application\n};\n\nconst nrp = new NRP(config); // This is the NRP client instance\n\n// --- Simple PubSub example ---\n\n// Subscribe to 'say hello' messages\nnrp.on('say hello', (data) => {\n  console.log(`[Subscriber] Hello ${data?.name ?? 'Unknown'} from NRP!`);\n});\n\n// Subscribe to messages matching a pattern\nnrp.on('city:*', (data, channel) => {\n  console.log(`[Subscriber] Received city message on channel '${channel}': ${data?.city ?? 'Unknown city'} is a great city.`);\n});\n\n// Emit messages after a short delay to ensure listeners are registered\nsetTimeout(() => {\n  console.log('[Emitter] Emitting \"say hello\"');\n  nrp.emit('say hello', { name: 'Registry Agent' });\n\n  console.log('[Emitter] Emitting \"city:paris\"');\n  nrp.emit('city:paris', { city: 'Paris' });\n\n  console.log('[Emitter] Emitting \"city:london\"');\n  nrp.emit('city:london', { city: 'London' });\n\n  // Example of unsubscribing after a message\n  console.log('[Emitter] Setting up a one-time greeting');\n  const unsubscribeGreet = nrp.on('greet_once', (data) => {\n    console.log(`[Subscriber] Greeting once: ${data?.message ?? 'No message'}`);\n    unsubscribeGreet(); // Unsubscribe immediately after first message\n  });\n\n  console.log('[Emitter] Emitting \"greet_once\" (first time)');\n  nrp.emit('greet_once', { message: 'This message should only appear once.' });\n\n  console.log('[Emitter] Emitting \"greet_once\" (second time - should not be received)');\n  nrp.emit('greet_once', { message: 'This message should NOT appear.' });\n\n}, 500);\n\n// Listen for errors from the underlying Redis connections\nnrp.on(\"error\", (err) => {\n  console.error(\"NRP Error:\", err.message);\n});\n\n// Shut down connections safely after a period\nsetTimeout(() => {\n  console.log('Quitting NRP connections safely...');\n  nrp.quit();\n  console.log('NRP connections closed.');\n}, 2000);\n","lang":"javascript","description":"This quickstart demonstrates basic setup for Node Redis PubSub (NRP), including connecting to Redis, subscribing to specific events and patterns, emitting messages, unsubscribing from events, and proper error handling and connection shutdown. It simulates a simple producer-consumer scenario.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}