http_client / http_listener handle multiple responses

Jan 6, 2016 at 10:59 AM
Edited Jan 6, 2016 at 11:01 AM
Hello,

first of all, thanks for this very good and modern library!

I have the following situation: I want to stream real-time financial data using the Lightstreamer text protocol. For that, I sent a POST request to a given server as
http_client client(U("https://demo-apd.marketdatasystems.com/"));
http_request create_session;
//set up create_session request
client.request(create_session).then([](http_response response){ /*... handle response ... */; }).wait();
Next I post another POST-request in order (to a different URI) in order to tell the server which data it should deliver. The server then starts to send real-time data. Following this request, the server https://demo-apd.marketdatasystems.com/ starts sending real time data.

In order to access this data, I thought an http_listener would be appropriate
        web::http::experimental::listener::http_listener listener(U("https://demo-apd.marketdatasystems.com/"));

        auto handle = [](web::http::http_request request) { auto str = request.extract_string().get(); std::wcout << str << std::endl;};
        listener.support(web::http::methods::GET, handle);
        listener.support(web::http::methods::POST, handle);
        listener.support(web::http::methods::PUT, handle);

        try
        {
            listener
                .open()
                .then([&listener]() {std::cout << "\nstarting to listen\n" << std::endl;})
                .wait();

            while (true);
        }
        catch (web::http::http_exception const& e)
        {
            std::wcout << e.what() << std::endl;
        }
However, in the try-block an exception is thrown which says that the
Address 'https://demo-apd.marketdatasystems.com/' is already in use
What does that mean and how can I get around it?

Again, the goal is to send a POST-request to a given address and the listen to its frequent responses.

(If the given information is insufficient, please let me know)
Coordinator
Jan 6, 2016 at 6:42 PM
Hi david,

I'm not familiar with lightstreamer at all, however briefly looking over the Wikipedia page suggests that you probably do not need to use http_listener at all. Our http_listener class is used to host an HTTP server; the http_client handles all connecting to other servers.

As for how you need to access the data it's "sending", that's up to the particulars of the lightstreamer protocol. I can't help you much there.
Jan 7, 2016 at 1:43 AM
Hi roschuma,

thanks for your answer. Maybe you're right and the process is not meant in that way -- at least I found a different solution given here, http://stackoverflow.com/a/29660785/2412846, in which the client repeatedly posts requests to the server. So my initial problem is solved.

Still, if you don't mind, you can help my understanding of web communication. What I thought is happening here is the following:

First, I tell some server that it should send the client -- me -- regular updates as POST requests. In order to notify the server, I send a request to it.

Second, after the server started to push the regular updates started, I listen to them via http_listener and handle the requests via callbacks.

Is this picture wrong somehow?

Thanks in advance,
David
Coordinator
Jan 8, 2016 at 7:45 PM
You are right that this is one possible way such a communication could be performed. However, due to the nature of firewalls and NAT devices, this is usually not feasible in a typical consumer setting; the server has no way to connect to your local TCP server (some firewall somewhere will block the connection).

Thus today, it is more typical to either use a polling approach like the above or use websockets: https://en.wikipedia.org/wiki/WebSocket. We do have support for websockets in the C++ REST SDK if they are appropriate for your application.