I’m a beginner with PHP/MySQL and new to javascript as well, I want to make a page that one user could send a real-time request to another user. For example, UserA could see the list of online users, and send a request to UserB (online), UserB’s page would have a popup alert, and UserB has the option to click Yes or No. UserA would receive a popup with B’s response (or if timed out then default to No)
How is this usually done? I guess my questions has 2 parts:
- how to record online/offline users? (should I have a table
recording login/logout times?) - how to usually send message
between 2 users
I don’t know what you are trying to make, but I assume that a private chat is a good example? I’ll pretend that that is what you are doing.
I’ll try to give a decent amount of information where appropriate without turning this into a complete tutorial. Expect a good spattering of links and suggestions to read things.
The Idea
You have one basic idea at the core of your application, User Communication. You need to somehow go from the stateless, asynchronous world of HTTP to a stateful, synchronous one. There are a few ways to do this.
Polling
This is using AJAX to repeatedly request information from the server. AJAX stands for Asynchronous Javascript And XML, don’t worry about the XML bit though, that’s just historical. Basically, AJAX is like making a regular request from a script in a browser, this gives you the advantage of being able to send and receive data from the server without a refresh. This is good for situations that are either user-initiated or can handle a delay. For your case, you would mark the last time somebody took an action, then specify some timeout to decide when somebody is offline. The result of the request contains any new messages that may have arrived since they checked.
Long-Polling/Comet
This is an extension of the polling idea before. This is almost explicitly a workaround for the client-initiated nature of basic AJAX. Comet is essentially just AJAX, but you keep the connection open for a much longer time, allowing the server to push information down. You can find information out on this technique here: Comet. However, there are a few issues with this technique:
.abort()on the request and you have to re-open it.You have to use essentially the same techniques as before, due to the fact that you still have disconnects that are part of the normal usage. The difference is that the server has to keep the connection open for as long as necessary. Depending on what technology you use in the back-end, this can be anywhere from trivial to difficult and can potentially inflict a significant performance cost if not handled carefully.
WebSockets
WebSockets are essentially the previous solution, but standardised and implemented properly. The essence of WebSockets are that you can make a regular request to the server, but ask that be upgraded to a TCP connection. The word upgrade here is a bit misleading, as it technically just drops the HTTP stuff from the connection and leaves you with the raw underlying connection.
WebSockets are designed for persistent connections and are full-duplex (communication both ways).
There are, however, two major downsides: They are new, which means that support isn’t that widespread, most modern browsers support them, but you can’t rely on everybody having an up-to-date browser. Server side support is required. This means that you have to have a system that is capable of upgrading the requests and handling them efficiently, again, because this is a new technology, support is limited, with socket.io being the major one. socket.io is a node.js module and therefore might not be suitable for you. Other languages/technologies are out there, but there is decidedly less support available.
(EDIT: Reread your question and saw that you are using PHP, you can use this library: php-websocket for websockets. I haven tested it, so caveat emptor)
With WebSockets, you can use the connection dropping as an offline signal and you just send the data around as you need to. However it is wise to fall back to a older, more reliable solution if WebSockets aren’t available, so try to keep that in mind.
There a still numerous issues around all of these solutions that you will need to solve, but hopefully this should give you some idea of where and how to start. At the very least it provides a number of phrases you can google (or bing or duckduckgo or ask)!