Using RedisCloud on Heroku with node.js to allow (future) scaling to more than one dyno.
Got Redis working by following this:
https://devcenter.heroku.com/articles/rediscloud#using-redis-from-node-js
For example the line below with the comment "Prints 'bar'" does write 'bar' to the console.
Then followed this to add socket.io-redis for scaling:
https://github.com/Automattic/socket.io-redis
The above should allow me to use something like the following:
io.adapter(redis({ host: 'localhost', port: 6379 }));
The equivalent in my code below is:
io.adapter(ioredis (redisOptions));
However, I kept getting an error saying that io had no method 'adapter'.
Or if I checked first with an 'if' statement to see if io.adapter existed (as shown in code below), then the conditional code never executed.
Can anyone see what I'm doing wrong here? Why doesn't io.adapter exist?
in package.json
"dependencies": {
"redis": "^0.12.1",
"express": "^3.4.8",
"socket.io": "^0.9.16",
"socket.io-redis":"^0.1.4"
}
also tried adding this to package.json
"socket.io-adapter": "^0.2.0"
in app.js (my node server code):
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
, redis = require('redis')
, ioredis = require('socket.io-redis')
, url = require('url')
, redisURL = url.parse(process.env.REDISCLOUD_URL);
And later in app.js :
io.sockets.on('connection', function (socket) {
var pub1 = redis.createClient(redisURL.port, redisURL.hostname, {return_buffers: true});
var sub1 = redis.createClient(redisURL.port, redisURL.hostname, {return_buffers: true});
pub1.auth(redisURL.auth.split(":")[1]);
sub1.auth(redisURL.auth.split(":")[1]);
var redisOptions = {
pubClient: pub1,
subClient: sub1,
host: redisURL.hostname,
port: redisURL.port
};
pub1.set('foo', 'bar');
sub1.get('foo', function (err, reply) {
console.log("redis test : "+reply.toString());
// Prints 'bar'
});
if (io.adapter) {
// Never reached
io.adapter(ioredis (redisOptions));
console.log ("mylog: io.adapter found");
}
console.log ("mylog server connection event fired");
socket.io-redis is meant to be used with socket.io 1.x.
The 0.9.x version doesn't have the adapter.
Related
I've setup a Redis caching and for that I've a Redis-server running on wsl2 and client on NodeJS platform. As soon as the redis-server went down I've to make a few connections that too with a waiting time, but it's making reconnection requests continuously even if I'm using this package(node-redis-retry-strategy).
Here's my code
const redis = require('redis');
var retryStrategy = require("node-redis-retry-strategy");
var client = redis.createClient({
host: "localhost",
port: 6379,
retry_strategy: retryStrategy()
});
client.connect()
client.on("connect", function(){
console.log("connected to redis server !!!")
client.on("reconnecting",function(){
console.log("inside reconnecting")
})
client.on("error",function(error ){
console.log(error)
})
And also I've tried sending option argument
const redis = require('redis');
var retryStrategy = require("node-redis-retry-strategy");
var client = redis.createClient({
host: "localhost",
port: 6379,
retry_strategy: retryStrategy({
allow_to_start_without_connection: true,
number_of_retry_attempts: 7,
delay_of_retry_attempts: 1000
})
});
client.connect()
client.on("connect", function(){
console.log("connected to redis server !!!")
client.on("reconnecting",function(){
console.log("inside reconnecting")
})
client.on("error",function(error ){
console.log(error)
})
And the versions for redis and the retrystrategy package I'm using is
"redis": "^4.3.1",
"node-redis-retry-strategy": "^2.1.1",
additional data
node version v16.17.1
for setting redis-server locally I've used : v=7.0.5 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=68bf11aad5b039df
And I'm using WSL2
I am trying to migrate my google cloud app engine from Redis 3.x to 4.x. However, it appears that there have been some major changes in Redis 4.x. It appears that the client no longer autoconnect and there have been some chnages to the syntax. Here's what I have run
'use strict';
import {createClient} from 'redis';
// These are just values stored in environment variables.
const REDISHOST = process.env.REDIHOST;
const REDISPORT = process.env.REDIPORT;
const REDISAUTH = process.env.REDISAUTH;
const redisClient.createClient();
redisClient.host = REDISHOST;
redisClient.port = REDISPORT;
redisclient.auth = REDISAUTH;
redisClient.on('error', (err) => console.error(`##### REDIS ERR: ${err}.`));
await redisClient.connect();
I can tell that host, port, and auth is being set in redisClient, but when I connect, it tries to connect to localhost and fails. Any idea what I am missing here?
You need to pass the connection information in the call the createClient():
const redisClient = createClient({
socket: {
host: REDISHOST,
port: REDISPORT
},
password: REDISAUTH
})
There are lots of options for connecting. They are all detailed in the client configuration guide.
Here is my complete code for sql connection, all code I have got from stackoverflow issues.
Everywhere, I found the same code is being suggested, hence I also tried with the same.
I have some other application which uses same connection with NextJs and it works fine, however, If I try only with NodeJS code, it gives some socket hang up error (code:'ESOCKET' name:'ConnectionError').
Please make a note that TCP is already configured on remote server and its working fine with other applications.
Any help is appreciated, thank you.
const express = require('express');
const fs = require('fs');
const path = require('path');
const cheerio = require("cheerio");
const sql = require('mssql');
require('dotenv').config(); //to use the env variables
// config for your database
var config = {
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
server: process.env.DATABASE_HOST,
database: process.env.SOMEDB,
port: 14345, // process.env.DATABASE_PORT,
options: {
encrypt: true, // for azure
trustServerCertificate: false // change to true for local dev / self-signed certs
}
};
// make sure that any items are correctly URL encoded in the connection string
let appPool = new sql.ConnectionPool(config);
//I got error on below connect
sql.connect(config).then(function(pool) {
//It never reaches here, it directly goes to the catch block
app.locals.db = pool;
const server = app.listen(3000, function () {
const host = server.address().address
const port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
}).catch(function(err) {
console.error('Error creating connection pool', err)
});
I have the same issue.
Try to use mssql version 6.0.1, it works on my code, but for sure we need to figure out the problem, since we can't think to mantain forever an old version of a package.
I kept trying to find the solution with different different configuration changes.
Finally, I have made a proper config, which worked and now its connecting properly as well as returning the data from the table.
require('dotenv').config(); //to access the process.env params
const sql = require("mssql"); //mssql object
var dbConfig = {
user: "ajay",
password: "abcd123",
server: "your_remote_sql_server_path",
port: 1433,
database: "your_database_name",
options: {
database: 'your_database_name',
trustServerCertificate: true
}
};
try {
//connection config will be used here to connect to the local/remote db
sql.connect(dbConfig)
.then(async function () {
// Function to retrieve the data from table
const result = await sql.query`select top 1 * from table_name`
console.dir(result)
}).catch(function (error) {
console.dir(error);
});
} catch (error) {
console.dir(error);
}
I am not sure what was the exact issue, but as per the previous config and this one, it seems like adding database name to the options has solved the issue.
Please make sure to save all the sensitive data to the .env file. (which you can access as PROCESS.env.parametername)
For me in driver mssql#9.1.1 making encrypt=false worked
const config = {
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
server: process.env.DATABASE_HOST,
database: process.env.SOMEDB,
port: 14345, // process.env.DATABASE_PORT,
options: {
encrypt: false
}
};
i am trying to deploy my app on heroku.i added redistogo addon in my app.it is based on node.js and express.
i write a small code
var redis = require('socket.io-redis');
var io = require('socket.io')(server);
io.adapter(redis(process.env.REDISTOGO_URL));
but on last line i am getting error:
Error: Redis connection to redistogo:6379 failed - getaddrinfo ENOTFOUND redistogo
can any one help why i am facing this error and get rid of this error.6379 is default port but my redistogo url doesn't has 6379 port no.it's port no is 10281.
Is this a bug in socket.io-redis module or i am doing something wrong ??
If your Redis is running on port 10281 you need to set it when initializing adapter.
var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: process.env.REDISTOGO_URL, port: 10281 }));
Check out documentation https://github.com/automattic/socket.io-redis#adapteropts
If redis DB has a password then it's better to opt for
var redis = require('redis').createClient;
var adapter = require('socket.io-redis');
var pub = redis(port, host, { auth_pass: "pwd" });
var sub = redis(port, host, { detect_buffers: true, auth_pass: "pwd"
});
io.adapter(adapter({ pubClient: pub, subClient: sub }));
in case of heroku enter host as
redis://redistogo:XXXXXXXXX#beardfish.redistogo.com
and port : provided in redistogo_url
and now it's working great.
I'm trying to use socket.io-redis to scale my app on Heroku to 2 dynos (or more). Here is my code (where config.redis is just an object housing RedisToGo port, host, and pass values):
var redisApp = require('redis');
var redis = require('socket.io-redis');
if(process.env.NODE_ENV === 'production') {
var socketpub = redisApp.createClient(config.redis.port, config.redis.host, {auth_pass: config.redis.pass, return_buffers: true});
var socketsub = redisApp.createClient(config.redis.port, config.redis.host, {auth_pass: config.redis.pass, detect_buffers: true});
var client = redisApp.createClient(config.redis.port, config.redis.host, {auth_pass: config.redis.pass, return_buffers: true});
socketio.adapter(redis({
pubClient: socketpub,
subClient: socketsub,
redisClient: client
}));
}
On the client side I have:
var ioSocket = io('', {
path: '/socket.io-client',
'force new connection': true,
transports: ['websocket']
});
..so socket.io doesn't try to use polling.
I also have the right Heroku env vars configured for RedisToGo (REDISTOGO_HOST,
REDISTOGO_PASS, REDISTOGO_PORT).
When we're scaled to 1 dyno, the socket behavior is perfect. At 2 dynos, the behavior is way off - requests are being randomly made to either 1 dyno or the other, and the socket events being emitted are sent only to clients running on the dyno to which the request was made and not all (which socket.io-redis & RedisToGo should be taking care of).
Any thoughts would be greatly appreciated!
not sure if this helps you but I am using in this way redis and socketio and it is working good.
var redis = require('redis').createClient;
var adapter = require('socket.io-redis');
var port = config.redistogo.port;
var host = config.redistogo.host;
var pub = redis(port, host, {
auth_pass: auth_pass
});
var sub = redis(port, host, {
detect_buffers: true,
auth_pass: auth_pass
});
io.adapter(adapter({
pubClient: pub,
subClient: sub
}));