E2OpenPlugins / e2openplugin-OpenWebif

GPL Web Interface for e2stabs
GNU General Public License v3.0
204 stars 226 forks source link

Websockets to replace polling (`/api/statusinfo` & `/api/signal`) #1321

Open wedebe opened 3 years ago

wedebe commented 3 years ago

OpenWebif (modern ui) currently sends frequent get requests to retrieve status and signal data.

I'd love to request a test build with a websockets server implementation installed for testing, if possible, please.

(I see there's a libqt5websockets in package manager but can't find how to hook it into controllers/web.py)

Further info The WebSocket API (WebSockets) - MDN

jbleyel commented 3 years ago

Hi @wedebe , i see no real benefit in performance or traffic if we use websockets for single requests. Websockets will be a good solution if you have triggered changes like push notifications. But these trigger needs to implemented on server side. This can cause performance issues.

Here are some examples. -> Server : https://autobahn.readthedocs.io/en/latest/websocket/programming.html

jbleyel commented 3 years ago

Let me try a test version.

wedebe commented 3 years ago

Hi @wedebe , i see no real benefit in performance or traffic if we use websockets for single requests. Websockets will be a good solution if you have triggered changes like push notifications. But these trigger needs to implemented on server side. This can cause performance issues.

Here are some examples. -> Server : https://autobahn.readthedocs.io/en/latest/websocket/programming.html

The benefit would by far apply to the client-side (web browser/mobile app), being able to notify clients of status change (power state/volume & channel change/signal strength etc.), rather than constant, periodic polling.

Another request from quite some time ago: https://forums.openpli.org/topic/46664-enigma2-websocket/

jbleyel commented 3 years ago

https://github.com/E2OpenPlugins/e2openplugin-OpenWebif/tree/websocket

NOT TESTET!!!

wedebe commented 3 years ago

https://github.com/E2OpenPlugins/e2openplugin-OpenWebif/tree/websocket

NOT TESTET!!!

tenor--

Thanks!

wedebe commented 3 years ago

Fixed typos #1323

Currently at

Plugin  Extensions/OpenWebif failed to load: No module named 'autobahn'
Traceback (most recent call last):
  File "/usr/lib/enigma2/python/Components/PluginComponent.py", line 56, in readPluginList
    plugin = my_import('.'.join(["Plugins", c, pluginname, "plugin"]))
  File "/usr/lib/enigma2/python/Tools/Import.py", line 2, in my_import
    mod = __import__(name)
  File "/usr/lib/enigma2/python/Plugins/Extensions/OpenWebif/plugin.py", line 36, in <module>
    from Plugins.Extensions.OpenWebif.httpserver import HttpdStart, HttpdStop, HttpdRestart
  File "/usr/lib/enigma2/python/Plugins/Extensions/OpenWebif/httpserver.py", line 35, in <module>
    from Plugins.Extensions.OpenWebif.controllers.root import RootController
  File "/usr/lib/enigma2/python/Plugins/Extensions/OpenWebif/controllers/root.py", line 44, in <module>
    from Plugins.Extensions.OpenWebif.controllers.ws import webSocketServer
  File "/usr/lib/enigma2/python/Plugins/Extensions/OpenWebif/controllers/ws.py", line 23, in <module>
    from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
ModuleNotFoundError: No module named 'autobahn'
jbleyel commented 3 years ago

We need autobahn on the feed and this works only on python3! So we should exclude this on python2. I will check this.

wedebe commented 3 years ago

We need autobahn on the feed and this works only on python3! So we should exclude this on python2. I will check this.

We can fallback to less frequent GET request polling when a websocket connect fails. (Users who don't update their e2 image shouldn't expect the very best experience anyway).

jbleyel commented 3 years ago

yes why, not the classic UI has also a higher timeout for the status info But the websockets implementation takes a while.

jbleyel commented 3 years ago

autobahn is on the feed soon

wedebe commented 3 years ago

autobahn is on the feed soon

Confirmed now available.

Now getting ModuleNotFoundError: No module named 'voluptuous'

jbleyel commented 3 years ago

This is a special module to create/validate the schema and can be done differently. https://alecthomas.github.io/voluptuous/docs/_build/html/voluptuous.html#


    REQUEST_BASE_SCHEMA = vol.Schema({
        vol.Required('id'): int,
    })

    BASE_MESSAGE_SCHEMA = REQUEST_BASE_SCHEMA.extend({
        vol.Required('type'): vol.Any(
                TYPE_AUTH,
                TYPE_PING
            )
    }, extra=vol.ALLOW_EXTRA)

    AUTH_MESSAGE_SCHEMA = REQUEST_BASE_SCHEMA.extend({
        vol.Required('type'): TYPE_AUTH,
    })
jbleyel commented 3 years ago

Here are some examples.

https://python.plainenglish.io/identify-websocket-clients-with-autobahn-twisted-and-python-3f90b4c135d4 https://www.programcreek.com/python/example/92376/autobahn.twisted.websocket.WebSocketServerProtocol https://www.fullstackpython.com/websockets.html

jbleyel commented 3 years ago

BUT.. as i said. The real benefit for websockets are push notifications. The push trigger source must be enigma.

Enigma2 triggers the a function in OWF if something new data is available and this function calls the websocket push to the client. This is such a trigger.

-> https://github.com/E2OpenPlugins/e2openplugin-OpenWebif/blob/d783fe31bf9d0fff8651b05b22b91c4ba05ff3d1/plugin/controllers/root.py#L88

jbleyel commented 3 years ago

https://stackoverflow.com/questions/28613399/websocket-vs-rest-api-for-real-time-data/28618369#28618369