5

I have a UDP dgram socket server written in node.js . here's the code snippet from server.js

 var PORT = 50000;
 var HOST = '0.0.0.0';
 var dgram = require('dgram');
 var server = dgram.createSocket('udp4');

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

server.on('message', function (message, remote) {
console.log('Message',  message);
});

server.bind(PORT, HOST);

I have a client which can connect with this server . here's the client.js file :

var PORT = 50000;
var HOST = '0.0.0.0';
var dgram = require('dgram');
var message = null;

var client = dgram.createSocket('udp4');


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

client.on('message', function (message, remote) {
console.log("got message from server ==> ",remote.address + ':' + 
remote.port +' - ' + message);
});

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

Uptil here...all is fine.. what i require is :

I need to put this client code into an html or jade page...so that i can connect to the server using a browser. I cannot change the server code :(

Nikhil
  • 467
  • 10
  • 22
  • Possible duplicate of [How to talk to UDP sockets with HTML5?](http://stackoverflow.com/questions/29532157/how-to-talk-to-udp-sockets-with-html5) – Joe Clay Oct 28 '16 at 14:23
  • (Short answer: you can't.) – Joe Clay Oct 28 '16 at 14:23
  • @JoeClay: no work around ....any wrapper that could be written around... – Nikhil Oct 28 '16 at 14:31
  • No, the browser is not capable of doing UDP communication directly. [WebRTC](https://www.html5rocks.com/en/tutorials/webrtc/datachannels/) is a UDP-based API that *is* available in the browser, but you'd have to rewrite your server code to support it. – Joe Clay Oct 28 '16 at 14:34
  • @JoeClay: thanks for the clarifications :) – Nikhil Oct 28 '16 at 14:38
  • No problem - you might also want to look into [Websockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API). They're TCP based, but it might still fit your needs. – Joe Clay Oct 28 '16 at 14:43
  • New wrinkle..... Is it possible for a browser to broadcast (and listen) via UDP? Strictly P2P. Via a Service Worker? – hypermiler Jan 27 '17 at 19:44

2 Answers2

5

From Javascript in a browser, you simply do not have the capability to do a plain UDP connection. Browser Javascript can't even do a plain TCP socket either. Javascript can do:

  • HTTP requests
  • webSocket connections
  • Server sent events
  • webRTC (in the process of being supported by browsers)

So, if you're looking to talk directly to your UDP server from straight browser Javascript, you cannot do that as of today.

With a constraint of not changing the UDP server in any way, these are the only options I can think of:

  1. Write a browser add-on that can talk to your server and then your Javascript can talk to the browser add-on. The client will, of course, have to download and install the browser add-on.

  2. Write a new server that can serve as a proxy for your UDP server (could easily be done in node.js). The new server would probably work best if it was a webSocket or socket.io server and then your browser Javascript could use webSocket or socket.io to talk directly to the proxy. The proxy would then handle the communication with your UDP server.


If you relieve the constraint of not modifying the server, the server design could be compatible with browser Javascript if you changed the server to be a webSocket server (or perhaps socket.io which is built on top of webSocket and adds a bunch of useful features).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • seems like a cumbersome solution but lets see if i could get it to work .i was thinking of some proxy server sort of thing but then there was server constraint..lets see if i could get relieved of this constraint anyhow.. – Nikhil Oct 28 '16 at 17:06
  • I didn't want to vote down the answer, because of the author's creativity :) but wrapping UDP inside TCP (https://en.wikipedia.org/wiki/WebSocket) is exactly not what you want, if you understand the main difference between those 2. – Mladen B. Feb 21 '21 at 09:12
  • @MladenB. - I'm not sure what your comment or your link to a webSocket description means . The original question poses a UDP server that you can't change and wants to connect to it from a browser. Since the browser supplies no UDP interface, the problem is overly constrained in that way so kludges are the only options. – jfriend00 Feb 21 '21 at 14:55
  • WebSockets are implemented over TCP, hence the link. Wrapping UDP communication into TCP protocol is pointless, due to the intentional differences in those 2 protocols. TCP is designed for cases where packet delivery should be guaranteed (e.g. download a file) and UDP is designed for live streaming where you don't care if you loose some packets, the next packets that arrive on the destination will continue the display of the live stream... – Mladen B. Feb 22 '21 at 08:50
  • 1
    @MladenB. - Please don't attempt to teach anyone here about the differences between UDP and TCP in this question. That is NOT the point at all of this question or this answer and is completely irrelevant to the problem at hand. The OP has a UDP service that they wish to communicate with from a browser. There are NO UDP options for doing that so you have to hack something together via either HTTP or webSocket to do so. Yes, it's a hack, but that's your only choice from browser Javascript. This question is about the options available only, not ideal design or theory. – jfriend00 Feb 22 '21 at 21:41
-7

Even though theyre saying it cant be done, the only thing that you have there that you can't just jam into a regular script file is the require statement. I'd still try to bundle that code up and see if it works.

var dgram = require('dgram');

That's it. If only you could figure out a way to get this in there your problems would all be solved. You can do this may ways. See if there is a client side version of this dgram library that you can just include as a script before your own client script is included into the html/jade document. This could potentially be a really cumbersome solution because the code/syntax may even need to change for such a client side library.

The better choice, I believe, is to use browserify. Browserify allows you to use the commonJS standard of requiring other files/libraries in the client side. Thus, if you develop a front end file called main.js, for instance, you can follow these steps to have browserify create a bundle with your main.js code and also the code from the dgram library, all jumbled together:

1- npm install browserify 2- go into package.json and create a new npm script (inside the "scripts" object) following the format of the test script:

"bundle": "browserify path/to/jsfile/main.js -o path/to/outputfile/prod.js"

3- create the html/jade file and have it link to the prod.js file that browserify will output 4- run the bundle script you created:

npm run bundle

5- Test the html file.

The above process will, if done correctly, create a single prod.js file with all the requires included within it.

  • 1
    browserify doesn't have the ability to magically add the ability to do UDP to a browser. – jfriend00 Oct 28 '16 at 16:08
  • @Victor Moreno: Have you had your code tested ...because your work around is not working....browser console shows error : prod.js:2019 Uncaught TypeError: dgram.createSocket is not a function(…) – Nikhil Oct 28 '16 at 16:49
  • @jfriend00 I didn't think it would, nor did I mean to imply it was gonna work for sure. I was just saying how to create a workflow where your browser will at least run the file so they could give it a shot. Nikhil, if it says dgram.createSocket is not a function that means that package is not being correctly included. However, as per the answers other people have provided, a web browser cannot do UDP sockets. – Victor Moreno Nov 10 '16 at 22:29