socket.io-Difference between emit functions - socket.io

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.

Related

Socket.io api inconsistency or how does it work?

When I do:
socket.join('room');
socket.to('room').emit('online');
the 'online'-event is never received on the client side.
When I do:
socket.join('room');
io.to('room').emit('online');
the 'online'-event is received on the client side.
When I do:
socket.join('room', () => {
socket.to('room').emit('online');
});
the 'online'-event is never received on the client side.
The docs state the following:
http://socket.io/docs/server-api/#socket#to(room:string):socket
If I read this the call to:
socket.to('room').emit('online');
should be emitted to the room.
If I use the call:
socket.broadcast.to('room').emit('online');
the event should emit to the room except the socket.
Now it looks like that:
socket.to('room').emit('online');
and
io.to('room').emit('online');
are different. But what I read from the docs I read that they are the same.
Can someone explain the differences and why behave different?

How does socket.io send binary data within a nested javascript object

anybody know the underlying implementation details of how socket.io can send binary data nested within a JS object?
For example, the example on their website on the socket.io 1.0 announcement is
var fs = require('fs');
var io = require('socket.io')(3000);
io.on('connection', function(socket){
fs.readFile('image.png', function(err, buf){
// it's possible to embed binary data
// within arbitrarily-complex objects
socket.emit('image', { image: true, buffer: buf });
});
});
I'm integrating webrtc into my site and no webrtc libraries seem to support this functionality, so I want to roll it myself. Reading through the socket.io 1.0 source was quite dense though, so any higher level overview or pointers on a better way to approach the source are appreciated!
clarification: using base64 encoding is not an option for me. I've tried it for my specific application and the performance impact of encoding was too large.

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 not working inside zmq function

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

Resources