Project

General

Profile

What is the way to invoke haveMoreData method of only selected response continuations?

Added by Plug Gulp over 3 years ago

I am trying to understand how notifications can be sent to client using a static resource, i.e. a REST API. I am using response continuation and long-polling to send notifications to clients of the resource. Not all clients will be sent a notification. Only specific clients matching certain criterial, e.g. some cookie value, will be dynamically chosen to receive notification.

I tried calling haveMoreData method of the resource when data is available and some clients needs to be notified. But that triggers request handler for all those clients that have response continuations for that resource. The WResource::haveMoreData method calls haveMoreData method of all continuations of that resource. I want to avoid that. I want to call haveMoreData method of continuations of responses for specific clients matching certain criterial. That way I will be able to trigger request handler for only those clients that match the criteria.

What is the way to invoke haveMoreData of only certain response continuations of the resource?


Replies (2)

RE: What is the way to invoke haveMoreData method of only selected response continuations? - Added by Bruce Toll over 3 years ago

Hi,

I have not worked with it, but there is a haveMoreData member function on the ResponseContinuation that sounds like it might meet your needs. See: https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Http_1_1ResponseContinuation.html#a08c2bc59b253f255c4cea87326373479

RE: What is the way to invoke haveMoreData method of only selected response continuations? - Added by Plug Gulp over 3 years ago

@Bruce Toll

... there is a haveMoreData member function on the ResponseContinuation...

Yes, I am aware of that method. If I use that then I will have to maintain a cache of and track all the continuations created for that resource. This is duplication of effort as WResource already stores continuation objects and invokes their haveMoreData method when WResource::haveMoreData method is called.

If I also maintain all the continuations of WResource then it will take more memory. I will also have to go through all the continuations and find the ones that are to be notified. That is not a scalable design. Also, how to access the continuations which are maintained by WResource? I don't see a way to access them except in the handleRequest method using the request object.

One solution that I can think of is passing a lambda or other parameter to WResource::haveMoreData that will filter out all those continuations that are not required. Something like:

some_resource->haveMoreData([] (std::any) -> bool {
        /* return true if the continuation needs to be invoked else false */
});

And in the WResource::haveMoreData method the lambda will be used as follows:

void WResource::haveMoreData(std::function<bool(std::any)> filter = [](std::any) -> bool {return true;} )
{
       // ....

       for(auto cont : continuations_) {
             if(filter(cont)) {
                  cont->haveMoreData();
             }
       }

       // ....
}
    (1-2/2)