In general, I'm completely confused ...
I try to make the chat so that the messages in it are updated as soon as a new line appears in the MySQL table ... At first I wanted to do it with long polling , but with active chat and large network delays ("big ping"), the delays are already more noticeable. Long polls (long poll)

What is better to use?

Then I wanted to on Server-Sent Events , but I need my own server for them, like I didn’t understand anything there in principle ...
Creating real-time applications using Server-Sent Events

There was only one WebSockets but here again I did not understand anything at all ...
Creating real-time applications using Server-Sent Events

In general, there is a code:

 <div id="messeges"> </div> 

It should be updated posts.

MySQL chat table

 `user_id` `text` 

What should be the code for the update?

  • eleven
    Ooh, yes, you have already written almost all the code, the rest will be written to you, of course. No, are you serious? In one delay, the second did not understand, the third too - here you are my div#messages . With this to freelance. Here people ask questions. - xEdelweiss
  • one
    // bash Irishka: well, I, of course, I can help you - I will help, but I will not write the whole diploma for you. dynamic_by: well, this by itself.
  • Yes, hell with the code! I didn’t really count on what someone else would write ... At least tell me exactly what it’s digging in ... Not three technologies to learn right away !!! - Kirpich643
  • 2
    You can search Google for "simple chat on php", but this is a complex search technology, you must first learn it - embarcadero
  • embarcadero with such manners to you here liveexpert.ru .. And what's the use ?! I will write it myself in PHP, but I don’t know how to update the messages in real time .. - Kirpich643

2 answers 2

Use the first method and normal hosting. The delay of 20-50ms will not be noticeable, believe me. You read longer.

    Well, it's not so difficult. Javascript

     (function(){ ///Здесь функция которая делает ajax запрос к серверу и вставляет в ваш див ответ от сервера, а так же вызывает себя допутим раз в полсекунды setTimeout(arguments.callee, 500); }); 

    php

     <? /* Здесь запрос к БД и формирование html. */ ?> 

    Then if the head and hands are there, then everything is simple, the information on the Internet is full, if there is already a chat without automatic updating, then it remains to light a couple of lines of code, and besides, there are plenty of ready-made solutions on the Internet.

    • The server will thank you very much .. - LightShock
    • and what don't you like? the easiest method and working. at least to begin with, if the TS understands how it works, then it will be able and more seriously to use if necessary ... - thunder
    • Once in half a second it is quite normal, if of course this is not a person in phpbb and similar scripts, moreover, if it is not run without need, then all this nonsense. I did once a second, but there were 5 queries to the database and work with large arrays in php, and this is one of the slowest places in it, the server started to fall only at 800-1000 online, invited to cheat the counter (taking into account the time online 10 minutes), after which he made every 10 seconds and silence. And such as the author of the question usually have small sites, so it does not threaten them. - ЬЬ_
    • I understand that you have setTimeout (arguments.callee, 500); re starts the function? And initially it should launch something? - Kirpich643
    • @ Kirpich643 launch of ajax request and the subsequent output to the page, everything is googling - Ьб