socket.io not working inside zmq function - zeromq

i am putting socketio inside zmq but its not working below is the code
not sure what i am doing wrong
var io = require('socket.io').listen(2939);
var zmq = require('zmq');
var socket = zmq.socket('sub');
console.log("Connecting to Currency Stream....");
socket.subscribe("");
socket.on('message', function (data){
//console.log(data.toString());
io.sockets.on('connection', function(socket){
console.log("In socket.io");
console.log(data.toString());
socket.emit("live_rates",
{date: data[0], time: data[1],
open: data[2], high: data[3],
low: data[4], close: data[5]});
socket.emit("news", {hello: 'world'});
console.log("emitted");
});
});
socket.connect('tcp://localhost:3191');
code inside io.socket.on is not running, handshake is successful as i can see in the log window. when i am putting the code inside zmq (uncomment //console.log(data.toString()); )
it's showing the data.
what i am really doing here is getting the data from zmq server on a zmq tcp port 3191 and forward it to socket.io http port 2939, then getting the data on browser by connecting to port 2939 using an html page. i do not wish to use zmq on client side as socket.io is much robust for that play.
also a separate socket.io connection ie io.sockets.on is working but inside a zmq socket it fails!
any one
zishan

solved it by using io.sockets.emit rather than socket.emit.
io.socket.on is now placed outside of zmq.
i suspect socketio callback is now responding if using socket.emit when using inside zmq function.
got a clue from this blog post
https://groups.google.com/forum/#!msg/socket_io/Vi0uWeHsj_0/JEOcXAc-pn8J

Related

How many WebSocket connection can be created on Client Side

I have started to learn web sockets. It is must learn technology in today's time.
But i am curious to learn more about it. My basic question is How many WebSocket connection can be created on Client Side.
My Typically Application is html UI based and on the server i have rest based services. I need to track whether
Session timeout has happed or not
Whether Connection to the server is lost or not ? A kind of pooling program to check with connections is alive or not.
So I am creating 2 websocket objects on client and different url for them.
I hope i have implemented it correctly ?
Basically Browser closes the old websocket connection once you opened to new connection to SAME URL(ws://127.0.0.1:8080/WebSocket-context-root/getResource). You can keep small hack like "ws://127.0.0.1:8080/WebSocket-context-root/getResource/"+k. where k is any number/any random string. On server side just ignore the path variable k.
In this way you can open many number of connection at same time. Browser restriction of max-number-connection per domain is not applying here (Tested on Firefox). I tried max 25 parallel connections.
You can use websocket.readyState to check the status of the web socket connection.
onclose Event of the Web socket have reason code for closed connection.
User below code to test number of active connections.
var x=0
var intervalID = setInterval(function () {
websocket = new WebSocket("ws://127.0.0.1:8080/WebSocketApi/web/chat/"+x);
websocket.onopen = function (evt) {
console.log('open')
}
websocket.onmessage = function (evt) {
console.log('msg');
}
websocket.onclose= function (evt) {
console.log('closed');
}
if (++x === 15) {
window.clearInterval(intervalID);
}
}, 1);

TCP Communicator Application?

I am looking for software to help debug client/server software.
I am looking for an application where I can simply connect to a remote host on a given port and send it bytes of data and observe the responses.
Is there such software existing?
Use netcat. There are many different implementations. This is the manual for BSD's netcat.
nodejs do have an high level tcp api you can use to create a 5lines script that wee do the job for you
var net= require('net');
var sock = net.connect(1234, '127.0.0.1', function(){
sock.write(new Buffer("some binary data here"));
sock.write(new Buffer([0,1,2])); // 3 more bytes
sock.on("data", function(buffer) {
console.log("Server response is ", buffer); // display raw response buffer
})
});

Exchange data between node.js script and client's Javascript

I have the following situation, where the already sent headers problem happens, when sending multiple request from the server to the client via AJAX:
It is something I expected since I opted to go with AJAX, instead of sockets. Is there is other way around to exchange the data between the server and the client, like using browserify to translate an emitter script for the client? I suppose that I can't escape the sockets, so I will take advice about simpler library, as sockets.io seems too complex for such a small operation.
//-------------------------
Update:
Here is the node.js code as requested.
var maxRunning = 1;
var test_de_rf = ['rennen','ausgehen'];
function callHandler(word, cb) {
console.log("word is - " + word);
gender.gender_function_rf( word , function (result_rf) {
console.log(result_rf);
res.send(result_rf);// Here I send data back to the ajax call
setTimeout(function() { cb(null);
}, 3000);
});
}
async.eachLimit(test_de_rf, maxRunning, function(item, done) {
callHandler(item, function(err) {
if (err) throw new Error(err);
done();
});
}, function(err) {
if (err) throw new Error(err);
console.log('done');
});
res.send() sends and finishes an http response. You can only call it once per request because the request is finished and done after calling that. It is a fairly high level way of sending a response (does it all at once in one call).
If you wanted to have several different functions contributing to a response, you could use the lower level functions on the http object such as res.setHeader(), res.writeHead(), res.write() (which you can call multiple times) and res.end() (which indicates the end of the response).
You can use the standard webSocket API in the browser and get webSocket module for server-side support or you can use socket.io which offers both client and server support and a number of higher level functions (such as automatic reconnect, automatic failover to http polling if webSockets are not supported, etc...).
All that said, if what you really want is the ability to just send some data from server to client whenever you want, then a webSocket is really the better way to go. This is a persistent connection, is supported by all modern browsers and allows the server to send data unsolicited to the client at any time. I'd hardly say socket.io is complex. The doc isn't particularly great at explaining things (not uncommon in the open source world as the node.js doc isn't particularly great either). But, I've always been able to figure advanced things out by just looking at a few runtime data structures in the debugger and/or looking at the source code.

socket.io-Difference between emit functions

I am a newby in socket.io and i really wonder like in this code example what is socket.emit and io.emit exactly doing? What is the difference of them? Thanks in advance
var io = require('socket.io')(http);
io.on('connection', function(socket){
socket.emit('asd', data);
io.emit('das', data);
In your code, socket is one connection to one user, whereas io is global so will send to everyone.

socket.IO: how can i get the socketid when i emit to a specific client in socket.io 1.0

I want to emit a action to a specific client using socket.io 1.0.
After reading Sending message to a specific ID in Socket.IO 1.0 , I know I can use:
io.sockets.connected[socketid].emit()
to emit a action to specific person.
But how can I get the socketid?
I used to write like this : socket.id=nickName, but it's wont work.
Simply access id property of socket object:
io.on('connection', function(socket) {
console.log(socket.id);
});

Resources