Question

i have a simple data flow that works something like this:

clients sign up to an event creator server, 3rd party issues a post request to the server, and the server then sends an event to all its listeners (the clients).

to send the events i thought i'd use server sent events, but i'm having problems because the script that sends the events is not running at all time, so anybody who tries to connect to the event just keeps on trying to connect and getting nothing, and when the 3rd party does issue the post nobody is signed up for the events of the script that runs.

how do i keep a script running at all time? wouldn't this cause a leak in resources if i did do this?

is there a way to do this so that the you are signed up to any events that a "future" script will create?

Was it helpful?

Solution

Basically you need to look at this as two completely seperate problems:

  1. How do I post an event into a subscribers notification queue
  2. How do I do near-instant notification to subscribed clients

Let's start with 2.)

There are lots of approaches to this very common problem, many of which have dependencies. If you control the server (on a root level) you can chose from many, but if you want this to be hostable in any low-cost PHP environment, I recommend you go for long polling as a slightly aged nut well-understood and easy technology: Have you clients poll the server (via AJAX) and the server-sided poll script watch for events on the server. Return from the script only after something like 30-120 seconds to stay inside timeouts (in the meantime maybe send some placeholders). If an event happens, return immediately with the event.

This solves your continually running script problem: The clients keep it running.

Assuming you got this, 1. seems quite trivial: Post the event to a central queue (e.g. Database) and kick the pollers e.g. via SHM or file deletion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top