meshtastic / js

JS/TS library for interfacing with Meshtastic devices
https://meshtastic.org
GNU General Public License v3.0
74 stars 36 forks source link

Consider WebSockets #12

Closed crossan007 closed 8 months ago

crossan007 commented 3 years ago

Not sure if the dev-https branch of meshtastic-device has support for it yet, but I wonder if WebSockets would be a better fit than HTTP polling.

It might get around the // Important: the connect action must be called from a user interaction (e.g. button press), otherwise the browsers won't allow the connect situation as well.

crossan007 commented 3 years ago

reference https://randomnerdtutorials.com/esp32-websocket-server-arduino/

crossan007 commented 3 years ago

A WebSockets approach might also mitigate the HTTPS handshake performance penalty: https://github.com/meshtastic/Meshtastic-device/wiki/How-to-use-the-Meshtastic-Web-Interface-over-WiFi#performance

mc-hamster commented 3 years ago

I was really considering building support for web sockets when doing the first (and then second) implementation of web support but had a few considerations where this may not have been the right approach.

1) The esp32 supports a maximum of 4 IP connections. If we started to use up those connections, we'll also need to build some logic to prevent over use of the available connection pool and error handling to communicate the limited resource and prevent locking out additional connections. 2) There are a few places where threading on the device is required and I did actually have to use FreeRTOS to generate the SSL Certificate, but use of threads is quickly being phased out of the main device code. It's a perfect case to use it for the web server and if we decide to use it here, we can. 3) We are very memory limited as is. Supporting 4 connections would mean 5 threads (4 IP + 1 management) running at once. It may be possible to create new threads "just-in-time", but I have not yet seen anyone do this with any embedded web server. It'll be easy to implement but may be difficult to implement right.

With all that, I thought it was a good approach to just let the client stand in line and wait for the next available connection. It pushes out the problem to a properly behaving web browser and we can start to take advantage of time based multiplexing.

All that said, web sockets is actually a really good idea. As support gets built for Meshtastic on other device platforms with more system resources, it may be good to have a new web server for those platforms that exposes our the API with web sockets.

The comment you highlighted "Important: the connect action must be called from a user interaction..." is actually because of the device's power saving feature and my guess is that comment is carried over from the BLE version of the .js library (I didn't write that). Because of a bug in the wifi library of the Espressif SDK, the meshtastic device really should be kept plugged into USB power and in that case, we will not go into power saving so that comment doesn't apply.

For your edification, we use the esp_32_https_server library and websocket support is natively supported with easy to view sample code.

https://github.com/fhessel/esp32_https_server

Really do appreciate your thoughts and at some point, I do want to revisit this.

I'll leave this request open in the event someone would like to jump in and implement it. I'd love to see this happen!

thepoweroftwo commented 3 years ago

One little addition: The // Important: the connect action must be called from a user interaction (e.g. button press), otherwise the browsers won't allow the connect Message is only true when using Bluetooth, where it is a security measure, so that a website can't connect to nearby devices without the user knowing. We didn't update the docs there, will do in next commit.

crossan007 commented 3 years ago

@mc-hamster Wow, thank you for the detailed thoughts. This is really my first venture into ESP32 / embedded programming (though I've done a fair amount of higher level programming)

mc-hamster commented 3 years ago

@crossan007 Anytime mate. If you wanna try your hand at a change, go right ahead and I’ll gladly help you out.

Noki commented 3 years ago

@mc-hamster

The esp32 supports a maximum of 4 IP connections.

I tried to research the source of this statement. Can you point it out? I think it might be outdated information that might have been true at some point for the ESP8266.

mc-hamster commented 3 years ago

@mc-hamster

The esp32 supports a maximum of 4 IP connections.

I tried to research the source of this statement. Can you point it out? I think it might be outdated information that might have been true at some point for the ESP8266.

Looks like You’re correct. Thus information is not accurate anymore.

The limit is 10 ip connections by default. This is set in the build of the sdk. See CONFIG_LWIP_MAX_SOCKETS in:

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/kconfig.html

There’s also a default limit of 4 AP connections that can be raised to 8 or 10 in newer versions of the sdk, based on the config of the softap object.