# WebSocket ping pong

**URL:** <https://discourse.nodered.org/t/websocket-ping-pong/16035>\
**Category:** Feature Requests\
**Created:** [28 September 2019 21:40 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035 "2019-09-28T21:40:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtnbrit](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mtnbrit/32/12614_2.png) [@mtnbrit](https://discourse.nodered.org/u/mtnbrit)\
**Post date:** [28 September 2019 21:40 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035/1 "2019-09-28T21:40:00Z")

</div>

Hi, I see quite a few folks have trouble with websockets getting stuck half open due to the other end rebooting for example. Does the node-red websocket implementation include ping and ping to keep the connections alive? If not, could it be added? RFC6455 describes ping and pong.

---

<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:** [28 September 2019 22:48 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035/2 "2019-09-28T22:48:02Z")

</div>

I believe that ping and pong are automatic. I haven't used raw ws for a while but I think that it does include them by default and there is nothing to do. There should be events for them which you could listen to in your front-end code.

Certainly [Socket.IO](http://Socket.IO) includes them as I've tested them in uibuilder.

---

<div class="post-metadata">

**Author:** ![mtnbrit](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mtnbrit/32/12614_2.png) [@mtnbrit](https://discourse.nodered.org/u/mtnbrit)\
**Post date:** [28 September 2019 23:56 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035/3 "2019-09-28T23:56:25Z")

</div>

I looked at the source code for the ws module and I didnt see any sign of ping pong being handled or generated. If there was a heartbeat/keepalive, the connection could be re-established if it drops. Right now it seems the only way to keep the ws connected over other-end restarts is to somehow detect it has dropped and restart the whole node-red flow - seems like a bit of a kludge when the ping/pong mechanism exists to solve just this problem at the socket/driver layer.

---

<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:** [29 September 2019 00:38 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035/4 "2019-09-29T00:38:48Z")

</div>

OK, so it looks like the ws library doesn't automatically do ping/pong but you can implement it:

> **[ws](https://www.npmjs.com/package/ws#how-to-detect-and-close-broken-connections)**
>
> Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js

---

<div class="post-metadata">

**Author:** ![mtnbrit](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mtnbrit/32/12614_2.png) [@mtnbrit](https://discourse.nodered.org/u/mtnbrit)\
**Post date:** [29 September 2019 01:19 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035/5 "2019-09-29T01:19:27Z")

</div>

When you say "you can implement it".... do you mean it can be implemented? Im assuming this would need to be implemented in the core code of node-red by the devs including this ws lib (if it isnt already), and add the heartbeat calls in their WebSocket connector module? Seems like something that just should be a feature, no? Or am I missing something obvious with how a user could enable this?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 September 2019 07:31 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035/6 "2019-09-29T07:31:39Z")

</div>

Which websocket code are we talking about ? The one the editor uses to talk to the runtime ( for debug tab etc) or the websocket node(s) in the palette that are used in flows ?

---

<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:** [29 September 2019 11:07 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035/7 "2019-09-29T11:07:38Z")

</div>

> [@mtnbrit](#):
>
> When you say "you can implement it".... do you mean it can be implemented?

😉 yes, there is an example in the link I gave so it certainly can be implemented.

But as Dave says, where specifically is the issue you are seeing?

---

<div class="post-metadata">

**Author:** ![mtnbrit](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mtnbrit/32/12614_2.png) [@mtnbrit](https://discourse.nodered.org/u/mtnbrit)\
**Post date:** [29 September 2019 14:12 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035/8 "2019-09-29T14:12:21Z")

</div>

The websocket nodes used in flows

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [14 November 2019 19:31 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035/9 "2019-11-14T19:31:43Z")

</div>

+1 for this, is there a way to manually send a pong frame?

---

<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:** [14 November 2019 20:23 UTC](https://discourse.nodered.org/t/websocket-ping-pong/16035/10 "2019-11-14T20:23:15Z")

</div>

I expect it could be done somehow but it kind of defeats the object of ping/pong.

You can certainly run a function on receipt of a pong.
