# Websocket Client doesnt reconnect to Websocket Server

**URL:** <https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941>\
**Category:** General\
**Created:** [7 February 2022 17:46 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941 "2022-02-07T17:46:14Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcom](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mcom](https://discourse.nodered.org/u/mcom)\
**Post date:** [7 February 2022 17:46 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/1 "2022-02-07T17:46:14Z")

</div>

Hello together my name is Mo,

im totally new in Node Red. So please excuse me when i response with stupid questions 🙂

I have a websocket server running on a esp32 and a node red websocket client on a pi. the connection is working well, but if i restart the esp then the client (node red) doesnt reconnect automatically. To reconnect the client, i have to restart the flows.  
Is there a way to reconnect it automatically?

Thank you and nice greetz  
Mo

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 February 2022 22:38 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/2 "2022-02-07T22:38:57Z")

</div>

Most people are putting ESP comms via MQTT which is designed for this kind of thing and is generally a lot more robust than a websocket connection.

I've not used NR's websocket to connect this way so I don't really know if it handles retries.

---

<div class="post-metadata">

**Author:** ![mcom](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mcom](https://discourse.nodered.org/u/mcom)\
**Post date:** [7 February 2022 22:57 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/3 "2022-02-07T22:57:21Z")

</div>

Hey Julian,  
thank you for your fast reply. I have tried MQTT already but i dont get a Connection to the Server. Websocket Server is already on the ESP. Thats why i choose this way. The problem with the MQTT could be the softAP Mode of the ESP or the Websocket Server.

---

<div class="post-metadata">

**Author:** ![lu4t](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lu4t/32/32276_2.png) [@lu4t](https://discourse.nodered.org/u/lu4t)\
**Post date:** [7 February 2022 23:29 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/4 "2022-02-07T23:29:46Z")

</div>

I'm not an expert on ESP32, but: what prevents you from connecting mqtt over websockets?.  
Based on your comments, I am guessing you're doing websockets over http, and this is why you need to reestablish the handshake.  
Mqtt should be the transport used, as @TotallyInformation has already suggested.

---

<div class="post-metadata">

**Author:** ![mcom](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mcom](https://discourse.nodered.org/u/mcom)\
**Post date:** [7 February 2022 23:52 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/5 "2022-02-07T23:52:46Z")

</div>

Hey Lu4t,

yes im using websocket over http. I didnt try mqtt over websockets.  
Do you have some example?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 February 2022 23:58 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/6 "2022-02-07T23:58:15Z")

</div>

Sorry, I don't understand why you aren't using MQTT directly. If there isn't a route to your normal MQTT broker, stand up another broker on the same device as Node-RED.

---

<div class="post-metadata">

**Author:** ![mcom](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mcom](https://discourse.nodered.org/u/mcom)\
**Post date:** [8 February 2022 13:33 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/7 "2022-02-08T13:33:58Z")

</div>

Hey Julian,

i have an existing project on my ESP. There is a Webserver and Websocket Server implemented. MQTT i have tried at the beginning, but i cant get a connection to the broker. With only a example Project from the MQTT library (ESP), the connection to the broker was okay. But not with my existing project. I have to mention that the ESP is configuerd as an AccesssPoint and the Pi as a Wifi Client. Thats why i choose this way. If you would help me to get a connection with MQTT, i would be happy 🙂 sorry for my bad english grammar

EDIT: My MQTT broker and Node Red is running on the same device (PI). Theyy are working well. I have tried them with an MQTT Explorer from my Notebook

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 February 2022 15:55 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/8 "2022-02-08T15:55:45Z")

</div>

> [@mcom](#):
>
> ESP is configuerd as an AccesssPoint

OK, that is likely the issue I guess, you'd have to look in detail at how the network is behaving to understand what is happening.

I truth though, I would have thought that it would be better ot use the Pi as the AP not the ESP since the Pi has a lot more umph and more capabilities.

---

<div class="post-metadata">

**Author:** ![mcom](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mcom](https://discourse.nodered.org/u/mcom)\
**Post date:** [8 February 2022 18:03 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/9 "2022-02-08T18:03:21Z")

</div>

> [@TotallyInformation](#):
>
> I truth though, I would have thought that it would be better ot use the Pi as the AP not the ESP since the Pi has a lot more umph and more capabilities.

You are right, but in the beginnig i did not plan to connect the ESP with my network. To catch some values.

> [@TotallyInformation](#):
>
> OK, that is likely the issue I guess, you'd have to look in detail at how the network is behaving to understand what is happening.

I think i have to go this way. maybe i have to reduce library by library!?  
to find out the reason.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 February 2022 20:49 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/10 "2022-02-08T20:49:49Z")

</div>

> [@mcom](#):
>
> maybe i have to reduce library by library

I would have thought that it is more likely a networking issue than a library one.

---

<div class="post-metadata">

**Author:** ![mcom](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mcom](https://discourse.nodered.org/u/mcom)\
**Post date:** [9 February 2022 12:12 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/11 "2022-02-09T12:12:26Z")

</div>

Hey Julian,

i get a Connection between the MQTT Broker and the ESP. I have just configured the ESP as WiFiClient. I try to check why? I add a reconnect function and via Serial Connecton i can observe, if it is configured as an AP the WiFIClient dont get a Connection Feedback and dont connect the MQTT Client.

Thank you for your support.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 February 2022 12:41 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/12 "2022-02-09T12:41:58Z")

</div>

Well the AP setting for ESP is generally only meant to handle an initial setup, it isn't meant to be a full-on WiFi provision - the ESP's aren't powerful enough for that really and they certainly aren't designed for that.

If you really wanted to have something that would work without a "proper" Wi-Fi in place, I would recommend using the ESPnow networking. Add another ESP with a serial connection to the Pi so that you can get data in/out and use that instead of MQTT between the ESPs and the Pi. ESPnow is a mesh network on the same frequencies as Wi-Fi.

Or just use the Pi as the AP if network coverage isn't an issue.

---

<div class="post-metadata">

**Author:** ![mcom](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mcom](https://discourse.nodered.org/u/mcom)\
**Post date:** [10 February 2022 11:59 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/13 "2022-02-10T11:59:49Z")

</div>

Hey Julian,

> [@TotallyInformation](#):
>
> If you really wanted to have something that would work without a "proper" Wi-Fi in place, I would recommend using the ESPnow networking. Add another ESP with a serial connection to the Pi so that you can get data in/out and use that instead of MQTT between the ESPs and the Pi. ESPnow is a mesh network on the same frequencies as Wi-Fi.

I´ve heard about it. But i dont spend time to read about.  
This sounds good for my next Project. There are 3 ESP´s.

> [@TotallyInformation](#):
>
> Or just use the Pi as the AP if network coverage isn't an issue.

For this issue i will do that.

Thank You again!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 February 2022 11:59 UTC](https://discourse.nodered.org/t/websocket-client-doesnt-reconnect-to-websocket-server/57941/14 "2022-02-24T11:59:54Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
