views:

1052

answers:

5

Is that possible?

I'm writing a web-admin interface for a network-system. I need to listen to a specific UDP port and show it in browser. Can I do that in javascript? Also; any other ideas are welcomed.

early-edit: Here's another idea: I have php and perl support at server-side. JS can call a php script to start listening, which will also call a perl script, which has an endless listener loop and collects the received messages into a sink variable. In theory; JS will be polling a PHP script every 2 seconds, get the sink variable and show it on browser/html. But; how can a php script reach/read that sink variable? And, how or when will the perl script stop? For the first part, using a file as a sink is a terrible idea; I'll be receiving 10 string messages per second and also reading and then clearing file contents every 2 seconds. Not to mention the file lock mechanisms... For the second part: i should put timeout somewhere but; where?

edit: Yes, I can choose where the listening will happen (server or client).

edit2: Why do I want to listen for UDP messages on a specific port and show it on browser: The project I'm working on has some special sensors activated by some bussiness-specific-keys, these sensor devices are network-enabled and I have programmed them to send a UDP network broadcast message containing biz-key-info whenever they sense something. There are lots of these devices on same network, and the web-admin interface should have a page where user can see what is happening in real-time or almost-real-time (~5 second delay is acceptable, that is why I had the poll-every-2seconds idea at my early-edit).

edit3: I have also found this: ActiveSocket which is an ActiveX component but; as predicted it didnt work on Firefox and has no documentation about it's html+JS part.

A: 

Javascript Code has no access to sockets. Well almost, you should be able to get something to work in IE6 using ActiveX but that about the only platform your code will ever work.

Oliver Weichhold
+1  A: 

you could probably do this using a hidden flash .swf and some glue code. no way with pure javascript

Scott Evernden
A: 

No, you can't listen to sockets in JavaScript.

As to using a scripting language and then polling it for data, it's possible, but that sort of long-polling setup is unusual. Why do you wish to do things in this way? In particular, why do you need to listen for traffic on particular ports and display it in a web browser? Are you wanting to check network traffic on the machine where the browser is running, or are the browser and the scripts on separate machines? Typically a web-admin setup is used for remote management, which is why I ask the question.

There may be a better way to accomplish your goal, but it's hard to tell without some more general information about the application you're trying to write.

Adam Bellaire
+3  A: 

The simpelst solution would be a Flash-Brigdem, since in Javascript there is no way to directly connect to a socket.

I've done something like that some time ago you can download the full source here. You can run simpleserver.py(requires python3) and then access the index.htm via localhost, in order to test it.

Also note, Flash's security model wants an XML-Document on port 843, it will send

<policy-file-request/>

to the socket. You then should respond with something like:

<?xml version="1.0"?><cross-domain-policy>
  <allow-access-from domain="*" to-ports="*" />
  </cross-domain-policy>

If you don't have anything running on Port 843 it will send the same request to the port you are trtying to connect to. But only after the 843 request has timed out.

There are some other security "features", but you can read about those in the Adobe docs.

Ivo Wetzel
+1  A: 

Which computer will listen ? your server or the client that runs the web browser ? Can you choose ?

If the listening can be done on the server, the easiest is to listen from a PHP webpage, but this is not concurrent, so if the user hits F5 a little bit too fast, only 1 php process can listen at a time, and it won't work.

Another solution is as you said, create a listener daemon in whatever language (perl, python) which is also a HTTP server (takes 10 lines of code, import the required module) and PHP connects to it via a simple HTTP request to ask it for the last stuff that happened. The daemon only needs to keep as much history as you need, if that's short, put it in a simple array.

peufeu