Examples for using redis-mesagging-manager library.
Clone the repository, and run npm install
git clone https://github.com/tomyitav/redis-messaging-manager-examples.git
npm install
tsc
Make sure a redis broker instance is up on localhost. You can configure a different ip address in messenger.ts file
import {PubsubManager} from 'redis-messaging-manager';
let messenger = new PubsubManager({
host: 'localhost'
});
export default messenger;
The following code snippet shows how to subscribe to a topic, (or channel) named "redis"
import messenger from "../messenger";
console.log('consuming messages..!');
messenger.consume('redis')
.subscribe(msg => {
console.log('Got message- ', msg);
});
Run command:
node dist\basic-pubsub\receiver.js
This is how we publish a message on the channel "redis":
import messenger from '../messenger';
messenger.publish('redis', 'Hello redis messenger!');
Run command:
node dist\basic-pubsub\publisher.js
And the message will now be published to the receiver.
Publishing multiple messages on a channel is supported in redis-messaging-manager
by using publishBulk
. This is implemented by using Redis Pipeline
functionality to boost performance.
import messenger from '../messenger';
let bulkOfMessages: Array<string> = [
"hello redis 1",
"hello redis 2",
"hello redis 3",
"hello redis 4",
"hello redis 5"
]
messenger.publishBulk('redis', bulkOfMessages);
Run command:
node dist\basic-pubsub\publish-bulk.js
Now all 5 Messages from the publisher will appear in the receiver console.
By using the getServerEventStream
method, we get an Observable that
acts as a stream of events from the Redis server instance.
import messenger from '../messenger';
messenger.getServerEventStream('error')
.subscribe(() => {
console.log('Got error event');
})
Run command:
node dist\events\event-listener.js