title: Pinging Websocket server with Websocket .NET client
body: <p>I'm trying to figure out how to send <code>ping</code>/<code>pong</code> heartbeats in order to be sure to maintain a connection with a Websocket server using <a href="https://github.com/Marfusios/websocket-client" rel="nofollow noreferrer">Websocket .NET client</a> in a .NET Core WPF Application, so I'm testing with <a href="https://binance-docs.github.io/apidocs/spot/en/#websocket-market-streams" rel="nofollow noreferrer">this Binance Websocket</a> which says</p>
  <blockquote>
  <p>The websocket server will send a <code>ping frame</code> every 3 minutes. If the websocket server does not receive a <code>pong frame</code> back from the connection within a 10 minute period, the connection will be disconnected. Unsolicited <code>pong frames</code> are allowed.</p>
  </blockquote>
  <p>However, when I try to connect using my sample code, I simply never receive any message from the websocket that contains the string <code>ping</code>, and the connection seems to never fail if there is at least one stream subscribed to. Also sending unsolicited <code>pong</code> messages seems to always throw an  <code>error 3: invalid JSON</code> followed by a disconnection.</p>
  <p><strike>Are <code>ping</code>/<code>pong</code> heartbeats managed by the NuGet Package <em>(even though I didn't find any line that mentions <code>ping</code> or <code>pong</code> in its source code)</em>, or simply doesn't support them? If so then how/why is my connection to Binance Websocket not getting disconnected?</strike></p>
  <p>Here's my sample code for the <code>WSClient</code> class so you can try and see that you won't get disconnected even after +15mins:</p>
  <p><strong>Update:</strong> It's handled internally by the <code>WebsocketClient</code>, and the <code>KeepAliveInterval</code> is actually sending <code>pong</code> frames each interval. I wonder though if it's possible to subscribe some event to see when these frames are being sent, which could be useful when dealing with strict messages rates limitations to not get accidently disconnected when sending the max number of messages at the same time as the pong frame is being sent.</p>
  <pre><code>using System;
  using System.Net.WebSockets;
  using Websocket.Client;
  using Websocket.Client.Models;
  using System.Threading.Tasks;
  using System.Reactive.Linq;
  using System.Collections.Generic;
  using System.Linq;
  using System.Diagnostics;
  
  public class WSClient
  {
      private List&lt;string&gt; logs = new List&lt;string&gt;();
  
      private WebsocketClient client;
      private string wsUrl = &quot;wss://stream.binance.com/ws&quot;;
      
      public WSClient() { }
  
      public async Task InitializeClient()
      {
          Uri uri = new Uri(wsUrl);
          Func&lt;ClientWebSocket&gt; factory = new Func&lt;ClientWebSocket&gt;(() =&gt; {
              ClientWebSocket wsClient = new ClientWebSocket
              {
                  Options =
                  {
                      KeepAliveInterval = TimeSpan.FromSeconds(5) // interval to send pong frames             }
              };
  
              return wsClient;
          });
  
          client = new WebsocketClient(uri, factory);
          client.Name = &quot;WebsocketClient&quot;;
          client.ReconnectTimeout = TimeSpan.FromSeconds(30);
          client.ErrorReconnectTimeout = TimeSpan.FromSeconds(30);
  
          client.ReconnectionHappened.Subscribe(OnReconnection);
          client.DisconnectionHappened.Subscribe(OnDisconnect);
  
          client.MessageReceived
              .Where((ResponseMessage message) =&gt; message.MessageType == WebSocketMessageType.Text)
              .Subscribe(OnMessage);
  
          try
          {
              await client.StartOrFail();
          }
          catch (Exception ex)
          {
              logs.Add($&quot;Error: { ex.Message }&quot;);
          }
  
          // Subscribing to a stream (btcusdt@aggTrade for example)
          client.Send(&quot;{\&quot;method\&quot;: \&quot;SUBSCRIBE\&quot;,\&quot;params\&quot;:[\&quot;btcusdt@aggTrade\&quot;],\&quot;id\&quot;: 1}&quot;);
      }
  
      private void OnReconnection(ReconnectionInfo info)
      {
          logs.Add(&quot;Reconnected&quot;);
      }
  
      private void OnMessage(ResponseMessage message)
      {
          logs.Add(message.Text);
      }
  
      private void OnDisconnect(DisconnectionInfo info)
      {
          logs.Add(&quot;Diconnected&quot;);
      }
  }
  </code></pre>