Adamhumbug Posted yesterday at 08:13 PM Share Posted yesterday at 08:13 PM 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 Quote Link to comment https://forums.phpfreaks.com/topic/326474-update-page-when-api-is-hit/ Share on other sites More sharing options...
gizmola Posted 22 hours ago Share Posted 22 hours ago 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: https://github.com/hhxsv5/php-sse https://github.com/clue/php-sse-react 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. Quote Link to comment https://forums.phpfreaks.com/topic/326474-update-page-when-api-is-hit/#findComment-1647769 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.