babelouest / ulfius

Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services
https://babelouest.github.io/ulfius
GNU Lesser General Public License v2.1
1.08k stars 182 forks source link

How to implement server push? #171

Closed Knutselsmurf1 closed 3 years ago

Knutselsmurf1 commented 3 years ago

I want to build a server which regularly sends events to a client with a server push.

The server controls several hardware and which should have a Rest interface. If the hardware fails, it should send an event to the client.

This is the pattern:

Server ------------------------ Client

<------ Connection request ------ ------- Connection response ---->

--------------- Event -------------> --------------- Event -------------> --------------- Event ------------->

Is this possible with Ulfius and can you give me an example.

babelouest commented 3 years ago

Hello @Knutselsmurf1 ,

Unfortunately, it's not a matter of Ulfius, but HTTP/1.1 doesn't support push messages by design. You have different workarounds for this though:

HTTP/2 has server push capabilities but Ulfius doesn't support HTTP/2 at the moment.

Hope that helps

Happy hacking!

Knutselsmurf1 commented 3 years ago

The websocket approach in my application is not really usable. The polling approach might be workable.

Is it expected that Ulfius gets HTTP/2 support soon? It would be nice, because it is convenient to use and its good examples.

However do you know a possible other comparable solution for C, Linux, with HTTP/2 support and preferably json support for rather small embedded devices in the mean time?

babelouest commented 3 years ago

Is it expected that Ulfius gets HTTP/2 support soon?

Expected: yes Soon: not really, depends on the definition of soon though

Ulfius is based on libmicrohttpd, which has a work-in-progress to add HTTP/2 support, but isn't complete yet. If you want to help with the HTTP/2 support in libmicrohttpd, I'm sure they would be thrilled.

However do you know a possible other comparable solution for C, Linux, with HTTP/2 support and preferably json support for rather small embedded devices in the mean time?

Not really, a quick search gave me some input like this page, but I haven't tried any of those implementations.

Knutselsmurf1 commented 3 years ago

As an alternative, is Server Side Events supported?

babelouest commented 3 years ago

Technically yes, but you have to implement it yourself.

Considering a Server Side Event implementation is reversing the communication flow between the http server and the client, you can switch the http service and client.

The outgoing http requests are described in the API Documentation: Outgoing request functions

babelouest commented 3 years ago

@Knutselsmurf1 , does this answer your questions?

Knutselsmurf1 commented 3 years ago

@Knutselsmurf1 , does this answer your questions?

For now I am not looking further into HTTP/2. I am not sure how I can help to build the HTTP/2 support in libmicrohttpd. If it becomes more mature I like to test it.

I start my project with the polling approach and which is for now the most robust and easy to implement. This means that the client should call the event endpoint on the server every x seconds, to retrieve its event data.

However for the near future I consider the Server Side Event implementation. It seems doable, however I am not sure what kind of issues could occur if I implement a reverse communication flow (for example if the connection between server and client is temporary broken, fully broken, is changed or something else).

Thank you for helping.

babelouest commented 3 years ago

No problem, I'm closing the issue then.

Good luck with your project!