Long polling - Message system

前端 未结 4 863
夕颜
夕颜 2021-02-02 01:06

I\'m looking into doing some long polling with jQuery and PHP for a message system. I\'m curious to know the best/most efficient way to achieve this. I\'m basing is off this Sim

4条回答
  •  醉梦人生
    2021-02-02 01:29

    If you only have one user, that's fine. If you don't, you'll run into complications. You'll also run one hell of a lot of SELECT queries by doing this.

    I've been firmly convinced for a while that PHP and long polling just do not work natively due to PHP not having any cross-client event-driven possibilities. This means you'll need to check your database every second/2s/5s instead of relying on events.

    If you still want to do this, however, I would make your messaging system write a file [nameofuser].txt in a directory whenever the user has a message, and check for message existence using this trigger. If the file exists and is not empty, fire off the request to get the message, process, feed back and then delete the text file. This will reduce your SQL overhead, while (if you're not careful) increasing your disk IO.

    Structure-wise, an associative table is by far the best. Make a new table dedicated to checking the status, with three columns: user_id message_id read_at. The usage should be obvious. Any combination not in there is unread.

提交回复
热议问题