Search code examples
javascriptnode.jssocketsportbroker

NodeJS simple UDP client server application using broker


I am writing a simple client server NodeJS application using UDP protocol. The main point of this application is that it requires the use of a broker, whose function, in the given case, is to link the sender with the receiver. The requirements tell me that the sender doesn't need to be aware of the receiver's IP address and port number - it only needs to know the broker's corresponding IP and PORT. Afterwards, the broker will send the client's message to the server, based on the server's IP and PORT.

To clarify the previous(?confusing) paragraph, below you will find a illustration of what I've done so far:

sender.js

var PORT1 = XXXXX;
var HOST = '127.0.0.1';
var fs = require('fs');
var dgram = require('dgram');
var client = dgram.createSocket('udp4');


fs.readFile('Path/to/the/file','utf8', function (err, data) {
  if (err) throw err;
  var message = new Buffer(data);

  client.send(data, 0, message.length, PORT1, HOST, function(err, bytes) {
    if (err) throw err;
    console.log('UDP message sent to ' + HOST +':'+ PORT1);
    client.close();
    });
});

The code above reads from a file, stores its contents in a buffer and sends it to the broker's port(the broker listens to the same port) and host(which, in my case is the localhost).

broker.js

var PORT1 = XXXXX;
var PORT2 = YYYYY;
var HOST = '127.0.0.1';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
var client = dgram.createSocket('udp4');

server.on('listening', function () {
    var address = server.address();
    console.log('UDP broker listening on ' + address.address + ":" + address.port);
});

server.on('message', function (message, remote) {
  client.send(message, 0, message.length, PORT2, HOST, function(err, bytes) {
    if (err) throw err;
    console.log('UDP message sent to ' + HOST +':'+ PORT2);
    client.close();
  });
    console.log(remote.address + ':' + remote.port +' - ' + message);
});

server.bind(PORT1, HOST);

Here, PORT1 is the port that the broker listens to(waiting for incoming messages from sender) and PORT2 is the port which transmits the message to the receiver(and correspondingly, the receiver listens to this port).

receiver.js

var PORT2 = YYYYY;
var HOST = '127.0.0.1';
var fs = require('fs');
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
var parser = require('xml2json');

server.on('listening', function () {
    var address = server.address();
    console.log('UDP receiver listening on ' + address.address + ":" + address.port);
});

server.on('message', function (message, remote) {
    console.log(remote.address + ':' + remote.port +' - ' + message);
  var contents = fs.writeFile("/Path/To/Written/File", parser.toJson(message),
  function(error){
      if (error) {
        console.log("error writing");
       }
  console.log("File was saved");
    });
});

server.bind(PORT2, HOST);

The receiver gets the message from the broker and writes it to a file in the JSON format.

Here are the results:

Sender

UserName's-MacBook-Pro:UDP server UserName$ node sender.js 
UDP message sent to 127.0.0.1:XXXXX

Broker

UserName's-MacBook-Pro:UDP server UserName$ node broker.js 
UDP broker listening on 127.0.0.1:XXXXX
127.0.0.1:60009 - <?xml version="1.0"?>
<Some XML content here>
</XML content ends here>
UDP message sent to 127.0.0.1:YYYYY

Receiver

UserName's-MacBook-Pro:UDP server UserName$ node receiver.js 
UDP receiver listening on 127.0.0.1:YYYYY
127.0.0.1:63407 - <?xml version="1.0"?>
<XML contents here>
</XML content ends here>
File was saved

I am sorry for the long post, but I want to specify all the details to eliminate(hopefully) any ambiguities. Now, to the matter,

HERE is my question

What changes should I make for the broker in order to solve the following problem:

In case of multiple senders and receivers, the broker should manage the ports to link the sender to the receiver(with any specified criteria).

Thank you in advance!


Solution

  • Take a look in this book "Node.js Design Patterns,Publisher:Packt Publishing By: Mario Casciaro ISBN: 978-1-78328-731-4 Year: 2014" at page 361. There is the exact thing you want to do with very good explanation. Hope it will help!