title: Nodejs ws module: Heartbeat in TypeScript
body: <p>I'm currently working on getting a WebSocket server running on NodeJs with TypeScript. As a WebSocket server implementation, I am using <a href="https://www.npmjs.com/package/ws" rel="nofollow noreferrer">ws</a>. Along with that I use the <a href="https://www.npmjs.com/package/@types/ws" rel="nofollow noreferrer">@types/ws</a> package for the typings.
  I want the server to periodically send out a ping package to every connected client. The readme file of the ws module already provides an example implementation in JavaScript which I would like to use aswell.
  You can find the example implementation <a href="https://www.npmjs.com/package/ws#how-to-detect-and-close-broken-connections" rel="nofollow noreferrer">here</a>:</p>
  <p>The problem I am facing is that the sample implementation adds the &quot;isAlive&quot; attribute to a socket object which I obviously can not do in TypeScript since the &quot;isAlive&quot; attribute does not exist for the socket class.</p>
  <p>My question:
  What is the best way to add this attribute to the socket class? Since I pass these socket objects around as parameters, I want to avoid adding some sort of extra import to all my files that somehow work with a socket.</p>
  <p>I want to avoid typecasting the socket object to any for this.</p>
  <p>I try to implement a wrapper class around ws, so that I can just instanciate my WebSocket class and use it throughout my application.</p>
  <p>My class looks like this</p>
  <pre class="lang-js prettyprint-override"><code>import * as ws from &quot;ws&quot;;
  
  
  export default class WebSocketServer {
    private wss: ws.Server;
    private onSubscription: (socket: ws, deviceId: string, topicFilter: string) =&gt; void;
  
    constructor(port: number, onSubscription: (socket: ws, deviceId: string, topicFilter: string) =&gt; void) {
      this.wss = new ws.Server({ port });
      this.onSubscription = onSubscription;
      const conhandler = this.handleConnection.bind(this);
      this.wss.on(&quot;connection&quot;, conhandler);
    }
  
    public static init(onSubscription: (socket: ws, deviceId: string, topicFilter: string) =&gt; void): WebSocketServer {
      const port: number = Number(process.env.WSS_PORT);
      if (!port) {
        throw new Error(&quot;Unable to create the WebSocket server: Environment variable \&quot;WSS_PORT\&quot; is missing&quot;);
      }
  
      const server: WebSocketServer = new WebSocketServer(port, onSubscription);
  
      console.info(`Websocket Server listening on port ${port}`);
      return server;
    }
  
  
    private handleConnection(socket: ws): void {
      const messageHandler: (socket: ws, data: ws.Data) =&gt; void = this.handleMessage.bind(this);
      socket.on(&quot;message&quot;, data =&gt; messageHandler(socket, data));
  
      /* This is where I try to add the isAlive attribute to a socket object */
      /* but since ws does not have an attribute isAlive, TypeScript complains */
      socket.on(&quot;pong&quot;, () =&gt; socket.isAlive = true);
    }
  
  
    private handleMessage(socket: ws, data: ws.Data): void {
      /* Do stuff with the message */
    }
  
  
    public quit() {
      this.wss.close();
    }
  }
  </code></pre>
  <h2>Edit</h2>
  <p>As mentioned by a commenter, i tried declaration merging. I created a new file called &quot;Augment.ts&quot; and added the following code:</p>
  <pre class="lang-js prettyprint-override"><code>import * as ws from &quot;ws&quot;;
  declare module &quot;ws&quot; {
    interface WebSocket {
      isAlive: boolean;
    }
  }
  </code></pre>
  <p>In the file that contains my WebSocketServer class, i added the import for the Augment.ts file like so: <code>import &quot;./Augment&quot;</code>. The result is another error message (but somehow stating the same?):
  <code>Property 'isAlive' does not exist on type 'import(&quot;c:/path/to/project/node_modules/@types/ws/index.d.ts&quot;)'.ts(2339)</code></p>