Since we can configure socket.io to use redis for it's internal workings like this:
var RedisStore = require('socket.io/lib/stores/redis')
, redis = require('socket.io/node_modules/redis')
, pub = redis.createClient()
, sub = redis.createClient()
, client = redis.createClient();
io.set('store', new RedisStore({
redisPub : pub
, redisSub : sub
, redisClient : client
}));
How is it possible to see inside of this RedisStore to see what data socket.io is inserting and removing. I set my socket.io configuration to use the redis instance installed from node-redis like this:
redis = require('redis')
but I don't see any socket.io activity going on and I wonder if socket.io is actually using redis. I do see my cookies being stored in redis because I configured express and connect to use redis as the MemoryStore, but I don't see anything related to socket.io.
For local redis node.js development/debugging I do this.
Start redis server in background and run the cli monitor in the same tty:
redis-server &
redis-cli monitor -h host -p port
You can leave - switches off if using local defaults.
You can also run a redis slave that echos all master commands and SYNCs the current redis-server's memory held data into a textfile defaulting to ./dump.rdb. The file can be loaded into redis and viewed with a text editor.
redis-cli --slave -h host -p port
vim ./dump.rdb
A basic node.js Passport session-cookie would look something like this:
sess:L2C4MPtAUmlO4zHGkXnq4icuÃ#U#Z{"cookie":{"originalMaxAg null,"expiresÀhttpOnly":true,"path":"/"}passport L}
To further help see inside you may want to step through your app and put breakpoints in the areas where your node code and redis driver interact.
You can use this to watch the handshake process, see incorrect data exchanging, problems with the socket, etc by stepping through your functions, and observing the changes in state during control flow.
The node app for this job is node-inspector.
It will allow you to view your app running in a debugging environment similar
to Chrome dev tools, where you can set breakpoints to pause, step in and out
of functions, and move forward in time until the next breakpoint or exception
triggers a pause.
npm -g install node-inspector
You install it with the '-g' global switch as you use it across multiple projects.
running the program without any options in a terminal window produces this output for me:
~/passaic-streaming git:weekend-refactor ❯❯❯
Node Inspector v0.7.0
Visit http://localhost:8080/debug?port=5858 to start debugging.
You can configure the HOST, PORT, DEBUG-PORT using config or ENV variables
(DEBUG_PORT, port 5858, passed as a parameter sets up a socket connection
between the running node process and your browser running the web-app).
Now, there are three ways to run your node.js code in debug mode. You do this after first running node-inspector:
node --debug-brk app.js
node --debug app.js
node app.js // then send a SIGUSR1 to the node process:
// pgrep node
// kill -s USR1 PID
Use the first command '--debug-brk', until you got everything working, as it tells your code to stop on your module's first line.
browser http://localhost:8080/debug?port=5858
It can take a little time to load depending on your app size. But once there you'll have a source tab to step through code, and a console tab.
The '--debug' will work fine on an express app, you may just need to hit an endpoint or do some transaction or tell it to pause at a breakpoint manually.
Some node scripts are too fast to get caught by the node-inspector listener and until you set up breakpoints in the source code it can be convenient to break on the first line, hence the command.
You can also put 'node debugger';lines around areas like:
// connect to Redis for sessionStore
node debugger; // node-inspector will pause here.
redisClient = redis.createClient(
options.PORT, options.HOST, options.REDIS_OPTIONS));
The main reason you may need to use the SIGUSR1 and not start the app in debug mode is if you are using the node.js cluster API.
If you try to monitor an app that is using clustering and creating multiples node processes, then if you try to debug in node < 0.11.x you'll see this problem with the socket:
Failed to open socket on port 5858, waiting 1000 ms before retrying
Failed to open socket on port 5858, waiting 1000 ms before retrying
In the unstable node versions the child_process.fork command listens on a debugger port 5858+1, so 59, 60, etc, depending on the workers you have.
For now in node 0.10.x if you need to inspect the state of a worker in a clustered app you'll have to start node not in '--debug' mode and instead send the kill USR1 signal to the process.
To coerce more logging out of node with redis put your node_redis driver in debug mode:
redis = require('redis');
redis.debug_mode = true; //turn on debug mode
Also if using connect-redis middleware if you start like this:
DEBUG=* node app.js
Then you will see connect-redis output like this:
connect:redis SETEX "sess:FVDBpNoFDFIOkynFDLcpM6St" ttl:604799
{"cookie":{"originalMaxAge":604799991,"expires":"2014-03-08T22:38:32.620Z","httpOnly":true,"path":"/"},"passport":{"user":"52edfdfdf3da2rer0c06eb34"}} +50ms
Finally, the team at StrongOps/Strongloop has part of their command line tools node-inspector baked in.
npm install -g strong-cli
slc debug app.js
It will boot the node-inspector, open the web browser to the proper inspector, and in the latest node versions -- it understands clustering.
Related
I am very New to NodeJS. I am developing Live Streaming based Speech to text for my web application. It works well but problem is
Sometimes, Nodejs throws an error as 'Request Time-Out' and http server has been stop. then I need to manually re run program again (with command node app.js)
I had used this example here.
Screen shot is bello
Please help. And thanks in advance.
You need first to try {} catch(ex){}your exceptions.
You may also use pm2 which can handle that autostart if it crashes.
When using pm2 please make use of --max-memory-restart option otherwise the app can indefinitly restart and will slow down your server. That option can help you specify the amount of memory the autorestart can consume.
Install pm2
npm install -g pm2
//or
yarn global add pm2
Run the app
pm2 start app.js --max-memory-restart 100 --name node-speech
Using pm2 is even recommanded on the repository readme
you can always have a global error handler, so that, your project won't fail and also you can take an appropriate action:
process.on
(
'uncaughtException',
function (err)
{
console.log(err)
var stack = err.stack;
//you can also notify the err/stack to support via email or other APIs
}
);
How can I debug a running nodejs app? I've found tools such as node-inspector, but it seems to only support starting the app and debugging from there.
Debugging a running nodejs app.
This is the combination of a little documented feature of V8 mixed with a non-documented feature of the node.js debugger client. Say you have an already running node process that you want to debug.
# start the remote debugger in the already running node process
kill -s USR1 pid
# attach to the debugger using the node.js client
node debug host:5858
# see where you are
debug> pause
debug> bt
From there you can poke around. You can also continue and pause again to see if you seem to consistently end up in the same code area.
Debugging a nodejs app.
V8 comes with an extensive debugger which is accessible out-of-process via a simple TCP protocol. Node has a built-in client for this debugger. To use this, start Node with the debug argument; a prompt will appear:
% node debug myscript.js
< debugger listening on port 3000
connecting... ok
break in /home/username/Code/myscript.js:1
1 x = 5;
2 setTimeout(function () {
3 debugger;
debug>
cont, c - Continue execution
next, n - Step next
step, s - Step in
out, o - Step out
pause - Pause running code
Check API for other commands reference and other details
You can also use node-inspector . Use it from any browser supporting websockets. Breakpoints, profiler, livecoding etc... It is really awesome.
Install it with
npm install -g node-inspector
then run
node-debug app.js
Im a little confused with all this nodejs debug syntax flying around.
I simply want to start the debugger on a process when I run it on a different port.
Normally I start debugging by node debug file.js
but now I have to process` running that I need to debug
Now I found the command node --debugger=7873 file.js but that starts the debugger and jumps past all the breaks and I tried node --debugger=7837 --debug-brk file.js but that forces me to consume another teminal window. How can I just run a script on a different port in the same terminal or with out using nohup?
node debug --port=[your port] your_program.js
responsible _debugger code here
Here is my code:
var express = require("express"),
app = express(),
server = require("http").createServer(app),
io = require("socket.io").listen(server),
redis = require("redis"),
env = {PORT: process.env.PORT || 8080, IP: process.env.IP || "localhost"};
client = redis.createClient(env.PORT , env.IP);
client.on("error", function(err) {
console.log(err);
});
server.listen(env.PORT);
console.log("Server started # " + env.IP + ":" + env.PORT);
After trying to run, I received the followings on the console:
Running Node Process
Your code is running at 'http://modified.address.c9.io'.
Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts!
info: socket.io started
Server started # modified.ip.address.1:8080
[Error: Auth error: undefined]
I tried establishing the connection, and it connects to the IP and PORT perfectly. However, the error [Error: Auth error: undefined] appears and stops there. I Googled the error, the supports from the IDE I used..., and surprisingly, there are only 7 links to my problems. So I think it may be a hole in my knowledge or it is not really a problem yet a thing I don't know to work it out. All I could pull out from those Google results were (I was not sure) I need to use client.auth(pass) right after creating it. But where should I find the password? When I installed it npm install redis I didn't configure anything and wasn't told to set password whatsoever. So I reach the impasse.
I use Cloud9 IDE (c9.io), and the modules used as shown in the code above.
----With best regards,
----Tim.
I've found out what was wrong.
I did install Redis, but that is a Redis library that acts like a bridge between Redis driver and NodeJS. On Cloud9, I have to manually install Redis, too.
So it would take 2 commands to actually install Redis:
Install the Redis Driver on Cloud9
nada-nix install redis
Install Redis library for NodeJS
npm install redis
Thanks for anyone who was trying to help me.
You can run the redis-server using your own config file.You can create your own config like below.
//port and ip of ur redis server
port 6371
bind 127.0.0.1
//password for this server
requirepass ucanmentionurpwd
//storing snapshots of the data
save 60 1
dbfilename dump.rdb
dir /tmp/db
//starting redis server
redis-server //ur config file location
See this link for redis configuration
https://raw.github.com/antirez/redis/2.6/redis.conf
If you mention requirepass with your password means only you need to do
client.auth('urPwd');
Otherwise no need to call the client.auth method.
I am trying to debug the child_process example from here using IntelliJ IDEA 12.1.3 and node 10.10. When I run nodejs app.js from a terminal everything works. The console output displays as expected. However, when I debug the same script using IDEA there are no messages in console output and the app just sits there. This is what is in the console window:
/usr/bin/nodejs --debug-brk=58954 app.js
debugger listening on port 58954
debugger listening on port 58954
When I run the script in IDEA without the debugger attached, the script works as expected.
Why does attaching the debugger break the script?
You can force the children to use a free port for debugging. InteliJ will automatically pick up the port chosen by the child process.
Here's an example:
// Determine if in debug mode.
// If so, pass in a debug-brk option manually, without specifying port.
var startOpts = {};
var isInDebugMode = typeof v8debug === 'object';
if(isInDebugMode) {
startOpts = {execArgv: ['--debug-brk']};
}
child_process.fork('./some_module.js', startArgs, startOpts);
looks like a bug in node.js fork to me: both parent and child processes receive --debug-brk=58954 switch and attempt to start debugger and listen port 58954.