1.0.0 • Published 4 years ago

@hearit-io/redis-channel v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

hearit.io

Smart home automatization designed for visually impaired people.

redis-channel

A fast, reliable, and scalable pub/sub implementation for IoT applications based on Redis streams. Applicable for all pub/sub use cases.

Can be used with a single Redis instance and later updated easily to a cluster configuration without need of any application change.

Simple integration in a web frameworks, already available plug-in for our favorite Fastify (@hearit-io/fastify-redis-channel coming soon!).

Do you want to grow? Then start right from the begging.

Install

$ npm install @hearit-io/redis-channel

Usage example

'use strict'

const {Redis} = require('ioredis')
const {RedisChannel} = require('@hearit-io/redis-channel')

// Redis clients needed for pub/sub operations.
const pub = new Redis();
const sub = new Redis();

const options = {
  // The three properties are used in the Redis keys as a prefix.
  application = 'app',
  version = 1,
  schema = 'channels',
  // Limits the size of the channel to a maximum number of elements.
  overflow = 100,
  // A fixed pre-sharded slots, possible values 32 (default) or 64. 
  slots = 32,
  // To use sharded channels or not.
  sharded = true  
}

const channel = new RedisChannel (sub, pub, options)

async function main () {
  try {
    const tunnel = await channel.use('room')
  
    // Implement your consumer
    async function consume(tunnel) {
      for await (const messages of channel.consume(tunnel)) {
        for (const i in messages) {
          // Do something with messages
          console.log(messages[i])
        }
      }
    }

    // Subscribe your tunnel
    await channel.subscribe(tunnel)

    // Start consuming 
    consume(tunnel).catch((error) => {
      console.error(error)
    })

    // Produce a message to your tunnel
    await channel.produce(tunnel, 'Hello Wold!')

    // Unsubscribe your tunnel from the channel
    await channel.unsubscribe(tunnel)

    // Delete your room if there are no more consumers/producers
    await channel.delete('room')
  }
  catch (error) {
    console.error(error)
  }
}
main()

Running tests

We commit to deliver the highest possible quality to our valuable open source community by implementing a 100 % test coverage for all packages.

To run the test cases a running single Redis instance or Cluster is required. In the environment variable REDIS_NODES you can set a list of Redis nodes separated by space as example:

export REDIS_NODES="127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382"

If there are more than one entry in the list a connection to a Redis Cluster will be used for the tests. If the REDIS_NODES is not set a connection to 127.0.0.1:6379 will be tried.

After each test, the data base will be FLUSHED. Please make sure there is no valuable data in the data base before running the tests!

Start the test with:

npm test