Node JS UDP Broadcast Example

This example will show you how to use Node.JS to create a client and server which will use UDP protocol to communicate. First, you need to import the node js dgram module. This module will control all UDP-related issues such as create a UDP client, create a UDP server, and then you can use the client to broadcast the UDP packet to the UDP server.

1. Node JS UDP Broadcast Example.

  1. There are two js files in this example, they are udp-client.js, and udp-server.js. The source code is listed below.

1.1 UDP Server JS File.

  1. When executing this file, it will create a UDP server socket which will listen on localhost with 8089 port number.
  2. When the server receives a message sent from a client, it will print the message in standard output ( log console in this example ).
  3. udp-server.js
    // Require dgram module.
    var dgram = require('dgram');
    
    // Create udp server socket object.
    var server = dgram.createSocket("udp4");
    
    // Make udp server listen on port 8089.
    server.bind(8089);
    
    // When udp server receive message.
    server.on("message", function (message) {
        // Create output message.
        var output = "Udp server receive message : " + message + "\n";
        // Print received message in stdout, here is log console.
        process.stdout.write(output);
    });
    
    // When udp server started and listening.
    server.on('listening', function () {
        // Get and print udp server listening ip address and port number in log console. 
        var address = server.address(); 
        console.log('UDP Server started and listening on ' + address.address + ":" + address.port);
    });

1.2 UDP Client JS File.

  1. When you execute the udp-client.js file, it will get user input text from the command line console.
  2. Until the user inputs ‘send\n’ in the command line, it will send all user input text to the UDP server.
  3. udp-client.js
    // Require node js dgram module.
    var dgram = require('dgram');
    
    // Create a udp socket client object.
    var client = dgram.createSocket("udp4");
    
    // message variable is used to save user input text.
    var message = "";
    
    // Set command line input character encoding to utf-8.
    process.stdin.setEncoding('utf-8');
    
    // When receive user input data in console.
    process.stdin.on('data', function (text) {
    
        // If user input 'send\n' then send all user input data to udp server.
        if('send\n' === text) {
    
            // If user do not input data in command line then send default message.
            if (message == null || message.length == 0) {
                message = "Hello udp server.";
    
            }
    
            console.log("User input : " + message);
    
            // Create a node Buffer object to wrap message object.
            message = new Buffer(message);
    
            // Send message to udp server through client socket.
            client.send(message, 0, message.length, 8089, "localhost");
    
        }else{
            // Concat all user input text in message.
            message += text;
        }
    });

1.3 Execute UDP Broadcast Example.

  1. Open a terminal and run the below command in it to start the UDP server.
    $ node udp-server.js
    UDP Server started and listening on 0.0.0.0:8089
  2. Open another terminal and run the below command to start the UDP client application.
    $ node udp-client.js 
    i love node js
    it is a very good framework
    send
    User input : i love node js
    it is a very good framework
    
  3. Move back to udp-server.js running terminal, you can see below output which means the server has received client sent message.
    $ node udp-server.js
    UDP Server started and listening on 0.0.0.0:8089
    Udp server receive message : i love node js
    it is a very good framework

3 thoughts on “Node JS UDP Broadcast Example”

  1. This does not work on a Windows computer.
    For it to work on Windows you need to change
    if(‘send\n’ === text) {

    to
    if(‘send\r\n’ === text) {

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.