Ajax chat polling gets duplicates in the message receiver interface in fast chat

I developed javascript chat (php on the backend) using:

1) long-polling to get new messages for the receiver 
2) sessionStorage to store the counter of messages  
3) setInterval to read new messages and if sessionStorageCounter < setIntervalCounter then the last message is shown to receiver.
4) javascript to create,update and write the chat dialogues

The module works fine, but when users have a quick chat, the front part of the recipient receives two or three identical messages (neither the counter works, nor the request provides double inserts).

The code seems to be correct (why I don’t provide the code), so the delay may be caused by a time delay (nothing changes when reducing the interval delay).

Do you think that the above scheme is bad practice and which scheme do you think will fix the errors?

+4
source share
2 answers

My approach, if you solve it yourself (as opposed to using an existing library that already processes this), would be:

  • Assign a unique identifier (GUID) to the server for each message as it arrives.
  • On clients, save the identifier of the last message received.
  • When polling new messages, do this with the identifier of the last successful message. The server then responds by finding this message in its own queue and playing back all subsequent messages.
  • To protect against “discarded” messages, each message may also contain the identifier of the immediately previous message (allowing the client to perform a consistency check)

repolling , , . , . , , .. - ", ", .

+5

, setInterval, , , . , , .

0

Source: https://habr.com/ru/post/1584234/


All Articles