How to check if the user is online?

10

I'm developing a project in PHP and using MySQL as a database. I only have one question about how to chat: How to know which users are online right now?

    
asked by anonymous 17.03.2014 / 18:08

4 answers

13

You can use a set of techniques to get this information.

Client-side:

  • Listen for closing event of the browser window, and when user close, send a signal to the server, indicating that the user has a window less open, and on the server will have to count how many pages the user has open

To implement the chat:

  • The option that will give you the widest scope in terms of browsers, is called long-polling , but it will also give you more work. See more about the technique: link

  • Another option that will be easier is to use websocket , and create a server connection, which will be able to send messages when any chat event occurs ... type send a message

17.03.2014 / 18:18
4

The question has already been marked as answered, but as a reference for who to stop here in the future: use websockets , they are already for some time and are the most suitable solution for real-time communication between browsers and servers (a chat for example). Their basic idea is to keep an open connection constantly between clients and the server to allow real-time communication, you could emulate this using ajax and long-polling , however the performance difference is relevant (worse in this case) and long-polling is a workaround which is usually only preferable in the absence of better mechanisms.

One of the only problems with using websockets would be the lack of support in browsers, a problem almost irrelevant since now almost almost all major is already supported and most (if not all) of the websockets libraries provide automatic fallback for long-polling if necessary.

In case the problem of verifying who is online would be simple, see an example using socket.io :

// código do servidor realtime
var onlineClients = {};
io.on('connection', function (socket) {
    onlineClients[socket.id] = socket;

    // on disconnected, unregister
    socket.on('disconnect', function() {
        delete onlineClients[socket.id];
    });

    // usado em um exemplo mais abaixo
    socket.on('enviarMensagem', function(mensagem) {
        io.emit('novaMensagem', mensagem);
    });
});

// código do cliente (browser)
<script>
  var socket = io();

  // usado em um exemplo mais abaixo
  socket.on('novaMensagem', function(mensagem) {
      alert(mensagem);
  });
</script>

When a client connects or disconnects your realtime server gets instantly aware and you can take actions based on it. Say for example that you want from PHP to send a "hello" to everyone who is on:

// código do servidor php, usando a elephant.io lib
$elephant = new Client($socketServerUrl);
$elephant->initialize();
$elephant->emit('enviarMensagem', 'ola');
Your php server would send a 'sendMessage' event to your realtime nodejs server, which in turn would issue the 'newMessage' event to all connected clients, upon reaching them a alert(mensagem); will be triggered with the sent message (see code snippet with the comment "used in an example below").

    
14.02.2016 / 22:00
0

The ideal would be to use Node.js (with chat inside an iframe, connecting to Node.js which would be a separate PHP server), and Sockets.io as basic technology. There are even several example chats written in socket.io that could be used as a base (example: link )

    
01.06.2016 / 06:47
-2

Give preference to use the websocket, but if you have a shared host that does not support this, follow something that can help.

If you create a field (EX: time) and in each text message it updates this field with value of php time (EX: $ time = time ()) in the area where you will get the result use the next select

  

$ query="SELECT * FROM users WHERE time > UNIX_TIMESTAMP () - 300";

UNIX_TIMESTAMP () - 300 means 5 minutes plus you can change to the time you want in seconds EX: UNIX_TIMESTAMP () - 60 = 1 minute

    
14.02.2016 / 22:51