?Briella 2014-08-03 15:27 采纳率: 0%
浏览 52

将数据推送到客户端

I like using PHP and JavaScript with jQuery.

I am displaying a list of items out of a SQL database using PHP. I have a button that opens up a new page in a new tab that will add an additional item to the database.

How would I add the newly added item in the database to the currently displayed list on the first page without refreshing that page?

I can force the page to make an ajax request every few seconds but I don't think that is very good practice.

I have been looking into sockets - however I don't know how to apply that knowledge to my current situation.

  • 写回答

3条回答 默认 最新

  • elliott.david 2014-08-03 15:35
    关注

    If you want the server to push the information back to the client, Comet may be what are you looking for.

    Recent browsers support WebSockets, which enables the server to communicate with clients without the need for the client to make regular AJAX requests. Older browsers don't support WebSockets, so if you need to support them, you may be interested in frameworks which allow to fallback to more basic techniques, such as long polling on those browsers.

    Note that there are libraries which make it really easy (well, some don't) to integrate WebSockets or Comet in your web application, so don't reinvent the wheel and stick with the one which is available in the PHP framework you use right now. For example, if it's Laravel, a simple Google search leads me to BrainSocket, "a Laravel package that allows you to get up and running with real-time event-driven PHP apps using WebSockets."

    In all cases, you're not expected to use sockets (unless by sockets, you mean WebSockets in your question).

    评论

报告相同问题?