How I can use io.emit ) - node.js

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.

Related

node js socket.io problem that will only respond to the last connection

Basically, I know that 'socket.io' is a one-on-one communication between the server and the client.
But when I tried to do it myself, it wasn't
Connect to the socket using a chrome browser.
Connect to the same page using the secret tab.
in the same situation as described above
If an event occurs on client 1, it works on client 2.
How can I generate an event only on each client?
I connected different rooms to each client.
//server
socket.join(`${socket.id}`)
socket.to(`${socket.id}`).emit('event')
It will not work anywhere.
This method:
socket.to(room).emit(...)
sends to all sockets in the room EXCEPT the one represented by socket.
If you want to send to everyone in the room, then use this:
io.to(room).emit(...)
Refer below link:
1.Rooms Concept
https://socket.io/docs/v3/rooms/
2.Emit cheatsheet
https://socket.io/docs/v3/emit-cheatsheet/

Callbacks are not supported when broadcasting

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.

Pure socket logic

I have a nodejs app, and every client has an open socket connection, the reason i am using sockets because I need to update the data on the client whenever the data in the database changes by an external process.
However every other operation in my app doesn't require a socket connection and mostly initiated by the client (CRUD operations), But i am confused about one thing since I always have an open socket connection, wouldn't it be better to use that socket connection for every operation and make the app with pure socket logic?
When using websockets maybe it's fine. But if socket.io switches to XHR (AJAX) transport it might be irrational.
Take a look at the differencies of these two approaches:
In case of simple AJAX (without socket.io) when you want to get some info from server, or change something on a server, you send GET or POST request,
and server responses. Everything's fine.
But in case of socket.io (XHR transport) there is one request to send data, and another to get the response.
(You can make your own experiment - write io.set('transports', ['xhr-polling']); and try to send data to the server and make server respond -
you will see 2 AJAX requests in the Network tab)
So instead of one AJAX request socket.io makes two requests.
This is not because socket.io is bad. This is a feature of sockets approach. This approach is good if you want one side (client or server) to send messages independenly from the other. This is what socket.io does very good.
But if you want to do "request-response" stuff it's the best to use simple AJAX because of traffic economy (note that I compare simple AJAX to socket.io AJAX. Websockets - is another story).
But since this question is about approaches and can't have 100% "yes" or "no" answer, there are might be different opinions.
Sorry for English. I tried to write as clearly as I could :)

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.

Send messages to specific socket without socket.io in node.js

I'd like to send messages through a socket to specific clients but can't seem to figure out how to do it without socket.io. The reason I can't use socket.io is because I'm trying to connect to a flash client, and I've read in numerous threads here on stackoverflow that socket.io isn't meant to connect to a flash clent. I have working code that will connect a node socket server to a socket client created in flash (actionscript 3), but it broadcasts the info to everyone connected to the socket. What I thought was if there was some way to send info to specific socket ids then I'd be in business. I looked in the docs and didn't find any info on how to do this, but then again I couldn't find any documentation about specific socket ids in the docs either, yet using "socket.id" will give the id so I'm wondering if there is some undocumented way to do this.
And just FYI this is for a card game where each player connected just gets their cards sent to them. The game logic is taken care of server side and I have that working. Now I just need to get each player only the info they need. Thanks.
Darryl
I know for sure you can use Adobe® Flash® Socket in conjunction with socket.io. When you look at the available transports(browsers) you will notice that socket.io also supports Adobe® Flash® Socket. But to be honest I do not know the exact details about this, but I think you can learn them by studying socket.io's specification page.
When looking at socket.io's WIKI I also found there already is a library to use socket.io with Adobe® Flash® Socket. The library is called FlashSocket.IO which I believe can help you although I don't know any flash myself.
Adobe® Flash® Socket is standard disabled in the latest socket.io branches, but you can enable them easily by studying configuring socket.io. This page tells you how to enable all tranports which I will summarize below:
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
If you have not yet had suffice answer(I think this should help you) I think you could also file an issue at socket.io's issue page, because I find the socket.io's maintainers are super friendly and will tell you how to accomplish this without the least pain.
Finally to accomplish this using plain node you will have to keep track of all the active socket connections(sessions) yourself and only send that message to that specific socket. This is a mundane task, but I think there are more than enough tutorials available that teach you this(you could also look at socket.io how they achieve this, although this is a pretty large project/codebase).
I hope this answered your question. If not, I recommend you to ask a question in the comments for example.

Resources