Node create a unique url and bind socket.io to it.
var io = require("socket.io").listen(server, {path: req.originalUrl});
When a client connects also binds his socket.io-client to that url
var socket = io('192.168.1.101:3000', {path: window.location.pathname});
I don't have problems and everything works great.
When a client performs a particular action, server do
socket.broadcast.emit("foo"); //I made console.log here and it prints
client-side:
socket.on("foo", () => console.log("okay"));
The problem is that client-side "foo" event is almost never fired. Sometimes it is fired but only in particular occations. For example it happened that a socket.io-client auto-reconnect to server and then the event is fired.
I don't know if the problem is related to this, because for example socket.emit works, but when another client connects I always get this
error
socket.broadcast.emit("foo");
broadcasts a message to all connected clients EXCEPT the one specified by socket. If you want to broadcast to all connected clients, then use:
io.broadcast.emit("foo");
You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.
Related
I'm developing a simple website, where the client and server communicate over web sockets. I'm using nodejs and the socket.io library for the socket communication
Specifically, my server works as a middleware between an mqtt broker and my client. So on one hand, my server connects with the mqtt broker to consume messages and on the other hand delivers these messages to the connected clients over web sockets. I'm using the node mqtt library for the mqtt communication.
My codebase is fairly large, so to give you a feeling of how my code looks like, I will show this example, which should be straightforward to understand:
const io = require("socket.io")(port);
handleRequests(io) {
io.on("connection", (socket) => {
logger.info(`New client connected: ${socket.id}`);
this.clients[socket.id] = { // track clients and subscribed topics
topic: '',
};
this.numberOfUsers++;
io.sockets.emit("onUser", this.numberOfUsers);
this.handleChange(socket);
this.addToSubscribedClients(socket);
this.removeFromSubscribedClients(socket);
this.handleDisconnect(socket);
this.sendMqttMessageToClient(socket);
});
}
This is my "main" function, where as you can see, I'm initializing an io object and using it later by passing it to the handleRequests function. Each time a new client connects, I'm calling the callback function where I call the five other functions and passing the socket object as a parameter, which should be fine I guess. I'm passing the socket object as a parameter because I need it to later call socket.emit in order to send back message to a specific client, since the socket object is unique for each client.
This works great until more than ~ 30 clients are connected. I'm trying to debug this for 2 weeks now and can't figure out why this is happening. I'm testing this by opening multiple tabs in my browser. I start with one client and then increase the number of clients/tabs. At some points, I notice that some clients receive no values from the server but other clients still do, which is incorrect since all clients should receive the values in real time.
I noticed that the clients, which are not receiving values have other ids than the ones stored on the server. I tested this with a simple console.log() on both clients and server. How this is happening? I'm very positive that I'm sending the ids correctly since there are other clients, which still receive values from the server. My guess is that the server is somehow disconnecting some clients automatically, because if a client reconnects then a new id will be assigned to it automatically.
Does anyone have any idea why this is happening? and why it works fine with the first ~30 clients and starts to occur when many clients are connected? This issue is very hard to debug since the code works fine for a small number of clients and no errors are thrown when the bug occurs, so I'm hoping that someone had this before.
Edit
Now I just found that i can print a reason for socket disconnection. When I do that, ping timeout is printed, which I don't understand because when I have one single or few clients connected then this error does not happen.
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
I'm building MEAN application with socket.io. When page is just loaded, socket connection is established and kept live while user navigates to various pages, thanks to single-page nature of the app.
The user information is available in my socket connection thanks to passport.socketio.
However, when user logs in or out, I want the connection to be re-initialized, since otherwise socket will contain obsolete data about the user. Currently, I tried to implement it in this way: when user logs in / out, server disconnects this particular client's socket by calling socket.disconnect();.
On the client side, I listen for disconnect event, and try to re-establish the connection, like this:
_socket.on('disconnect', function(reason) {
_socket.connect();
});
Ok, now, when user logs out or in, server disconnects the client, this client connects back, and user information in the socket is up-to date. So far, so good.
But, consider different case when connection is broken: server is restarted. Previously, it "just worked": when I stop my server, connection is broken, but when I start server again, connection is automatically re-established. But after I've added my _socket.connect(); call, it doesn't work anymore: connection is still down until I refresh the page in the browser.
I've checked that when server calls disconnect();, the reason given to disconnect handler is: io server disconnect. And when server stops, the reason is: transport close.
Ok, then, I've implemented my disconnect handler as follows:
_socket.on('disconnect', function(reason) {
if (reason === 'transport close'){
// don't do anything special
} else {
_socket.connect();
}
});
Now it works. But, all of it seems as absolute dirty hack. At the very least, the reasons given (io server disconnect and transport close) seem to be just human-readable strings, so they might change in the future, and this will cause my code to stop working. And, well, there should be better way to do this; I must miss something essential, but unfortunately I can't find any good documentation on socket.io.
So, the question is: what is the correct way for the server to reconnect some particular client?
Additionally, if you have any recommendations on resources to learn about socket.io, I'd appreciate it very much as well.
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.
I have a page on which users connect to my node server with socket.io but I only allow them to have one open socket.io connection to the server (by passing along their account id when authorizing them and storing it in an array) and this works fine 99% of the time. The problem is that sometimes when users disconnect, the serverside disconnect event doesn't fire for some reason, so I can't clear their account from the array of clients, which ends up with them being locked out.
Is there a way for me to check if their old socket connection (which I have the ID of) is still active? (So if it isn't I can clear their old connection and let them connect again)
Make sure that heartbeats is set to true (the default). If the timeout lapses, disconnect should happen automatically. However, there was a bug report about heartbeats not working. Make sure you have the latest version of Socket.IO (I'm not sure the status of the bug).
If you still need help, you could send a ping to the old connection when the user tries to reconnect:
Emit a 'ping' from the server, and reply with 'pong' from the client if the connection is still alive. If the ping times out (after say, 20 seconds), drop the connection manually so the user can reconnect. You could use the ping to notify the original client that another client is trying to connect, and raise some UI to that effect.