dongzaizai2015 2017-11-04 17:01
浏览 57
已采纳

开发实时更新网站的最佳方式[关闭]

I am trying to develop a website that shows a post automatically when it was posted by another user, or a user receives notification when a message has been sent to him, basically something that works like Facebook. Without needing to reload the page or hitting F5.

I have been working with Javascript and PHP for years and the only way to keep page updated as far as I know is by using AJAX requests continuously.

This is an example (Checking message from dataBase):

setInterval(function() {
    $ajax({
      url: "http://myweb.com/checkmessage.php",
      type: "POST",
      data: {value: userID},
      dataType: "json",
      success: function(result){

              //If true do something and update message inbox

         }
    });
}, 500);

This basically sends requests every 0.5 seconds to my PHP file where I have access to my database and I check for new messages and return them. Thanks to this code I can get message notification in real-time without having to update my website.

But honestly, from performance perspective it's very bad practice to have hundreds AJAX requests sending every 0.5 seconds at once, it slows down the website.

That's why I am asking here if there is a better way to implement this, I have been checking Facebook and the way it was implemented pure mystery to me. I still don't know how do they do it, to have such massive updates on a page and just a few calls (You can check by yourself opening facebook waiting till it loads then open google developer tools, go to network and see it by your eyes!).

  • 写回答

4条回答 默认 最新

  • dongpan2788 2017-11-09 11:25
    关注

    You would use websockets for such a scenario. Using HTTP, AJAX polling or AJAX long polling would essentially add a lot of overhead on the network due to many request/response cycles being involved which in turn intrinsically involve all the connection setup and validation steps each time.

    However, implementing this from scratch is usually not an option. You will find a lot of PaaS, IaaS options. I've used Ably before and it seemed to perfectly solve the purpose.

    Basically even if you are trying to implement the underlying functionality yourself, you would need to use the Publish/Subscribe architecture using which an event is pushed everytime something changes (like a user posts a new post on FB) and this event will in turn be broadcasted to all the subscribers. On the subscribers end, you would implement the logic to handle the event.

    Websockets are a perfect solution to this use-case because they allow full-duplex and persistent connections, so the server can utilise the push paradigm to continually push updates as the data updates.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?