socket.io-redis giving error on heroku - node.js

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.

Related

Redis unable to connect with env port

I'm trying to create a Redis client. However whenever I do:
const REDIS_PORT = process.env.PORT || 6379;
const client = redis.createClient(REDIS_PORT);
I get AbortError: Ready check failed: Fatal error encountered. Command aborted. It might have been processed.
However if I do:
const REDIS_PORT = 6379;
const client = redis.createClient(REDIS_PORT);
It connects properly. Why do I get this error when I put process.env.PORT?
Assign the desired value to the port key, use this code for create client
redis.createClient({port : REDIS_PORT})
you can check the documentation
const redis = require('redis');
const client = redis.createClient({
host: '127.0.0.1',
port: <port>
});

How to troubleshoot problem with setting WebRTC server on vps?

I wrote a simple node express server for webRTC using peerjs-server and simple client using peerjs. Everything works fine on localhost, but when I try it on vps, I get error:
Firefox can't connect with server ws://my.vps/peerjs/peerjs?key=peerjs&id=hj3hpekwaa38fr00&token=ymtfvhagiw
PeerJS: Socket closed.
PeerJS: ERROR Error: Lost connection to server.
Error: "Lost connection to server."
emitError https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:16426
_initializeServerConnection https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:12260
emit https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:25516
onclose https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:19350
Server:
const express = require('express');
enter code here`const app = express();
const ExpressPeerServer = require('peer').ExpressPeerServer;
app.use(express.static('./public'));
const server = app.listen(80, () => { // 3000 on localhost
console.log('Express server listen on port ' + 80);
});
const options = { debug: true };
const peerserver = ExpressPeerServer(server, options);
app.use('/peerjs', peerserver);
app.use('/*', express.static('./public/index.html'));
Client:
var peer = new Peer('', {
host: location.hostname,
port: location.port || (location.protocol === 'https:' ? 443 : 80),
path: '/peerjs',
debug: 3
});
peer.on('open', function (id) {
console.log(id);
});
Any help appreciate.
It looks like you are connecting with server ws://my.vps/, which is a web socket to a server at http://my.vps/ which doesn't seem to exist.
It should probably also be using https (or wss)

How do i get the redis store client from socket-io-redis?

socket-io-redis use redis inside to connect, I wonder how can i get the redis store client from the library so that i can use that client to store key value pairs in redis.
var redis = require('socket.io-redis');
var socketio = require('socket.io');
io = socketio.listen(app);
io.adapter(redis({ host: 'localhost', port: 6379 }));
I am using node cluster model, So i do not want to create a new redis client as i already have one.
It's shown here:
var adapter = redis({ host: 'localhost', port: 6379 });
var pubClient = adapter.pubClient;
io.adapter(adapter);
There's also an equivalent adapter.subClient, but you shouldn't use that (clients running in "subscribe" mode shouldn't be used for regular Redis commands).
I'm not sure if it's wise to use the pubClient to issue commands, either. Redis can handle multiple connections just fine, and it's easy to create a new client from the pubClient instance:
var pubClient = adapter.pubClient.duplicate();

Scaling to 2+ dynos on Heroku with socket.io-redis and RedisToGo

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
}));

Node.js + Express.js + Socket.io on port 443 (HTTPS TLS/SSL)

I have an app with Node.js, Express.js, and Socket.io that runs fine using ANY port except 443. The server is meant to only operate over HTTPS port 443 and likewise, the websocket should be encrypted as well.
CODE THAT WORKS
var fs = require('fs');
var https = require('https');
var express = require('express');
var socket = require('socket.io');
var sslOptions = {
key: fs.readFileSync(__dirname + '/../ssl/server.key,
cert: fs.readFileSync(__dirname + '/../ssl/server.pem,
ciphers: 'ECDHE-RSA-AES256-SHA:AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM',
honorCipherOrder: true
};
var app = express();
var server = https.createServer(sslOptions, app);
var io = socket.listen(server, {
"log level" : 3,
"match origin protocol" : true,
"transports" : ['websocket']
});
server.listen(8443);
When I change the port (last line) to 443, the Node server crashes right away with an error:
warn: error raised: Error: listen EADDRINUSE
Apparently you've already got a server listening on that port on your machine. Is is possible that you started this server elsewhere and it's still running?
It means that the port is in use, you can check using :
sudo netstat -tapen | grep ":443".
If you use Apache, Ngnix or other server it is likely to be it.

Resources