0

I got a very simple socket.io application where I'm able to see when a user connects to my site and when a user leaves. The first user is able to see everybody that gets in, however the second will only see the people coming in after him, and so on.... What I want the user that connects to see are all current connected users

Server.js

var io =  require("socket.io")(http);

var users = [];

io.on("connection", function (socket) {
console.log("User connected", socket.id);

Some loop to see user connected I think?!

socket.on("user_connected", function (username) {
    users[username] = socket.id;
    console.log(users);
    if (username !== 'null'){
    io.emit("user_connected", username);
    }
});

socket.on("send_message", function (data) {
    io.emit("new_message", data);
});

socket.on("user_left", function (datan) {
    io.emit("user_remove", datan);
    console.log("user left", datan);
});
});

site.html

window.onbeforeunload = function () {
io.emit("user_left", name);
};

var name =  <?php echo json_encode($_SESSION['displayname']); ?>;
var bild = <?php echo json_encode($_SESSION['userpic']);?>;
var dt = new Date();
var tid = dt.getHours() + ":" + dt.getMinutes();

io.emit("user_connected", name);

sender = name;

io.on("user_connected", function (username) {
var html = "";
html += "<li data-username='" + username + "'>" + username + "</li>";
    document.getElementById("users").innerHTML += html;
});

Looked into this question, but me a dumb coder doesn't understand what 'my_room' is towards my soloution. Sorry for weird question, I'm just kind of lost

  • in your server code your adding to users which is good, but you also need remove them after [disconnecting](https://socket.io/docs/server-api/#Event-%E2%80%98disconnect%E2%80%99) (maybe replace user_left), you can then simply emit `users.length` in that disconnect event – Lawrence Cherone Apr 16 '20 at 19:30
  • removed the session_start guess it was executed earlier since I still got the session value –  Apr 16 '20 at 19:41
  • So my current way of checking when user_left is not the right approach? –  Apr 16 '20 at 19:42
  • oh wait I forgot some of my code –  Apr 16 '20 at 19:43
  • 1
    no you could strip that out as the socket will close and the server will know, then you can just emit the count to other users – Lawrence Cherone Apr 16 '20 at 19:43
  • you need to send a complete list of users to anyone who connects, not just append a single item upon new connection. you never send that list in your code, only updates. that could be as simple as sending `Object.keys(users)` – dandavis Apr 16 '20 at 20:11
  • 1
    lost my mind working around codesandbox's crashes, here's an example https://codesandbox.io/s/sharp-field-oqyfv open the preview in another tab – Lawrence Cherone Apr 16 '20 at 21:25
  • Title says amount, but question says users, are you trying to get the name of all users or total user count? – siniradam Apr 18 '20 at 09:00
  • I'm trying to acheive what Lawrence did in his codesandbox, however It is pretty far away from what I was so I need time understanding how I need to implent it. for example how I add my var name instead of faker –  Apr 18 '20 at 13:39

1 Answers1

0

If you want to send a user list to everyone, create an object like users, add a record each time a user connected, and broadcast to everyone.

if someone disconnects, then first remove that from your collection then send only disconnected user's info to clients, so clients can delete that user from their list.

If all you want is total number of questions, just send io.sockets.clients().length

siniradam
  • 2,727
  • 26
  • 37