title: Using Socket.io with typescript and react
body: <p>I am working on establishing a websocket connection between a Node app and a React app. However I cannot seem to get function this.io.on() to run in my listen method in class socket. The server logs "listen" but not 'Connected client on port %s.', this.port. My question is why it doesn't?</p>
  
  <p>File index.ts in server application.</p>
  
  <pre><code>/// &lt;reference types="@types/node" /&gt;
  
  // Imports.
  import * as http from 'http';
  import App from './App';
  import * as socketIo from 'socket.io';
  import { SocketIO, Server } from 'socket.io';
  
  const port = process.env.PORT || 3000;
  const server = http.createServer(App);
  
  // Start the server.
  App.listen(port, (err) =&gt; {
    if (err) {
    return console.error(err);
  }
    new socket(server);
  
    return console.log(`Server is listening on ${port}`);
  });
  
  
  class socket {
  
    private server: Server;
    private io: SocketIO.Server;
    private port = 3000;
  
    constructor(server) {
        this.createServer(server);
        this.sockets();
        this.listen();
    }
  
    private createServer(server: any): void {
        this.server = server;
    }
  
    private sockets(): void {
        this.io = socketIo(this.server);
    }
  
    private listen(): void {
        console.log("listen");
        this.io.on('connect', (socket: any) =&gt; {
            console.log('Connected client on port %s.', this.port);
            socket.on('message', (m: any) =&gt; {
                console.log('[server](message): %s', JSON.stringify(m));
                this.io.emit('message', m);
            });
  
            socket.on('disconnect', () =&gt; {
                console.log('Client disconnected');
            });
        });
    }
  }
  </code></pre>