I am currently working on an instant messaging system. But, I have a problem with the optimization. Here is my current code:
<script>
setInterval('load_messages()', 500);
function load_messages(){
$('#messages').load('loadMessages.php');
}
</script>
Apart from the fact that, it is better to use an AJAX request to ask only for messages after a defined timestamp. Is there a way to pass a packet from the sender's page to the receiver to refresh to avoid spamming the database. Here is a schema explaining what I would like to do: Is there a way in php, apache or javascript to do this?
Thanks for reading
The technique you use is called short-polling. It's basically spamming the server until the server has something new to show you. Looks something like:
Client: cookie?
Server: no
Client: cookie?
Server: no
Client: cookie?
Server: no
Client: cookie?
Server: yes; here's the cookie: ????
Client: cookie?
Server: no
...
This is really inefficient, as you understood yourself; I don't know of any websites that still use short-polling.
You have other options. The easiest one (given you are using PHP) is long polling. Basically, you send a request, and the server just stalls the request, and once a new message comes the server sends the response back. This allows you to get messages immediately and not send so many messages:
Client: tell me when you have a cookie...
[loads for 1m 32s]
Server: the cookie has come [sends response]
Client: tell me when you have a cookie...
...
However, Apache handles concurrent requests by creating a new thread per request. So if you have 2 users in your chat, that's fine. But let's say you have many rooms and a total of 100,000 users long-polling your server. Your server probably can't handle that many threads and will crash. (NodeJS is very popular for realtime applications for its event-driven architecture and built-in asynchronism.)
But there are other much, better, options:
Your context: SSE is quite easy to implement in PHP without extra libraries; here's an example. Websockets, on the other hand, are a bit more complex to set up (may require a third-party library like rachet), but because of how well it works that's probably what I would consider.
Your question: Is there a way to pass a packet from the sender's page to the receiver to refresh to avoid spamming the database? If I were to exactly answer your question, SSE.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.