Jump to content

Recommended Posts

I have done a bit of research on this and i fear the answer is no... but is it possible to update a page on a node/express application without needing to look at websockets socket.io?

It feels like such a standard thing to want to do but my research is saying that it is not possible.

An example is:

In my application, i hit an API and display stuff on the page.  I just want to hit the api externally and update stuff on the page.  I know i can use polling, but i have always turned my nose up at that - feels very brute force.

Any pointers here would be lovely.

 

Thanks All

Link to comment
https://forums.phpfreaks.com/topic/326474-update-page-when-api-is-hit/
Share on other sites

This is typically where a pub-sub solution helps.

As you mentioned, the old school ways of hacking ajax to do this involved polling.  

Websockets were introduced to provide a better solution, but have the downsides of being an entirely different protocol, requiring a websocket server.  Some people get somewhat around this by utilizing a websocket PAAS, at least for me, the best known of which is Pusher.  When I last utilized it, the economics were very reasonable, and for a site with known relatively low traffic there's a free tier.  There are alternatives, but if you really need bi-directional communication, websockets are the best solution.

If however, this is simply a "push" requirement to "publish" to the "subscribers" then there is now a standard feature you can use known as "server sent events (SSE)".  Support for this is very good at this juncture as you can see here: https://caniuse.com/eventsource

SSE sounds tailor made to solve your problem here, as your updates can be pushed out to all your clients.  Here's the MDN page for the feature.

I know of a few php libraries that that have all the serverside plumbing taken care of:

I will caution that there is no free lunch.  SSE's are better than (long) polled ajax, but they still require a dedicated socket connection per client.  Anything like this requires significantly more resources, but if it is an essential feature of your application, SSE could be tailor made for what you want, although you should also evaluate using Pusher as an alternative.  

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.