title: How to listen to different UDP ports on the same address within the same process in Node.js
body: <p>I'm writing a Node.js app to control a small drone. Here are the instructions from the SDK:</p>
  <hr />
  <p>Use Wi-Fi to establish a connection between the Tello and PC, Mac, or mobile device.</p>
  <h2>Send Command &amp; Receive Response</h2>
  <p>Tello IP: 192.168.10.1 UDP PORT: 8889 &lt;&lt;--&gt;&gt; PC/Mac/Mobile</p>
  <p>Step 1: Set up a UDP client on the PC, Mac, or mobile device to send and receive message from the Tello via the same port.</p>
  <p>Step 2: Before sending any other commands, send 'command' to the Tello via UDP PORT 8889 to initiate SDK mode.</p>
  <h2>Receive Tello State</h2>
  <p>Tello IP: 192.168.10.1 --&gt;&gt; PC/Mac/Mobile UDP Server: 0.0.0.0 UDP PORT: 8890</p>
  <p>Step 3: Set up a UDP server on the PC, Mac, or mobile device and check the message from IP 0.0.0.0 via UDP PORT 8890. Steps 1 and 2 must be completed before attempting step 3.</p>
  <h2>Receive Tello Video Stream</h2>
  <p>Tello IP: 192.168.10.1 --&gt;&gt; PC/Mac/Mobile UDP Server: 0.0.0.0 UDP PORT: 11111</p>
  <p>Step 4: Set up a UDP server on the PC, Mac, or mobile device and check the message from IP 0.0.0.0 via UDP PORT 11111.</p>
  <p>Step 5: Send 'streamon' to the Tello via UDP PORT 8889 to start streaming. Steps 1 and 2 must be completed before attempting step 5.</p>
  <hr />
  <p>The Command &amp; Receive part works like a charm and I am sending and receiving datagrams to/from the drone on port 8889. My problem is that it looks like I'm not receiving any State or Video Stream messages on the other ports and I'm pretty sure that it's not a problem with the drone but something I'm not setting up properly with Node. Can anyone see where the problem is in my implementation. Here is my code:</p>
  <p><em>tello.ts</em></p>
  <pre><code>import dgram from 'dgram';
  
  export class Tello {
    private LOCAL_IP_ = '0.0.0.0';
    private TELLO_IP_ = '192.168.10.1';
  
    private COMMAND_PORT_ = 8889;
    private commandSocket_ = dgram.createSocket('udp4');
  
    private STATE_PORT_ = 8890;
    private stateSocket_ = dgram.createSocket('udp4');
  
    private VIDEO_PORT_ = 11111;
    private videoSocket_ = dgram.createSocket('udp4');
  
    constructor() {}
  
    startCommandSocket() {
      this.commandSocket_.addListener('message', (msg, rinfo) =&gt; {
        const message = msg.toString();
        console.log(`from ${rinfo.address}: ${message}`);
      });
      this.commandSocket_.bind(this.COMMAND_PORT_, this.LOCAL_IP_, () =&gt; {
        console.log('Started listening on the command socket');
      });
    }
  
    startStateSocket() {
      this.stateSocket_.addListener('message', (msg, rinfo) =&gt; {
        const message = msg.toString();
        console.log(`from ${rinfo.address}: ${message}`);
      });
      this.stateSocket_.bind(this.STATE_PORT_, this.LOCAL_IP_, () =&gt; {
        console.log('Started listening on the state socket');
      });
    }
  
    startVideoSocket() {
      this.videoSocket_.addListener('message', (msg, rinfo) =&gt; {
        console.log('receiving video');      
        const message = msg.toString();
        console.log(`from ${rinfo.address}: ${message}`);
      });
      this.videoSocket_.bind(this.VIDEO_PORT_, this.LOCAL_IP_, () =&gt; {
        console.log('Started listening on the video socket');
      });
    }
  
    private sendCommand_(command: string) {
      // As this is sent over UDP and we have no guarantee that the packet is received or a response given
      // we are sending the command 5 times in a row to add robustess and resiliency.
      //for (let i = 0; i &lt; 5; i++) {
      this.commandSocket_.send(command, this.COMMAND_PORT_, this.TELLO_IP_);
      //}
      console.log(`sending command: ${command} to ${this.TELLO_IP_}`);
    }
  
    /**
     * Enter SDK mode.
     */
    command() {
      this.sendCommand_('command');
    }
  
    /**
     * Auto takeoff.
     */
    takeoff() {
      this.sendCommand_('takeoff');
    }
  
    /**
     * Auto landing.
     */
    land() {
      this.sendCommand_('land');
    }
  
    streamVideoOn() {
      this.sendCommand_('streamon');
    }
  
    streamVideoOff() {
      this.sendCommand_('streamoff');
    }
  
    ...
  
  }
  </code></pre>
  <p><em>index.ts</em></p>
  <pre><code>import { waitForSeconds } from './utils';
  import { Tello } from './tello'
  
  const main = async () =&gt; {
    const tello = new Tello();
  
    tello.startCommandSocket();
    await waitForSeconds(1);
    tello.command();
    await waitForSeconds(1);  
    tello.startStateSocket();
    await waitForSeconds(1);
    tello.startVideoSocket();
    await waitForSeconds(1);
    tello.streamVideoOn();
    await waitForSeconds(1);
      
    tello.takeoff();
    await waitForSeconds(10);
    tello.land(); 
  };
  
  main();
  </code></pre>