Last active
November 24, 2020 02:29
-
-
Save ryanthegiantlion/1dfb5350e2405c04438b0f4f6e71d3eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// setup | |
1. if you want the worker to be in a separate project and separately deployed: heroku create <projectname> | |
2. if separate project created in step 1: heroku addons:attach <addonname> | |
// subscriber | |
var redis = require('redis'); | |
var RedisChannels = require('./constants/redisChannels') | |
if (process.env.REDIS_URL) { | |
var redisSub = redis.createClient(process.env.REDIS_URL) | |
var redisPub = redis.createClient(process.env.REDIS_URL) | |
} | |
else { | |
var redisSub = redis.createClient() | |
var redisPub = redis.createClient() | |
} | |
redisSub.subscribe(RedisChannels.SmartBotMessage) | |
redisSub.on('message', onMessage) | |
function onMessage(channel, data) { | |
// make api call etc | |
// switch on channel if you have multiple subscriptions | |
} | |
// publisher | |
var redis = require('redis'); | |
var RedisChannels = require('./constants/redisChannels') | |
if (process.env.REDIS_URL) { | |
var redisPub = redis.createClient(process.env.REDIS_URL); | |
} else { | |
var redisPub = redis.createClient(); | |
} | |
redisPub.publish(RedisChannels.BotMessage, JSON.stringify(data)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment