Node JS UDP Broadcast Example

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

1. Node JS UDP Broadcast Example.

node js udp broadcast example source file list

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

1.1 UDP Server JS File.

When execute this file, it will create a udp server socket which will listen on localhost with 8089 port number. When the server receive message sent from client, it will print the message in standard output ( log console in this example ).

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.

When you execute udp-client.js file, it will get user input text from command line console. Until user input ‘send\n’ in command line, it will send all user input text to udp server.

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 below command in it to start udp server.
    $ node udp-server.js
    UDP Server started and listening on 0.0.0.0:8089
  2. Open another terminal and run below command to start 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
(Visited 1,660 times, 9 visits today)
READ :   Node JS EventEmitter Example

Leave a Reply

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.