Javascript auto update page?

前端 未结 3 1894
轻奢々
轻奢々 2021-01-07 12:24

Can anybody give me a direction, I want to know how to do auto updating pages like facebook have for new status updates, new likes etc. Ie., if you have a status open in a w

3条回答
  •  心在旅途
    2021-01-07 12:56

    These are done using a PUSH model (subscribe / publish).

    The client side first subscribes by issuing an AJAX request. This AJAX request stays alive indefinitely. When the server receives a new like / comment, it publishes this update to the client side thereby ending the AJAX request. The client receives this update and isntantly issues another AJAX request.

    It is broadly categorized as Comet.

    Once before, I had implemented a Comet web chat application and wrote a somewhat technical write up of what went into it. You can read it here, if interested.

    Comet Web Chat Application

    Edit:

    A heartbeat mechanism (PULL model) is definitely easier to implement, but a PUSH model is far more efficient.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题