fast and simple pubsub using redis and cloud pub/sub
-
Using
busType
const { FastBus, BusType } = require('../lib'); // redis const redisOpts = { prefix: 'bus', redis: { host: 'localhost', port: 6379, db: 0 } }; const redisBus = FastBus.create({ fastBusOpts: redisOpts, busType: BusType.REDIS }); // cloud pub/sub const gcpOptions = { clientConfig: { projectId: 'project-test', apiEndpoint: 'localhost:8085' }, topicPrefix: 'topic-', subscriptionPrefix: 'sub-', }; const cloudPubSubBus = FastBus.create({ fastBusOpts: gcpOptions, busType: BusType.CLOUD_PUBSUB });
-
Using env
env setting
-
Redis setting:
process.env.FASTBUS_BACKEND=redis
export FASTBUS_BACKEND="redis"
-
Cloud PubSub setting:
process.env.FASTBUS_BACKEND=gcp_pubsub
export FASTBUS_BACKEND="gcp_pubsub"
source code
const { FastBus } = require('../lib'); // redis const redisOpts = { prefix: 'bus', redis: { host: 'localhost', port: 6379, db: 0 } }; const redisBus = FastBus.create({ fastBusOpts: redisOpts }); // cloud pub/sub const gcpOptions = { clientConfig: { projectId: 'project-test', apiEndpoint: 'localhost:8085' }, topicPrefix: 'topic-', subscriptionPrefix: 'sub-', }; const cloudPubSubBus = FastBus.create({ fastBusOpts: gcpOptions });
-
-
Inject create client method in
opts
parameterconst { FastBus } = require('../lib'); // redis const createRedisClient = new RedisClient(opts); const redisOpts = { createRedisClient, prefix: 'bus', redis: { host: 'localhost', port: 6379, db: 0 } }; const redisBus = FastBus.create({ fastBusOpts: redisOpts }); // cloud pub/sub const createPubSubClient = () => new PubSub(opts); const gcpOptions = { createPubSubClient, clientConfig: { projectId: 'project-test', apiEndpoint: 'localhost:8085' }, topicPrefix: 'topic-', subscriptionPrefix: 'sub-', }; const cloudPubSubBus = FastBus.create({ fastBusOpts: gcpOptions });
const { FastBus, BusType } = require('../lib');
// redis
const opts = { prefix: 'bus', redis: { host: 'localhost', port: 6379, db: 0 } };
const bus = FastBus.create({ fastBusOpts: opts, busType: BusType.REDIS });
bus.subscribe('greeting', (message) => console.log('hello', message));
bus.subscribe('greeting', (message) => console.log('hi', message));
bus.publish('greeting', 'there');
// hello, there
bus.publish('greeting', 'everyone', true);
// hello, everyone
// hi, everyone
$ npm test
$ npm run build
$ npm start
may the SOURCE be with you...