We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[Support]: mqtt sometimes fails
### Describe the problem you are having

Sometimes on boot mqtt fails with the following log.
After several reboots it will run just fine

### Version

0.13.1-34fb1c2

### Frigate config file

```yaml
mqtt:
  host: 192.168.86.13
  user: *
  password: *
```


### Relevant log output

```shell
2024-02-03 11:29:28.183720955  [INFO] Preparing Frigate...
2024-02-03 11:29:30.429983288  [INFO] Starting Frigate...
2024-02-03 11:29:43.565715210  [2024-02-03 11:29:43] frigate.app                    INFO    : Starting Frigate (0.13.1-34fb1c2)
2024-02-03 11:29:43.668674989  [2024-02-03 11:29:43] frigate.config                 WARNING : Customizing more than a detector model path is unsupported.
2024-02-03 11:29:43.693848249  [2024-02-03 11:29:43] peewee_migrate.logs            INFO    : Starting migrations
2024-02-03 11:29:43.786091779  [2024-02-03 11:29:43] peewee_migrate.logs            INFO    : There is nothing to migrate
2024-02-03 11:29:44.077802695  [2024-02-03 11:29:44] frigate.app                    INFO    : Recording process started: 465
2024-02-03 11:29:44.082183481  [2024-02-03 11:29:44] frigate.app                    INFO    : go2rtc process pid: 89
2024-02-03 11:29:44.092318107  Exception in thread mqtt:
2024-02-03 11:29:44.092347726  Traceback (most recent call last):
2024-02-03 11:29:44.092350843    File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
2024-02-03 11:29:44.132000773      self.run()
2024-02-03 11:29:44.132008391    File "/usr/lib/python3.9/threading.py", line 892, in run
2024-02-03 11:29:44.132351154      self._target(*self._args, **self._kwargs)
2024-02-03 11:29:44.132356410    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 3591, in _thread_main
2024-02-03 11:29:44.166476612      self.loop_forever(retry_first_connection=True)
2024-02-03 11:29:44.166483467    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 1756, in loop_forever
2024-02-03 11:29:44.167015600      rc = self._loop(timeout)
2024-02-03 11:29:44.167020085    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 1164, in _loop
2024-02-03 11:29:44.167335718      rc = self.loop_read()
2024-02-03 11:29:44.167339384    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 1556, in loop_read
2024-02-03 11:29:44.167728400      rc = self._packet_read()
2024-02-03 11:29:44.167732996    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 2439, in _packet_read
2024-02-03 11:29:44.168284999      rc = self._packet_handle()
2024-02-03 11:29:44.168305384    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 3033, in _packet_handle
2024-02-03 11:29:44.169013241      return self._handle_publish()
2024-02-03 11:29:44.169016831    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 3327, in _handle_publish
2024-02-03 11:29:44.169718804      self._handle_on_message(message)
2024-02-03 11:29:44.169722152    File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 3556, in _handle_on_message
2024-02-03 11:29:44.170469665      callback(self, self._userdata, message)
2024-02-03 11:29:44.170472610    File "/opt/frigate/frigate/comms/mqtt.py", line 110, in on_mqtt_command
2024-02-03 11:29:44.177426729      self._dispatcher(
2024-02-03 11:29:44.177430972    File "/opt/frigate/frigate/comms/dispatcher.py", line 80, in _receive
2024-02-03 11:29:44.179490027      self._camera_settings_handlers[command](camera_name, payload)
2024-02-03 11:29:44.179512034    File "/opt/frigate/frigate/comms/dispatcher.py", line 281, in _on_recordings_command
2024-02-03 11:29:44.179681083      self.publish(f"{camera_name}/recordings/state", payload, retain=True)
2024-02-03 11:29:44.179685405    File "/opt/frigate/frigate/comms/dispatcher.py", line 111, in publish
2024-02-03 11:29:44.179791262      comm.publish(topic, payload, retain)
2024-02-03 11:29:44.179795488    File "/opt/frigate/frigate/comms/ws.py", line 102, in publish
2024-02-03 11:29:44.180243298      self.websocket_server.manager.broadcast(ws_message)
2024-02-03 11:29:44.180247725  AttributeError: 'WebSocketClient' object has no attribute 'websocket_server'
2024-02-03 11:29:44.244791022  [2024-02-03 11:29:44] frigate.app                    INFO    : Output process started: 476
```


### FFprobe output from your camera

```shell
[{"return_code":0,"stderr":"","stdout":{"programs":[],"streams":[{"avg_frame_rate":"30/1","codec_long_name":"H.264/AVC/MPEG-4AVC/MPEG-4part10","height":1440,"width":2560},{"avg_frame_rate":"0/0","codec_long_name":"AAC(AdvancedAudioCoding)"}]}},{"return_code":0,"stderr":"","stdout":{"programs":[],"streams":[{"avg_frame_rate":"20/1","codec_long_name":"H.264/AVC/MPEG-4AVC/MPEG-4part10","height":672,"width":896},{"avg_frame_rate":"0/0","codec_long_name":"AAC(AdvancedAudioCoding)"}]}}]
```


### Frigate stats

_No response_

### Operating system

Debian

### Install method

Docker Compose

### Coral version

CPU (no coral)

### Network connection

Wired

### Camera make and model

Reolink

### Any other information that may be helpful

_No response_

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `frigate/comms/ws.py`
Content:
```
1 """Websocket communicator."""
2 
3 import errno
4 import json
5 import logging
6 import threading
7 from typing import Callable
8 from wsgiref.simple_server import make_server
9 
10 from ws4py.server.wsgirefserver import (
11     WebSocketWSGIHandler,
12     WebSocketWSGIRequestHandler,
13     WSGIServer,
14 )
15 from ws4py.server.wsgiutils import WebSocketWSGIApplication
16 from ws4py.websocket import WebSocket as WebSocket_
17 
18 from frigate.comms.dispatcher import Communicator
19 from frigate.config import FrigateConfig
20 
21 logger = logging.getLogger(__name__)
22 
23 
24 class WebSocket(WebSocket_):
25     def unhandled_error(self, error):
26         """
27         Handles the unfriendly socket closures on the server side
28         without showing a confusing error message
29         """
30         if hasattr(error, "errno") and error.errno == errno.ECONNRESET:
31             pass
32         else:
33             logging.getLogger("ws4py").exception("Failed to receive data")
34 
35 
36 class WebSocketClient(Communicator):  # type: ignore[misc]
37     """Frigate wrapper for ws client."""
38 
39     def __init__(self, config: FrigateConfig) -> None:
40         self.config = config
41 
42     def subscribe(self, receiver: Callable) -> None:
43         self._dispatcher = receiver
44         self.start()
45 
46     def start(self) -> None:
47         """Start the websocket client."""
48 
49         class _WebSocketHandler(WebSocket):  # type: ignore[misc]
50             receiver = self._dispatcher
51 
52             def received_message(self, message: WebSocket.received_message) -> None:
53                 try:
54                     json_message = json.loads(message.data.decode("utf-8"))
55                     json_message = {
56                         "topic": json_message.get("topic"),
57                         "payload": json_message.get("payload"),
58                     }
59                 except Exception:
60                     logger.warning(
61                         f"Unable to parse websocket message as valid json: {message.data.decode('utf-8')}"
62                     )
63                     return
64 
65                 logger.debug(
66                     f"Publishing mqtt message from websockets at {json_message['topic']}."
67                 )
68                 self.receiver(
69                     json_message["topic"],
70                     json_message["payload"],
71                 )
72 
73         # start a websocket server on 5002
74         WebSocketWSGIHandler.http_version = "1.1"
75         self.websocket_server = make_server(
76             "127.0.0.1",
77             5002,
78             server_class=WSGIServer,
79             handler_class=WebSocketWSGIRequestHandler,
80             app=WebSocketWSGIApplication(handler_cls=_WebSocketHandler),
81         )
82         self.websocket_server.initialize_websockets_manager()
83         self.websocket_thread = threading.Thread(
84             target=self.websocket_server.serve_forever
85         )
86         self.websocket_thread.start()
87 
88     def publish(self, topic: str, payload: str, _: bool) -> None:
89         try:
90             ws_message = json.dumps(
91                 {
92                     "topic": topic,
93                     "payload": payload,
94                 }
95             )
96         except Exception:
97             # if the payload can't be decoded don't relay to clients
98             logger.debug(f"payload for {topic} wasn't text. Skipping...")
99             return
100 
101         try:
102             self.websocket_server.manager.broadcast(ws_message)
103         except ConnectionResetError:
104             pass
105 
106     def stop(self) -> None:
107         self.websocket_server.manager.close_all()
108         self.websocket_server.manager.stop()
109         self.websocket_server.manager.join()
110         self.websocket_server.shutdown()
111         self.websocket_thread.join()
112         logger.info("Exiting websocket client...")
113
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/frigate/comms/ws.py b/frigate/comms/ws.py
--- a/frigate/comms/ws.py
+++ b/frigate/comms/ws.py
@@ -38,6 +38,7 @@
 
     def __init__(self, config: FrigateConfig) -> None:
         self.config = config
+        self.websocket_server = None
 
     def subscribe(self, receiver: Callable) -> None:
         self._dispatcher = receiver
@@ -98,6 +99,10 @@
             logger.debug(f"payload for {topic} wasn't text. Skipping...")
             return
 
+        if self.websocket_server is None:
+            logger.debug("Skipping message, websocket not connected yet")
+            return
+
         try:
             self.websocket_server.manager.broadcast(ws_message)
         except ConnectionResetError:

{"golden_diff": "diff --git a/frigate/comms/ws.py b/frigate/comms/ws.py\n--- a/frigate/comms/ws.py\n+++ b/frigate/comms/ws.py\n@@ -38,6 +38,7 @@\n \n     def __init__(self, config: FrigateConfig) -> None:\n         self.config = config\n+        self.websocket_server = None\n \n     def subscribe(self, receiver: Callable) -> None:\n         self._dispatcher = receiver\n@@ -98,6 +99,10 @@\n             logger.debug(f\"payload for {topic} wasn't text. Skipping...\")\n             return\n \n+        if self.websocket_server is None:\n+            logger.debug(\"Skipping message, websocket not connected yet\")\n+            return\n+\n         try:\n             self.websocket_server.manager.broadcast(ws_message)\n         except ConnectionResetError:\n", "issue": "[Support]: mqtt sometimes fails\n### Describe the problem you are having\n\nSometimes on boot mqtt fails with the following log.\r\nAfter several reboots it will run just fine\n\n### Version\n\n0.13.1-34fb1c2\n\n### Frigate config file\n\n```yaml\nmqtt:\r\n  host: 192.168.86.13\r\n  user: *\r\n  password: *\n```\n\n\n### Relevant log output\n\n```shell\n2024-02-03 11:29:28.183720955  [INFO] Preparing Frigate...\r\n2024-02-03 11:29:30.429983288  [INFO] Starting Frigate...\r\n2024-02-03 11:29:43.565715210  [2024-02-03 11:29:43] frigate.app                    INFO    : Starting Frigate (0.13.1-34fb1c2)\r\n2024-02-03 11:29:43.668674989  [2024-02-03 11:29:43] frigate.config                 WARNING : Customizing more than a detector model path is unsupported.\r\n2024-02-03 11:29:43.693848249  [2024-02-03 11:29:43] peewee_migrate.logs            INFO    : Starting migrations\r\n2024-02-03 11:29:43.786091779  [2024-02-03 11:29:43] peewee_migrate.logs            INFO    : There is nothing to migrate\r\n2024-02-03 11:29:44.077802695  [2024-02-03 11:29:44] frigate.app                    INFO    : Recording process started: 465\r\n2024-02-03 11:29:44.082183481  [2024-02-03 11:29:44] frigate.app                    INFO    : go2rtc process pid: 89\r\n2024-02-03 11:29:44.092318107  Exception in thread mqtt:\r\n2024-02-03 11:29:44.092347726  Traceback (most recent call last):\r\n2024-02-03 11:29:44.092350843    File \"/usr/lib/python3.9/threading.py\", line 954, in _bootstrap_inner\r\n2024-02-03 11:29:44.132000773      self.run()\r\n2024-02-03 11:29:44.132008391    File \"/usr/lib/python3.9/threading.py\", line 892, in run\r\n2024-02-03 11:29:44.132351154      self._target(*self._args, **self._kwargs)\r\n2024-02-03 11:29:44.132356410    File \"/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py\", line 3591, in _thread_main\r\n2024-02-03 11:29:44.166476612      self.loop_forever(retry_first_connection=True)\r\n2024-02-03 11:29:44.166483467    File \"/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py\", line 1756, in loop_forever\r\n2024-02-03 11:29:44.167015600      rc = self._loop(timeout)\r\n2024-02-03 11:29:44.167020085    File \"/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py\", line 1164, in _loop\r\n2024-02-03 11:29:44.167335718      rc = self.loop_read()\r\n2024-02-03 11:29:44.167339384    File \"/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py\", line 1556, in loop_read\r\n2024-02-03 11:29:44.167728400      rc = self._packet_read()\r\n2024-02-03 11:29:44.167732996    File \"/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py\", line 2439, in _packet_read\r\n2024-02-03 11:29:44.168284999      rc = self._packet_handle()\r\n2024-02-03 11:29:44.168305384    File \"/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py\", line 3033, in _packet_handle\r\n2024-02-03 11:29:44.169013241      return self._handle_publish()\r\n2024-02-03 11:29:44.169016831    File \"/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py\", line 3327, in _handle_publish\r\n2024-02-03 11:29:44.169718804      self._handle_on_message(message)\r\n2024-02-03 11:29:44.169722152    File \"/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py\", line 3556, in _handle_on_message\r\n2024-02-03 11:29:44.170469665      callback(self, self._userdata, message)\r\n2024-02-03 11:29:44.170472610    File \"/opt/frigate/frigate/comms/mqtt.py\", line 110, in on_mqtt_command\r\n2024-02-03 11:29:44.177426729      self._dispatcher(\r\n2024-02-03 11:29:44.177430972    File \"/opt/frigate/frigate/comms/dispatcher.py\", line 80, in _receive\r\n2024-02-03 11:29:44.179490027      self._camera_settings_handlers[command](camera_name, payload)\r\n2024-02-03 11:29:44.179512034    File \"/opt/frigate/frigate/comms/dispatcher.py\", line 281, in _on_recordings_command\r\n2024-02-03 11:29:44.179681083      self.publish(f\"{camera_name}/recordings/state\", payload, retain=True)\r\n2024-02-03 11:29:44.179685405    File \"/opt/frigate/frigate/comms/dispatcher.py\", line 111, in publish\r\n2024-02-03 11:29:44.179791262      comm.publish(topic, payload, retain)\r\n2024-02-03 11:29:44.179795488    File \"/opt/frigate/frigate/comms/ws.py\", line 102, in publish\r\n2024-02-03 11:29:44.180243298      self.websocket_server.manager.broadcast(ws_message)\r\n2024-02-03 11:29:44.180247725  AttributeError: 'WebSocketClient' object has no attribute 'websocket_server'\r\n2024-02-03 11:29:44.244791022  [2024-02-03 11:29:44] frigate.app                    INFO    : Output process started: 476\n```\n\n\n### FFprobe output from your camera\n\n```shell\n[{\"return_code\":0,\"stderr\":\"\",\"stdout\":{\"programs\":[],\"streams\":[{\"avg_frame_rate\":\"30/1\",\"codec_long_name\":\"H.264/AVC/MPEG-4AVC/MPEG-4part10\",\"height\":1440,\"width\":2560},{\"avg_frame_rate\":\"0/0\",\"codec_long_name\":\"AAC(AdvancedAudioCoding)\"}]}},{\"return_code\":0,\"stderr\":\"\",\"stdout\":{\"programs\":[],\"streams\":[{\"avg_frame_rate\":\"20/1\",\"codec_long_name\":\"H.264/AVC/MPEG-4AVC/MPEG-4part10\",\"height\":672,\"width\":896},{\"avg_frame_rate\":\"0/0\",\"codec_long_name\":\"AAC(AdvancedAudioCoding)\"}]}}]\n```\n\n\n### Frigate stats\n\n_No response_\n\n### Operating system\n\nDebian\n\n### Install method\n\nDocker Compose\n\n### Coral version\n\nCPU (no coral)\n\n### Network connection\n\nWired\n\n### Camera make and model\n\nReolink\n\n### Any other information that may be helpful\n\n_No response_\n", "before_files": [{"content": "\"\"\"Websocket communicator.\"\"\"\n\nimport errno\nimport json\nimport logging\nimport threading\nfrom typing import Callable\nfrom wsgiref.simple_server import make_server\n\nfrom ws4py.server.wsgirefserver import (\n    WebSocketWSGIHandler,\n    WebSocketWSGIRequestHandler,\n    WSGIServer,\n)\nfrom ws4py.server.wsgiutils import WebSocketWSGIApplication\nfrom ws4py.websocket import WebSocket as WebSocket_\n\nfrom frigate.comms.dispatcher import Communicator\nfrom frigate.config import FrigateConfig\n\nlogger = logging.getLogger(__name__)\n\n\nclass WebSocket(WebSocket_):\n    def unhandled_error(self, error):\n        \"\"\"\n        Handles the unfriendly socket closures on the server side\n        without showing a confusing error message\n        \"\"\"\n        if hasattr(error, \"errno\") and error.errno == errno.ECONNRESET:\n            pass\n        else:\n            logging.getLogger(\"ws4py\").exception(\"Failed to receive data\")\n\n\nclass WebSocketClient(Communicator):  # type: ignore[misc]\n    \"\"\"Frigate wrapper for ws client.\"\"\"\n\n    def __init__(self, config: FrigateConfig) -> None:\n        self.config = config\n\n    def subscribe(self, receiver: Callable) -> None:\n        self._dispatcher = receiver\n        self.start()\n\n    def start(self) -> None:\n        \"\"\"Start the websocket client.\"\"\"\n\n        class _WebSocketHandler(WebSocket):  # type: ignore[misc]\n            receiver = self._dispatcher\n\n            def received_message(self, message: WebSocket.received_message) -> None:\n                try:\n                    json_message = json.loads(message.data.decode(\"utf-8\"))\n                    json_message = {\n                        \"topic\": json_message.get(\"topic\"),\n                        \"payload\": json_message.get(\"payload\"),\n                    }\n                except Exception:\n                    logger.warning(\n                        f\"Unable to parse websocket message as valid json: {message.data.decode('utf-8')}\"\n                    )\n                    return\n\n                logger.debug(\n                    f\"Publishing mqtt message from websockets at {json_message['topic']}.\"\n                )\n                self.receiver(\n                    json_message[\"topic\"],\n                    json_message[\"payload\"],\n                )\n\n        # start a websocket server on 5002\n        WebSocketWSGIHandler.http_version = \"1.1\"\n        self.websocket_server = make_server(\n            \"127.0.0.1\",\n            5002,\n            server_class=WSGIServer,\n            handler_class=WebSocketWSGIRequestHandler,\n            app=WebSocketWSGIApplication(handler_cls=_WebSocketHandler),\n        )\n        self.websocket_server.initialize_websockets_manager()\n        self.websocket_thread = threading.Thread(\n            target=self.websocket_server.serve_forever\n        )\n        self.websocket_thread.start()\n\n    def publish(self, topic: str, payload: str, _: bool) -> None:\n        try:\n            ws_message = json.dumps(\n                {\n                    \"topic\": topic,\n                    \"payload\": payload,\n                }\n            )\n        except Exception:\n            # if the payload can't be decoded don't relay to clients\n            logger.debug(f\"payload for {topic} wasn't text. Skipping...\")\n            return\n\n        try:\n            self.websocket_server.manager.broadcast(ws_message)\n        except ConnectionResetError:\n            pass\n\n    def stop(self) -> None:\n        self.websocket_server.manager.close_all()\n        self.websocket_server.manager.stop()\n        self.websocket_server.manager.join()\n        self.websocket_server.shutdown()\n        self.websocket_thread.join()\n        logger.info(\"Exiting websocket client...\")\n", "path": "frigate/comms/ws.py"}], "after_files": [{"content": "\"\"\"Websocket communicator.\"\"\"\n\nimport errno\nimport json\nimport logging\nimport threading\nfrom typing import Callable\nfrom wsgiref.simple_server import make_server\n\nfrom ws4py.server.wsgirefserver import (\n    WebSocketWSGIHandler,\n    WebSocketWSGIRequestHandler,\n    WSGIServer,\n)\nfrom ws4py.server.wsgiutils import WebSocketWSGIApplication\nfrom ws4py.websocket import WebSocket as WebSocket_\n\nfrom frigate.comms.dispatcher import Communicator\nfrom frigate.config import FrigateConfig\n\nlogger = logging.getLogger(__name__)\n\n\nclass WebSocket(WebSocket_):\n    def unhandled_error(self, error):\n        \"\"\"\n        Handles the unfriendly socket closures on the server side\n        without showing a confusing error message\n        \"\"\"\n        if hasattr(error, \"errno\") and error.errno == errno.ECONNRESET:\n            pass\n        else:\n            logging.getLogger(\"ws4py\").exception(\"Failed to receive data\")\n\n\nclass WebSocketClient(Communicator):  # type: ignore[misc]\n    \"\"\"Frigate wrapper for ws client.\"\"\"\n\n    def __init__(self, config: FrigateConfig) -> None:\n        self.config = config\n        self.websocket_server = None\n\n    def subscribe(self, receiver: Callable) -> None:\n        self._dispatcher = receiver\n        self.start()\n\n    def start(self) -> None:\n        \"\"\"Start the websocket client.\"\"\"\n\n        class _WebSocketHandler(WebSocket):  # type: ignore[misc]\n            receiver = self._dispatcher\n\n            def received_message(self, message: WebSocket.received_message) -> None:\n                try:\n                    json_message = json.loads(message.data.decode(\"utf-8\"))\n                    json_message = {\n                        \"topic\": json_message.get(\"topic\"),\n                        \"payload\": json_message.get(\"payload\"),\n                    }\n                except Exception:\n                    logger.warning(\n                        f\"Unable to parse websocket message as valid json: {message.data.decode('utf-8')}\"\n                    )\n                    return\n\n                logger.debug(\n                    f\"Publishing mqtt message from websockets at {json_message['topic']}.\"\n                )\n                self.receiver(\n                    json_message[\"topic\"],\n                    json_message[\"payload\"],\n                )\n\n        # start a websocket server on 5002\n        WebSocketWSGIHandler.http_version = \"1.1\"\n        self.websocket_server = make_server(\n            \"127.0.0.1\",\n            5002,\n            server_class=WSGIServer,\n            handler_class=WebSocketWSGIRequestHandler,\n            app=WebSocketWSGIApplication(handler_cls=_WebSocketHandler),\n        )\n        self.websocket_server.initialize_websockets_manager()\n        self.websocket_thread = threading.Thread(\n            target=self.websocket_server.serve_forever\n        )\n        self.websocket_thread.start()\n\n    def publish(self, topic: str, payload: str, _: bool) -> None:\n        try:\n            ws_message = json.dumps(\n                {\n                    \"topic\": topic,\n                    \"payload\": payload,\n                }\n            )\n        except Exception:\n            # if the payload can't be decoded don't relay to clients\n            logger.debug(f\"payload for {topic} wasn't text. Skipping...\")\n            return\n\n        if self.websocket_server is None:\n            logger.debug(\"Skipping message, websocket not connected yet\")\n            return\n\n        try:\n            self.websocket_server.manager.broadcast(ws_message)\n        except ConnectionResetError:\n            pass\n\n    def stop(self) -> None:\n        self.websocket_server.manager.close_all()\n        self.websocket_server.manager.stop()\n        self.websocket_server.manager.join()\n        self.websocket_server.shutdown()\n        self.websocket_thread.join()\n        logger.info(\"Exiting websocket client...\")\n", "path": "frigate/comms/ws.py"}]}