Two way communication between server and client on socket.io node.js - javascript

I am trying to write code for server in node.js in which, client running on browser will send some data to server when some ".on" event will occur. Now at server side the task is to receive data coming from client and send that data back to client.
I am using socket.io.
Write now I am doing this as,
Client side:
<p id="ValSlider1"> Curr Val </p>
<input class = "mySlider1" type="range" name="slider" id="slider-0" value="0" min="0" max="100" />
<script>
var socket = io.connect('http://localhost');
$(".mySlider1").change(function() {
var sVal = $(this).val();
socket.emit('ValSlider1', sVal);
console.log('ValSlider1: ' + sVal );
});
socket.on('packet', function (data) {
var valFromServer = data.split('-');
document.getElementById("ValSlider1").innerHTML = valFromServer[0];
document.getElementById("ValSlider2").innerHTML = valFromServer[1];
document.getElementById("ValSlider3").innerHTML = valFromServer[2];
document.getElementById("ValSlider4").innerHTML = valFromServer[3];
$('#container1').html(data);
});
and on server side:
var qs = require('querystring'),
fs = require('fs'),
parser = new require('xml2json'),
urr = require('url'),
app= require('http').createServer(handler).listen(3000),
io = require('socket.io').listen(app);
function handler (req, res) {
var reqObj = urr.parse(req.url, true);
var reqPath = reqObj.pathname;
if ('/' == reqPath ) {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile(__dirname + '/client.html', function(err, data) {if (err) {res.writeHead(500);
return res.end('Errorloadingclient.html');
}
res.end(data);
});
}
};
var slider1, slider2, slider3, slider4 ;
io.sockets.on('connection', function(socket) {
socket.on('ValSlider1', function(data){
slider1 = data ;
socket.emit('packet', data);
console.log("Slider 1 Value: " + data);
});
});
setInterval(function () {
var data = slider1 + "-" + slider2 + "-" + slider3 + "-" + slider4;
socket.emit('packet', data);
console.log(data);
},1000);
});
app.maxConnections = 1;
Now, when communication starts initially two-three slider change values are received by server and printed on console but then the values not get updated, server sends previous values only to client.
Also, if I use socket.emit('ValSlider1', sVal); twice at client side it works better, but why it require emit twice am unable to find, any help will appreciated.
Thanks.

I finally managed to do it in this way:
On server side:
var app = require('http').createServer(handler).listen(3000),
var io = require('socket.io').listen(3001);
var publisher = require('socket.io').listen(app);
On client side:
var socket = io.connect('http://localhost:3001');
var socket2 = io.connect('http://localhost');
The client will send data to server on 'socket'. In response, the server will also send some data on the same port. The server will continuously push data on port 3001 at every second which is received in the client with the ".on('data')" event of server1.

Related

synchronizing http response with socket connection

I am using a http server with a socket connection , when http request hits, socket connection sends message to different client which performs a http request after this http response the client replies to the message and and response of initial http request is send.
My problem is first http request sometimes get response successfuly and sometimes not, I think there is a sync problem, how to solve it.
code for creating socket and httpserver -
const app = require('express')()
bodyParser = require('body-parser')
app.use(bodyParser.json())
const net = require('net');
var client;
var res1,currentReq;
//----------------------------------------------------------------------------
// http requests listener
//----------------------------------------------------------------------------
app.listen(8001, () => console.log('Http server listening on port 8001'));
//----------------------------------------------------------------------------
// http requests handling
//----------------------------------------------------------------------------
app.post('/makeCall', (req, res) => {
console.log('sd' + req.body)
res1 = res;
currentReq='makeCall';
console.log('{"route":"/api/makeCall","data":{"product_id":"' + req.body.product_id + '","destination":"' + req.body.destination + '"}}');
client.write('{"route":"/api/makeCall","data":{"product_id":"' + req.body.product_id + '","destination":"' + req.body.destination + '"}}');
});
//----------------------------------------------------------------------------
// Establishing tcp connection for incoming requests
//----------------------------------------------------------------------------
var server = net.createServer(function(connection) {
console.log ('client has connected successfully!');
client = connection;
client.on('data',function(data){
switch(currentReq)
{
case 'makeCall' :
console.log('send make call response');
res1.end(data);
break;
}
console.log(data.toString());
//res1.end(data);
});
connection.pipe(connection);
});
//----------------------------------------------------------------------------
// listener for tcp connections
//----------------------------------------------------------------------------
server.listen(8000, function() {
console.log('server for localhost is listening on port 8000');
console.log('server bound address is: ' + server.address ());
});
code for client to which socket connects -
tcpClient.on('connect',function(){
logger.info("[%s] , Connected to the server at %s:%s",__file,CONFIG.tcp_server_host,CONFIG.tcp_server_port);
logger.info("[%s] , TCP client info %s",__file,tcpClient.address().address);
});
// Handle data event
tcpClient.on('data',function(data){
logger.info('[%s] , Data recevied',__file);
// Convert the Buffer to JSON object
var reqInfo;
reqInfo = JSON.parse(data.toString());
if(reqInfo!=null){
switch(reqInfo.route){
case '/api/makeCall':;
var product_id = reqInfo.data.product_id;
var destination = reqInfo.data.destination;
var test = {};
var source;
var myJSONObject = {'product_id':product_id};
MongoClient.connect(url, function(err, db) {
var dbo = db.db("mapping");
dbo.collection("mapping").findOne({"id":product_id},function(err,result)
{
if(err)
{
throw err;
}
JSON.stringify(result);
sourceDB = result.source;
// Set the options for HTTPS request
options.method = "POST";
options.url = "url";
options.json = true;
options.auth = {
user: '123',
password: '123'
};
options.body = {"Source": sourceDB,"Destination": destination} ;
logger.info('[%s] , HTTPS request options : %o',__file,options);
request(options,function(error,res1,body){
tcpClient.write('Sending data to Falcon');
});
});
});

cannot read property '<fieldname>' of null after some time running

I have an angularjs website with NodeJS socket methods as a client, and a NodeJS application as a server.
Data is being passed from the server to the client through socket events. The problem is that after working fine for about 5 minutes, intermittently data that is successfully being sent from the NodeJS server is being received as 'null' in the website, thus giving me the following error:
angular.js:11598 TypeError: Cannot read property '<fieldName>' of null
at ecabsAngular.js:42
at angular.js:12149
at angular.js:12137
at angular.js:12137
at Object.<anonymous> (angular.js:12837)
at l.$digest (angular.js:14222)
at l.$apply (angular.js:14493)
at Socket.<anonymous> (ecabsAngular.js:275)
at Socket.Emitter.emit (socket.io-1.2.0.js:1)
at Socket.onevent (socket.io-1.2.0.js:1)
Do you have any idea what is causing this and how this can be solved?
Thanks in advance!
Edit:
This is the code that is crashing on the client side (on the website)
(function(){
var app = angular.module('Module', []);
app.controller('MainCtrl', ['$scope', '$window', function($scope, $window){
$scope.bookings = [];
socket.on('updateBooking', function(booking){
$scope.loadAllocations();
for (var i=0; i<$scope.bookings.length; i++) {
if ($scope.bookings[i].CRMBookingID == booking.CRMBookingID) {
$scope.$apply(function(){
$scope.bookings[i] = booking;
});
}
}
});
}
]);
})();
This is the code from the NodeJS (server):
var app = require('express')();
var http = require('http');
http.globalAgent.maxSockets = 200;
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var sql = require('mssql');
server.listen(3000);
app.post('/updatebooking', function(request, response) // receive web service request
{
response.writeHeader(200, {"Content-Type": "text/plain"}); // build response header
response.end(); // end the response
request.on('data', function(changes)
{
try{
var c = JSON.parse(changes.toString()); // parse data received
var connection = new sql.Connection(config); // create DB connection
connection.connect(function (err) // connect to the database
{
if (err!=null) { logger.error('Update booking connection error: ' + c + ' ' + err); }
var request = new sql.Request(connection); // create SQL request
request.stream = true;
try{
var sqlquery = <sqlquery>; // build SQL query
logger.trace("Executing SP in DB: " + sqlquery);
request.query(sqlquery); // execute SP
request.on('row', function (row) // once committed send to the client (website)
{
logger.trace("Updated booking with Reference ID " + row.Ref + " " + sqlquery);
io.emit('updateBooking', row); // sending data
})
request.on('error', function(err) {
logger.error('Update booking SP request error: ' + sqlquery + err);
});
}catch( e){
logger.error('Update booking request error' + sqlquery + ' ' + e);
}
});
} catch(e)
{
var c = JSON.parse(changes.toString());
logger.error('Update booking error for: ' + c + ' ' +e);
}
connection.on('error', function(err) {
logger.error('Update error connection: ' + err);
});
});
});

Nodejs net.createServer detect when data is done

Say that I use net.createServer in Node.js to create a server like so:
'use strict';
var net = require('net'),
serverPort = 1024,
serverHost = 'localhost',
server;
server = net.createServer(function (socket) {
var whole = '';
console.log('Client connected.');
socket.setEncoding('utf8');
socket.on('data', function (data) {
console.log('Received data.');
whole += data;
});
socket.on('end', function () {
console.log('Client disconnected.');
});
});
server.listen(serverPort, serverHost, function () {
console.log('Server listening at ' + serverHost + ':' + serverPort);
});
I create a connection to this server in an external program and send it a big string. "Received data." is printed 4 times. How can I definitively know when the entire string has been received by the server? The end event is only called when the client disconnects, not when he is done sending his big string.

How can i make a response.write in the request module

I'm creating HTTP server and inside i'm sending a request to to yahoo finance website and getting some data from it, what i want to do is to print to browser the data i got from yahoo finance.
the thing is that response.write isn't working inside the request.
Here is my code:
var http = require('http');
var request = require('request');
var cheerio = require('cheerio');
var util = require('util');
var host = "127.0.0.1";
var port = 1400;
var server = http.createServer(function (req, res) {
//writing the headers of our response
res.writeHead(200, {'Content-Type':'text/plain'});
// Variable Deceleration
// TODO: move from the global scope
var ticker = "IBM";
var yUrl = "http://finance.yahoo.com/q/ks?s=" + ticker;
var keyStr = new Array();
//
// The main call to fetch the data, parse it and work on it.
//
request(yUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
// the keys - We get them from a certain class attribute
var span = $('.time_rtq_ticker>span');
stockValue = $(span).text();
res.write("trying to print something");
console.log("Stock - " + ticker + " --> text " + stockValue );
}
}); // -- end of request --
res.write('Welcome to StockWach\n');
//printing out back to the client the last line
res.end('end of demo');
});
server.listen(port, host, function () {
console.log("Listening : " + host +":" + port);
});
You have to end the response (res.end();). Almost all browsers buffer some number of bytes from the response before showing anything, so you won't see the trying to print something until the response has ended.
If you use something like cURL though, you will see the trying to print something right away before the response is ended.

Array being sent as String on node.js

I am doing some experimenting with socket.io. I have a canvas that successfully sends data to the server which receives it fine.
It receives a Uint8ClampedArray which is correct because that is what is being sent.
When I then .send this message from the server to the client, I get a string: [object Object]. Again I have checked!
Am I missing something, the code for the server is below:
var fs, http, io, server;
fs = require('fs');
http = require('http');
server = http.createServer(function(req, res) {
return fs.readFile("" + __dirname + "/front.html", function(err, data) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
return res.end(data, 'utf8');
});
});
server.listen(1337);
io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket) {
socket.on('publish', function(message) {
return io.sockets.send(message);
});
});
On Client:
var dataStr = JSON.stringify(data); // converts object to string
On Server:
var dataObj = JSON.parse(data); // converts string to object
The only way is to reuse Uint8ClampedArray in your client size. If you have this on your server-side
var x = new Uint8ClampedArray(3);
x[0] = -17;
x[1] = 93;
x[2] = 350;
var dataThatWillBeSent = JSON.stringify(x);
// '{"0":0,"1":93,"2":255,"length":3,"byteLength":3,"byteOffset":0,"buffer":{"byteLength":3}}'
In your client side, assume that you have included Uint8ClampedArray, you can do this
var dataReceived = '{"0":0,"1":93,"2":255,"length":3,"byteLength":3,"byteOffset":0,"buffer":{"byteLength":3}}';
dataReceived = JSON.parse(dataReceived);
// reconstruct Uint8ClampedArray object
I have not used Uint8ClampedArray before, so I dont know exactly how you can recover a Uint8ClampedArray object from a JSON data, but if you read the document you might be able to figure something out

Categories