Skip to content Skip to sidebar Skip to footer

Pushing Data To A Client

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

Solution 1:

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).

Solution 2:

There are 3 solutions to your problem,

First (easiest) Start off by using the pull technique, so check every seconds for new items at the database level.

Second You can implement the "add to database" form as a modal and once the user submits the form, you send an ajax request to update your list (as long as it is the same user doing the request)

Third investigate using sockets, it requires an open socket, and adds quite a bit of complexity to and you will need to look at socket.io project, or similar

Solution 3:

You've got a bunch of options for actual push and depending on what browsers (and browser versions) you need to support you'll likely need more than one approach.

The simplest thing you can do is long-polling friendly comet (a pun on ajax) where your client opens a connection to the server and just leaves it open. The server responds whenever it has data for you. However, this means you need some mechanism for handling a relatively large number of open connections on your server and likely some async event handling.

WebSockets are definitely an option, but WebSockets are fairly complicated to implement, have a bunch of competing standards and support. However, they're extremely powerful and give you ways to send and receive data.

If you only need to push data to your clients and are okay with normal HTTP POST or AJAX user actions then you can, and probably should, look into SSE or Server Sent Events:

https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events

Post a Comment for "Pushing Data To A Client"