Wednesday, 8 February 2017

Difference between CreateConnection and poolConnection in NodeJS

Difference between CreateConnection and poolConnection in NodeJS


MySQL CreateConnection Example
var mysqlClient = mysql.createConnection({
         host     : 'localhost',
         user     : 'root',
         password : '',
         database : 'mydb'
}); 
mysqlClient.connect();
mysqlClient.query(
'SELECT id,name,email FROM `cmf_users` WHERE id = ' + data.uid, function (error, results, fields) {
    console.log(results);
});


MySQL PoolConnection Example
var mysqlPool = mysql.createPool({
       connectionLimit : 10,
       host     : 'localhost',
       user     : 'root',
       password : '',
       database : 'mydb'
});
mysqlPool.getConnection(function(err, mysqlConn) {
mysqlConn.query(
    'SELECT id,name,email FROM `cmf_users` WHERE id = ' + data.uid, function (error, results, fields) {
    console.log(results);
    }
    )
});

Redis Commands with examples

Question: How to set the data with hset?
hset testdata name "Arun Kumar"



Question: How to get the data with hset?
hget testdata name



Question: How to set more data with hset?
hset testdata age "12"
hget testdata age



Question: How to get all data which was set with hset?
hgetall testdata

Output
1) "name"
2) "Arun Kumar"
3) "age"
4) "30"



Question: How to delete data which was set with hset?
hdel testdata age



Question: How to get all data which was set with hset?
hgetall testdata
Command
1) "name"
2) "Arun Kumar"


Redis Commands with examples

Question: How to insert data with lpush ?
lpush usernames "Arun"
lpush usernames "Tarun"
lpush usernames "Karan"



Question: How to list data which was set with lpush ?
lrange usernames 0 10
1)"Arun"
2)"Tarun"
3)"Karan"



Question: How to insert data with zadd?
zadd testdata1 0 "Zero" 
zadd testdata1 1 "One" 
zadd testdata1 2 "Two" 
1) "zero"
2) "ONe"
3) "Two"



Question: Difference between io.sockets.emit and socket.broadcast.emit?
io.sockets.emit will send to all the clients.
socket.broadcast.emit will send the message to all the other clients except the newly created connection.