What does no_ready_check means in redis client argument - node.js

var dbConnection = redis.createClient(config.db.port, config.db.host, {no_ready_check: true});
What does it means and works?
{no_ready_check: true}

This is explained in the documentation:
no_ready_check: defaults to false. When a connection is established to the Redis server, the server might still
be loading the database from disk. While loading, the server not respond to any commands. To work around this,
node_redis has a "ready check" which sends the INFO command to the server. The response from the INFO command
indicates whether the server is ready for more commands. When ready, node_redis emits a ready event.
Setting no_ready_check to true will inhibit this check.

Related

Node JS and Socket.IO. Client getting disconnected. During request

I've ran into a fairly difficult to debug error with my node web server.
Background
I'm creating a node server with socket.io to provide a restful service, connected to mongodb which use web sockets(socket.io) for server-client messages.
Issue
In my node app, I've used an npm package called node-scheduler, in which I do some processing at set times(these are very dynamic times but work fairly well to date).
So I'll set off a job, using node-scheduler and when it ends you can provide a function.
In this function I emit a web socket message, exactly how I emit messages in the rest of the application but my client side never receives the message.
Checking the logs the client disconnections then re connections after the function has completed.
I've debugged a little further, and I send two messages to the client in this function. Only one of them is processed by the client. May be a client issue not a server issue.
Any ideas for solutions or suggestions would greatly be appreciated.
Well generally socket.io is only meant to be used as a "channel". You should have the Client exist as a separate entity in memory or something, and update the socket if and when it reconnects. Otherwise you're just sending to the past (disconnected) sockets.
Using passport you can identify a client as a user.
app.get('/', function(req, res){
// req.user;
});
Using passport.socketio you can get the same user in your socket
io.on('connection', function(socket){
// socket.request.user;
socket.request.user.socket = socket;
// this will be updated with the latest socket in case of a future reconnection
// So now you can be sure that user object will always have the latest socket
nodeScheduler(function(){
carryOutJobs(function callback(){
socket.request.user.socket.emit('done');
// will always emit to the "latest" socket.
});
});
});

Sails V0.10-rc7 Get a record from the database using REST Blueprints via Socket.IO

Sails 0.10.0-rc7
Sails Socket IO : Client not receiving response from server.
Using sails built in blueprints I am attempting to get information from my server using this functionality. (Im looking to use the default behaviour)
Client
//Client on different server (localhost:8000)
//Sails server
var socket = io.connect('http://localhost:1337');
socket.get('/event',function serverSays(err,events){
if (err)
console.log(err)
console.log(JSON.stringify(events));
});
Server
Event Model
module.exports = {
schema : true,
attributes: {
name : {
type : 'STRING',
maxLength: 50,
required: true
}
}
};
In the server terminal (logs) :
verbose: client authorized
verbose: handshake authorized 4TGNw-ywabWYG9j-AHaC
verbose: setting request GET /socket.io/1/websocket/4TGNw-ywabWYG9j-AHaC?__sails_io_sdk_version=0.10.0&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript
verbose: set heartbeat interval for client 4TGNw-ywabWYG9j-AHaC
verbose: client authorized for
verbose: websocket writing 1::
verbose: A socket.io client (4TGNw-ywabWYG9j-AHaC) connected successfully!
BUT the callback on my client is never being called????
It seems as if the client connects with the server..
Any suggestions?
EDIT
I must stress that the client and the sails server are running on different servers. The handshake when performing io.connect(localhost:1337) talks with the server correctly based on the server logs.
Its the subsequent action socket.get("/Event") which does not result in anything. Based on the server logs, I would say that its not ever reaching the server....
I thought I would just leave a note as I have my implementation working now.
So as it turns out, I made a fairly embarrassing mistake/assumption.
using Sails js's browser SDK I was connecting to a remote server using:
io.connect("serverurl")
and then happily went about my business attempting to perform various socket functions such as socket.get..
What I did not do is after
io.connect("url")
I still had to ensure that my app had indeed connected to the server by listening on the socket for the connect event:
socket.on("connect",function())...
Once I had this little piece of the puzzle resolved all went and is going swimmingly!
I must also state that I believe the reason I was running into to this issue was because I was attempting to perform the initial connection and subsequent requests in the sails run (init) function. So my subsequent actions were more than likely executing before the app and the server had successfully established a connection.
I believe had the initial connect (io.connect) and the subsequent actions been executed in separate user flows, all would have been as the connection would have surely been established already.

connecting to RedisToGo on Heroku thru Nodejs [duplicate]

I'm using Redis To Go in combination with the https://github.com/mranney/node_redis library. Redis gives me a url that looks like redis://me:978287c0b670694673d045f08b2e0371#icefish.redistogo.com:9393 but I don't know how to use it as createClient() only takes the host and the port.
I believe that the scheme for the URL you have is:
redis://username:password#host:port.
I don't believe username is used. node_redis provides two methods that you'll use to log in: createClient and auth. There are details in the readme, but for reference here is the relevant portion:
redis.createClient(port, host, options)
Create a new client connection. port defaults to 6379 and host
defaults to 127.0.0.1. If you have redis-server running on the
same computer as node, then the defaults for port and host are
probably fine. options in an object with the following possible
properties:
parser: which Redis protocol reply parser to use. Defaults to
hiredis if that module is installed. This may also be set to
javascript.
return_buffers: defaults to false. If set to true, then bulk
data replies will be returned as node Buffer objects instead of
JavaScript Strings.
createClient() returns a RedisClient object that is named client
in all of the examples here.
client.auth(password, callback)
When connecting to Redis servers that require authentication, the
AUTH command must be sent as the first command after connecting.
This can be tricky to coordinate with reconnections, the ready check,
etc. To make this easier, client.auth() stashes password and will
send it after each connection, including reconnections. callback is
invoked only once, after the response to the very first AUTH command
sent.
I also had to add the parameter no_ready_check: true to the call to redis.createClient().
client = redis.createClient(settings.redis.port,
settings.redis.host,
{no_ready_check: true});
if (settings.redis.password) {
client.auth(settings.redis.password, function() {
console.log('Redis client connected');
});
}

socket.io client connection cannot be made on the 2nd time

Currently, I am implementing an API using nodejs express, then it needs to connect to socket.io and send event.
The API is located in socket.io-client (client), and it connects to socket.io (server)
1st API call: success
The connection is made for the 1st call of the API, message is sent and socket can be disconnected, with the 'disconnect' callback is invoked both on client and server side.
2nd API call: failure
When the API is invoked the 2nd time, the connection to server cannot be made, 'client' callback on client side is not called.
3rd API call: success
Then I tried to restart the client side, keeping other things unchanged. The API is called again, and the connection to socket.io is made successfully and everything is fine.
Can anyone explain the logistics behind this?
Updated
client.js
App.getByUserId(message.to_id, function(error, app) {
var socket = io.connect('http://127.0.0.1:9002');
socket.on('connect', function(){
console.log("client connect socket id:" + socket.id);
console.log("appkey:" + app.private_token);
socket.emit('appkey.check',{appkey: app.private_token, uuid: message.to_id.uuid}, function(data){
socket.emit("forceDisconnect");
socket = null;
});
});
You just hit one of Socket.IO's many "features" or "bugs" depending how you see this. Socket.IO tries to be smart and re-use connections (which causes a lot of connection issues actually) The way around this is use the force new connection option in your io.connect:
io.connect('http://127.0.0.1:9002', { 'force new connection': true });
What you could also do is use https://github.com/primus/primus which wraps Socket.IO if you use the socket.io transformer. Internally, it completely removes the use of the io.connect and uses the much more lower level io.Socket constructor to create more stable connections that you would get with a stock socket.io.
With socket 1.0+, you have to use this for forcing new connection.
io.connect(SERVER_IP, { 'forceNew': true });

Connecting to RedisToGo through Node.JS

I'm using Redis To Go in combination with the https://github.com/mranney/node_redis library. Redis gives me a url that looks like redis://me:978287c0b670694673d045f08b2e0371#icefish.redistogo.com:9393 but I don't know how to use it as createClient() only takes the host and the port.
I believe that the scheme for the URL you have is:
redis://username:password#host:port.
I don't believe username is used. node_redis provides two methods that you'll use to log in: createClient and auth. There are details in the readme, but for reference here is the relevant portion:
redis.createClient(port, host, options)
Create a new client connection. port defaults to 6379 and host
defaults to 127.0.0.1. If you have redis-server running on the
same computer as node, then the defaults for port and host are
probably fine. options in an object with the following possible
properties:
parser: which Redis protocol reply parser to use. Defaults to
hiredis if that module is installed. This may also be set to
javascript.
return_buffers: defaults to false. If set to true, then bulk
data replies will be returned as node Buffer objects instead of
JavaScript Strings.
createClient() returns a RedisClient object that is named client
in all of the examples here.
client.auth(password, callback)
When connecting to Redis servers that require authentication, the
AUTH command must be sent as the first command after connecting.
This can be tricky to coordinate with reconnections, the ready check,
etc. To make this easier, client.auth() stashes password and will
send it after each connection, including reconnections. callback is
invoked only once, after the response to the very first AUTH command
sent.
I also had to add the parameter no_ready_check: true to the call to redis.createClient().
client = redis.createClient(settings.redis.port,
settings.redis.host,
{no_ready_check: true});
if (settings.redis.password) {
client.auth(settings.redis.password, function() {
console.log('Redis client connected');
});
}

Resources