From 1762204b0039386a561a489c37138e6197ffdd32 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 23 Nov 2023 23:49:55 +0100 Subject: [PATCH] dashboard: set nodelay on the websocket to avoid a delay seeing log messages (#5802) --- esphome/dashboard/web_server.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/esphome/dashboard/web_server.py b/esphome/dashboard/web_server.py index 7c5f653b5b..9a9ccb462b 100644 --- a/esphome/dashboard/web_server.py +++ b/esphome/dashboard/web_server.py @@ -27,6 +27,7 @@ import tornado.process import tornado.queues import tornado.web import tornado.websocket +import tornado.httputil import yaml from tornado.log import access_log @@ -136,7 +137,15 @@ def websocket_method(name): @websocket_class class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler): - def __init__(self, application, request, **kwargs): + """Base class for ESPHome websocket commands.""" + + def __init__( + self, + application: tornado.web.Application, + request: tornado.httputil.HTTPServerRequest, + **kwargs: Any, + ) -> None: + """Initialize the websocket.""" super().__init__(application, request, **kwargs) self._proc = None self._queue = None @@ -145,6 +154,12 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler): # use Popen() with a reading thread instead self._use_popen = os.name == "nt" + def open(self, *args: str, **kwargs: str) -> None: + """Handle new WebSocket connection.""" + # Ensure messages from the subprocess are sent immediately + # to avoid a 200-500ms delay when nodelay is not set. + self.set_nodelay(True) + @authenticated async def on_message( # pylint: disable=invalid-overridden-method self, message: str