Tuesday, 8 August 2017

Send Acknowledgment for socket.io event

Send Acknowledgment for socket.io event

Question: How to send Acknowledgment for socket.io event?
in node.js (Server side)
io.sockets.on('connection', function(socket){    

    /* Message Received from Client*/
    socket.on('chat_messsage', function(data, callback){        
        var responseData = { 'message':data.message, 'received':1};        
        callback(responseData); //Send Acknowledgment to the client
    });


});

Client Side JavaScript
var socket = io.connect('http://localhost:3000');
socket.on('connected', function (data) {    
    
    //Send message to Server with callback function
    socket.emit('chat_messsage', {message:"this is message"}, function(response){
        console.log(response); //console the Acknowledgment
    });

});



Question: How to Send additional data on socket connection?
Client side, connect with parameter
  var socket =  io.connect(chatURL,{ query: "req_from=browser" });


Sever side, Detect the client Connection.
io.on('connection', function (socket) {
  console.log(socket.handshake.query['req_from']);
});


Question: How to connect node with http and https?
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync('cert/server.key', 'utf8');
var certificate = fs.readFileSync('cert/server.crt', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

//http connect
httpServer.listen(8080);
//https connect
httpsServer.listen(8443);



Friday, 4 August 2017

php amazing question and logical answer

php  amazing question and logical answer

Question: What is output of 1...1?
Its Output is 10.1
How?
Divide it into three points.
1.
.
.1

  1. 1.0 is equal to 1
  2. 2nd . is a concatenation operator
  3. .1 is equal to 0.1
  4. So {1} {.} {0.1} is EQUAL to 10.1



Question: What is final value of $a, $b in following?
$a = '1';
$b = &$a;
$b = "2$b";
echo $a;
echo $b;
output
21
21

Explaination
$a = '1'; //1
$b = &$a; //both are equal
$b = "2$b"; //now $b=21
echo $a;  //21
echo $b; //21



Question: What is output of 016/2?
Output is : 7. The leading zero indicates an octal number in PHP, so the decimal number 14 instead to decimal 16;
echo 016; //14
echo 016/2; //7