2

Is there anyway to send a UDP packet to a local port (and receive UDP packets from a local port) with Javascript?

I don't want to have to run node.js, although there is a datagram object there. I'm using IE, so can't use the Mozsockets or chrome.udp.sockets objects.

Could I host a swf in an iFrame and use flash to send from javascript (via Flash) to a local port, for example? That's the only think of so far.

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
user2146441
  • 220
  • 1
  • 19
  • 43
  • 1
    Is ActiveX an option? How about Java? Is the page on which you want to use this hosted on `localhost`, too? – Phillip Apr 03 '15 at 13:22
  • See http://stackoverflow.com/questions/18799364/webrtc-vs-websockets-if-webrtc-can-do-video-audio-and-data-why-do-i-need-web – guest271314 Apr 04 '15 at 20:48
  • if you can live with passing strings, then localStorage+"storage" window events are a fast local solution. – dandavis Apr 06 '15 at 00:37

1 Answers1

1

Short answer: No, there is no way to do this in Javascript for security reasons.

Long answer: Some plugins support UDP communications. For example, you could use Flash's Adobe AIR's DatagramSocket. If you are interfacing with an existing API which you cannot change, this might be your only option.

However, if you are building this app from scratch and intend to deploy it on the web, I would strongly suggest that you consider a different mode of transport. Take a look at LocalStorage or Shared Workers for browser-based IPC, Websockets for asynchronous client-server communication and WebRTC for peer-to-peer communication (although support for this standard is still a work in progress). This will allow you to support Apple devices and Linux as Adobe drops flash support on the platform, as well as provide better security guarantees than Flash or Java applets.

Dylan MacKenzie
  • 632
  • 5
  • 13
  • shared workers are even better than localStorage, but alas IE is not on-board yet. good overview! – dandavis Apr 07 '15 at 22:38
  • Interesting, I wasn't aware you could share a context between web workers. I'll add this to the answer. – Dylan MacKenzie Apr 07 '15 at 22:55
  • Your long answer is probably wrong: "The DatagramSocket class can only be used in Adobe AIR applications and only in the application security sandbox." – Phillip Apr 08 '15 at 06:03
  • using a shared worker instead of localStorage means you don't have to contend with sync file IO overhead, converting all data to strings, being limited to 5mb messages, or risk writing over permanent local data. oh, and the perf is much better as well since everything is kept in RAM the whole time. IE can also use the clipboard, FSO to hit a temp RAMdisk, and a local IIS server can use the Application object to share RAM-saved state via local HTTP connections. – dandavis Apr 08 '15 at 17:37
  • Implementations of localStorage generally read and write to an in-memory cache so it's not like you'd hit the disk with every get or setItem(). That said, it's clearly a hack. – Dylan MacKenzie Apr 08 '15 at 21:56
  • @DylanMacKenzie: which browser uses a RAM-based implementation? in windows task manager, a chrome tab, before running this code did about 3-8 IO/sec just sitting there, while running `setInterval(function(){ localStorage.junk=Array(16).join(Math.random()) }, 1)`, it instantly has hundreds of "I/O Writes" per second. i think localStorage is blocking, sync, and goes right to disk. even if it's cached, it's still dumped to disk right away, which makes it a heavy consumer of power and resources. – dandavis Apr 09 '15 at 08:54
  • @DylanMacKenzie: just for fun, i pulled the plug on a desktop just after setting localStorage (well under a second later) and the value was there when it rebooted and reloaded... i think that means that yes, it really does " hit the disk with every setItem", based on everything i can tell. – dandavis Apr 09 '15 at 09:06
  • But your JavaScript won't block until these writes are complete. The OS is responsible for actually persisting the data to disk and will do so independent of your JavaScript. The file io only blocks when localStorage is first read into memory. http://calendar.perfplanet.com/2012/is-localstorage-performance-a-problem/ – Dylan MacKenzie Apr 09 '15 at 09:40
  • I could have been clearer about what i meant by "hit the disk". I definitely wouldn't want to use it for anything requiring any significant amount of throughout – Dylan MacKenzie Apr 09 '15 at 09:57