Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to notify a non-web application about a change on a web page?

Let's say I have two applications which have to work together to a certain extent.

  1. A web application (PHP, Ruby on Rails, ...)
  2. A desktop application (Java, C++, ...)

The desktop application has to be notified from the web application and the delay between sending and receiving the notification must be short. (< 10 seconds)

What are possible ways to do this? I can think of polling in a 10 second interval, but that would produce much traffic if many desktop applications have to be notified. On a LAN I'd use an UDP broadcast, but unfortunately that's not possible here...

I appreciate any ideas you could give me.

like image 310
Daniel Rikowski Avatar asked Mar 01 '23 18:03

Daniel Rikowski


2 Answers

I think the "best practice" here will depend on the number of desktop clients you expect to serve. If there's just one desktop to be notified, then polling may well be a fine approach -- yes, polling is much more overhead than an event-based notification, but it'll certainly be the easiest solution to implement.

If the overhead of polling is truly unacceptable, then I see two basic alternatives:

  1. Keep a persistent connection open between the desktop and web-server (could be a "comet"-style web request, or a raw socket connection)
  2. Expose a service from within the desktop app, and register the address of the service with the web-server. This way, the web-server can call out to the desktop as needed.

Be warned, though -- both alternatives are chock full of gotchas. A few highlights:

  • Keeping a connection open can be tricky, since you want your web-servers to be hot-swappable
  • Calling out to an external service (eg, your desktop) from a web-server is dangerous, because this request could hang. You'd want move this notification onto a separate thread to avoid tying up the webserver.

To mitigate some of the concerns, you might decouple the unreliable desktop from the web-server by introducing an intermediary notification server -- the web-server could post an update somewhere, and the desktop could poll/connect/register there to be notified. To avoid reinventing the wheel here, this could involve some sort of MessageQueue system... This, of course, adds the complexity of needing to maintain the new intermediary.

Again, all of these approaches are probably quite complex, so I'd say polling is probably the best bet.

like image 68
joshng Avatar answered Apr 26 '23 03:04

joshng


I can see two ways:

  • Your desktop application polls the web app
  • Your web app notifies the desktop application

Your web app could publish an RSS feed, but your desktop app will still have to poll the feed every 10 s.

The traffic need not be huge: if you use an HTTP HEAD request, you'll get a small packet with the date of the last modification (conveniently named Last-Modified).

like image 42
Christian Lescuyer Avatar answered Apr 26 '23 01:04

Christian Lescuyer