fhessel / esp32_https_server

Alternative ESP32 Webserver implementation for the ESP32 Arduino Core, supporting HTTPS and HTTP.
MIT License
344 stars 125 forks source link

How to send push events from esp32 web server #35

Open ShivamJoker opened 5 years ago

ShivamJoker commented 5 years ago

Hey, now a days push notifications are the best thing for browsers to send alerts can you please add that feature in this library which fire push events from the server

fhessel commented 5 years ago

Hi, I haven't been using push notifications by myself yet, but to my understanding, they are realized on client side by implementing a service worker in Javascript (see also this introduction by Google) - as far as you're talking about browser-based notifications. So if I'm not mistaken, you should already be able to build an application that supports this by implementing the required handler functions with the server. Or am I missing something there?

ShivamJoker commented 5 years ago

I have already built one with node js but I didn't knew how they managed to send push event from the sever cause the Google told to use webpush library and in the service worker we listen for push event from server but I don't know how will we convert webpush.send(data) To esp codes if you want to look there is also a C library in web push GitHub account

fhessel commented 5 years ago

Okay, having a look at the code there I get your point, the server side seems not to be as trivial as "just write a short handler function" - however, I didn't see any major blocker either. The ESP32 is capable of the encryption stuff that is used there, but it'll require a rewrite to fit to either the OpenSSL or the mbedtls implementation of the esp-idf.

Having looked into it, I'm not really sure if the library itself is the correct place to put this code, as it's something more or less on top of HTTP, but not part of the protocol itself. So in my opinion, the right way to implement this would be a new Arduino Library that supports the push notifications and depends on the HTTPS server for communication purposes. So if you need push notifications, you just include both libs into your project and have a handy interface for sending notifications, and if not, you stay with just the server.

However, I'm currently quite short on time, so I cannot make any promises whether I could implement that at the moment, especially as I'm not familiar with the specification. And there are some features of the server that I need for personal projects and that I would have to implement first. I'd leave the issue open for now and tag it as a feature, but if you want it fast, I might need some support here.

ShivamJoker commented 5 years ago

Hey really thanks for response but it has nothing to do with https the service worker file just needs to be installed and I will help whatever I can. And I looked the whole Google didn't found any library which does this thing it just needs to be incripted and send a request service worker will keep listening for that event to be fired by server. And this can be really great for making an alarm system otherwise people are using 3rd party services like mqtt or pusher which make things more complex

And for the every client a subscription is generated and it should be saved somewhere into the server and at the time of firing the push event we will pass the token

If you will implement this feature not only I but many people will get help of this cause I am gonna make video tutorial for this auto temperature controlled water heater

fhessel commented 5 years ago

As far as I could read it by now, this feature would require implementing three different tasks (treat everything with caution, as mentioned, I haven't looked into push messages before):

1) Once: Generating Application Keys The web server on the ESP32 needs to be able to proof its identity when sending push notifications. So you generate a asymmetric key pair (that's why I mentioned mbedtls) and store it permanently in the NVM. Generating the key pair could also be done before flashing with known tools like generate-vapid-keys of the web-push command line, to reduce implementation effort on the ESP32. However, the ESP32 must be able to use these keys.

2) Per Client: Generating a Subscription The application's public key then is part of the Javascript client application (the receiver of the push messages). It can be used to create a subscription object on the client, that is bound to a web worker, that will receive the push messages once it wakes up. This subscription contains:

3) Per Push Message: Call Push Webservice Endpoint For each push message that you want to send to a client, the web server now has to encrypt the content of the message with the keys provided by the specific client (step 2), sign it with the application's private key (step 1) and then send it via HTTP(S) to the endpoint specified by the client (step 2). That's where it gets tricky, because you have to do all the crypto stuff on the ESP32 - probably doable, but a variety of algorithms takes part in it and you'll most likely end up with using the basic functionality of mbedtls again. After that, you have to play the role of an HTTP(S) client to call the browser specific endpoint that you received in step 2. The client-side part of HTTP isn't part of the library by now, and while you may reuse classes like HTTPHeader there's still much left to do. As you have no control over which push provider the client chooses, you need to be prepared to communicate with various servers. So the trial-and-error approach and "Let's just write a working HTTP request in progmem" will not work here either. You'll also have to deal with the problems that the ESP32 has when acting as a TLS client:

So the more I dive into that topic, the more time-consuming and less trivial the implementation of this seems to get. The problem is less of generating the subscription and storing it on the server, but more in the communication with the push message provider. That's hidden by the NodeJS libraries, but if you want to build this from scratch, you'd have to take care of that.

ShivamJoker commented 5 years ago

Thanks for the deep explanation but I don't know that much of programming so that I can dig up a library and rewrite it in C++ ☹️ hopefully one day I will. But I can write the front end part or if it's too complicated then I will serve page through my node server and fetch details from esp32 and update the service worker from server and database

fhessel commented 5 years ago

As mentioned, I understand why that's a useful feature, but I'll not be able to provide the server part at the moment.

I may be able to eventually get back to it, but most certainly not in the next few weeks, so it'll probably be too late for your project.