Callbacks are not supported when broadcasting - node.js

Here I am facing one issue with Callbacks in socket.io with nodejs
io.sockets.to(usersocketid).emit('receivemsg',
{'success':'1','data':message},function(deliverycb){
console.log('delivery call back');
console.log(deliverycb);
});
Where i am facing error of Callbacks are not supported when broadcasting so anyone have solution of this.

Since you are using callback for acknowledgement,and according to socket.io documentation.
Acknowledgements are not supported when broadcasting.
Acknowledgements are not supported when emitting from namespace.
I would say, if you need to send acknowledgement to the server, then emit another event from the client-side.

First, get socket object from id, which you can refer to Socket.IO 1.0.x: Get socket by id
Then with the socket object, socket.emit('type', 'message', ack =>{});

It's possible you were trying to emit the acknoledgement from the server side, without specifying the socket.id;
By the way, you can avoid that error, moving the acknowledgement request to the clint-side and just work the callback on the server-side.

Related

How I can use io.emit )

I am working on twitter bot with node.js. I wanna realtime data, but i don't work socket.io How can i use io.emit or io.socket.emit ?
My code is
output
socket.io website is down and the documentation is moved to here: https://github.com/socketio/socket.io/tree/master/docs
Assuming io is the Socket.IO namespace on server side.
io.sockets.emit is equivalent to io.emit, and io.socket.emit is not a documented function (I see that you used io.sockets.emit in your code example). What this does is it sends an event to all connected clients. io.emit does not have a documented return value.
Flag sockets is for backward compatibility, in most code examples, you'll see io.emit without sockets flag. Other flags includes volatile and local.

socket.io-redis not forwarding messages to sockets on a namespace

I have a solution where an application is emitting messages using socket.io-emitter and a socket server is handling the messages.
The socket server receives messages from the client browser without an issue however it does not pickup any messages from the other application which are sent over the redis adapter.
I've debugged the redis adapter and can see that the messages are being received and I can also see that they are associated with the correct namespace. They just appear to not be firing the socket.on() event
The server code has more going on however basically boils down to the following
io.adapter(redisIO({host: redisHost, port: redisPort}));
io.of('/mynamespace').on('connection',
function(socket) {
// This message never gets fired
socket.on('other-server-message',dosomething);
// This message works fine
socket.on('message-from-browser-client',dosomethingelse);
}
);
There isn't much documentation around so any help would be great
I eventually realised what the issue was. I was misunderstanding what the socket.io-emitter was doing.
I was sending a message from the emitter and then trying to capture it on the server and then push it out to the clients. The emitter is actually broadcasting directly to the clients in the web browser as if it was just another server socket (which is what I wanted).
Soon as i updated the client code to check for the message it worked perfectly

Is passing socket.id from server to client possible security issue?

I'm using node.js with socket.io. Every socket connected to server has its own socket.id. I'm wondering if it is possible security issue if I pass socket.id from server to clients?
i don't believe that's a problem there aren't any known vulnerabilities to this, server-side socket.io is using ID to identify websocket connections just to "know" where to emit events and from where an event was emitted.

How do I completely destroy a socket.io connection?

I'm creating a browser chat from Socket.io and Node.js. Everything has been running smoothly, but I appear to be having a problem with disconnecting sockets. When I run socket.disconnet();, the server runs the socket.on("disconnect", event, but it doesn't actually remove the socket from internal listeners.
When I run socket.disconnect(); on a socket, the socket no longer recieves any new messages, but when the "disconnected" user sends a message, the server receives and sends it back to all clients. I want to create a proper /kick command but it's difficult when I have to restructure all of my code just to accomidate for a simple function.
Commands like socket.connection.destroy();, socket.end();, and socket.transport.destroy(); are invalid and undefined. Does anyone have any suggestions? I've been working on this problem for days and I haven't found any answer other than to set a shutup boolean to the socket and tell the message event to ignore specific sockets. Is this the best way? What happens if the user starts editing javascript code and I need a way from receiving other events from a client?
Well you can see if the socket is connected or not. If socket is connected you can emit the data and vice versa. :)
Hope it helps..!
YourProject.sockets.on('connection',function(socket){
setInterval(function(){
if(!socket.disconnected){
socket.emit('entrance',{message:'Hey Bro'});
}
},10000);
});
have you tried to interrupt thread ? That should end all I/O operations with an Exception.

Bind event to all socket.io connections

I know it's possible to emit events to all connections using io.sockets.emit('some event'), but can I also globally bind events to all present and future connections? Would probably look like
io.sockets.on('feedTheDog', function (data) {
// Someone told me to feed the dog..
});
Also, can I remove event listeners with .removeAllListeners() with Socket.IO?
I also don't think you can bind events to future connections, but yes, you can use removeAllListeners with socket.io. They ported node's event emitter to the browser, so it's exactly the same api there. Here's the code for it: https://github.com/LearnBoost/socket.io-client/blob/master/lib/events.js
You can use socket.io-events and attach middleware functions to handle messages for all socket connections, without actually attaching a handler to a socket.

Resources