3

In my app I'm using server-sent events and have the following situation (pseudo code):

$response = new StreamedResponse();
$response->setCallback(function () {
    while(true) {
        // 1. $data = fetchData();
        // 2. echo "data: $data";
        // 3. sleep(x);
    }
});
$response->send();

My SSE Response class accepts a callback to gather the data (step 1), which actually performs a database query. Now to my problem: As I am trying to avoid polling the database each X seconds, I want to make use of Doctrine's onFlush event to set a flag that the corresponding entity has been actually changed, which would then be checked within fetchData callback. Normally, I would do this by setting a flag on current user session, but as the streaming loop constantly writes data, the session cannot be accessed within the callback. So has anybody an idea how to resolve this problem?

BTW: I'm using Symfony 3.3 and Doctrine 2.5 - thanks for any help!

arm1n
  • 151
  • 1
  • 7

1 Answers1

1

I know that this question is from a long time ago, but here's a suggestion: Use shared memory (the php shm_*() functions). That way your flag isn't tied to a specific session.

Be sure to lock and unlock around access to the shared memory (I usually use a semaphore).

David Patterson
  • 1,780
  • 3
  • 17
  • 40
  • 1
    That's a very interesting approach, haven't thought of that. In case I'll work again with SSE I'll give that a try - thanks for you contribution, very much appreciate it! – arm1n May 15 '19 at 20:10