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.
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.
Related
What I have :
A C++ application server running, Ready to send data to client which is supposed to a HTML5 page or app.
What I want : Is there any way to communicate using udp port with HTML5 given both c++ server and HTML5 app are local to system ?
What I know :
Because of Security Concern, JS doesn't allow UDP port communication from browser.
Have read in many places, Answer is no. But answers are old.
Is the answer still 'NO' ?
Is there any work-around possible ?
Any lead is appreciated.
Yes, the answer is still 'no'. Websockets are TCP based. Note that a WebSocket is not a plain TCP connection, there is HTTP negotiation and a framing protocol in place. So you also cannot create a plain TCP connection in Javascript.
WebRTC is based on UDP, it may cover your use cases: http://www.html5rocks.com/en/tutorials/webrtc/datachannels/
Chrome now seems to have something: https://developer.chrome.com/apps/sockets_udp
It looks like UDP for web is still an active area of development and potential standards creation. Posting this answer to record some new info current as of May 2020.
The following whitepaper has outlined a potential path forward that satisfies the security needs for an "unreliable-unordered" protocol: https://gafferongames.com/post/why_cant_i_send_udp_packets_from_a_browser/
There are extensions to desktop Chrome and desktop Firefox that are in active development.
https://github.com/RedpointGames/netcode.io-browser
The way mobile browsers are designed prevents this kind of modification from being added at present (good security reasons again) but could be added down the road.
This is a major issue for gamers. See that link for a discussion of websockets, webrtc, quic (in chrome), and the author's netcode.io
You could alternatively create an additional python local server for bridging the data between your C++ application and webpage.
The html5 webpage connects to a local port that allows a web socket connection (use Flask/tornado).
The C++ application connects to a UDP listener on a different port. See https://wiki.python.org/moin/UdpCommunication to setup.
The python server basically forms a transparent data bridge between UDP port to websocket connection .
After reading all the links and comments, we can conclude:
NO, YOU CAN'T SEND THE UPD PACKAGE FROM THE BROWSER.
And you probably won't, because adding such a feature would be a giant leap backwards in web security.
You could possibly use a work around, design a program/script/server(I would use PHP, being a html client) to get the UDP gram from the server, if you would like I could help, I have worked on something similar.
I was just wondering if there was a way to send CoAP queries in the browser (using JavaScript?), but not using node.js.
Thanks!
Unfortunately this is not directly possible. Web browsers do not provide any JavaScript API for UDP sockets for security reasons.
If you need an in-browser app to communicate to a device via a CoAP interface, you may need to provide an intermediary service. For example, you could create a web service that is accessible via HTTP, and acts as a translator to CoAP.
Note that node.js does support UDP connections, and could be used to send CoAP messages. However, it is a server-side technology, and cannot be used in the browser. It could be used to build such a translator service; but would not be required.
Also, it appears that "Chrome Apps", which are based on HTML5 and JavaScript, but are distinctly different from web pages, do have access to UDP Sockets.
It is possible when using CoAP over TCP/IP or a CoAP-HTTP proxy, like Californium, jCoAP, SmartServiceProxy or Squid with CoAP-HTTP.
There is also a partial JavaScript implementation of CoAP - JSCoAP.
Is it possible to bind to a socket with in-browser javascript code? I need to open a local web server when a user visits a page to provide some localhost web publishing.
you cannot listen on a port with websocket so you cannot create a local web server within a browser.
Do websockets allow for p2p (browser to browser) communication?
But you can create javascript server with Nodejs. This is also javascript, but not in the browser. This is easy, fast and lightweight.
(i guess) javascript, silverlight, flash cant create in-browser server. they cant access to the sockets directly. the browser not allows. i think the websockets are forwarded sockets by the browser. also i dont really understand why you want to create this.
Modern browsers can use WebSockets, though it has it's own protocol. Otherwise, there are solutions out there that use a hidden Flash object to do TCP sockets. One of them is jsxmlsocket.
Are there websocket libraries (like ajax for jquery) I can use on the browsers with fallback to ajax long polling?
I found that Socket.IO implements a weird, arbitrary layer over the WebSocket protocol. I'd rather just pass raw data back and forth, so for my project, I went with web-socket-js.
web-socket-js works similarly to Socket.IO in that it reverts to using Flash sockets if WebSocket support is unavailable. Thus, it works in all the major browsers that support Flash (I tested this myself).
Just make sure you open port 843 in your firewall or you'll get a Flash security policy error. You'll probably need a script to listen on port 843 as well...I used em-websocket for the socket server, and in its readme I believe is a link to a Perl script that provides this.
Library with WebSockets support and fallback long polling solution would probably depend also on server side technology and not only browser client. Try to look at socket.io for example.
I have been using SignalR for the last several months, and it is awesome. It does what Socket.IO does, but maybe even better. It degrades as follows: Web Sockets, Server Sent Events, Forever Frame, AJAX long polling. Only works with .NET though.
https://github.com/SignalR/SignalR/wiki/Faq
Yes, you'll need server support for that as well. Kaazing WebSocket Gateway supports a very fast emulation/Polyfill. If you have a plugin like Flash it may use that (opportunistic optimization), but if you don't it emulates WebSocket with encrypted streaming, which is a lot more efficient than long polling. It comes with SSE and Cross Document Messaging support and emulation as well, as well as many higher level protocol abstractions (JMS/Stomp, XMPP, etc.)
Union Server has WebSocket support with fallback to comet-style AJAX communication. Union is a platform for creating connected applications, such as online multiplayer games.
http://www.unionplatform.com
To clarify when I ask about browser to browser communication I mean without a server in between forwarding message. I would like to implement something like this for a game. If p2p in websockets isn't possible are there similar alternatives? Any help is appreciated.
No. Browsers can only initiate WebSockets connections, not receive them. The W3C browser API spec only defines how to start an outbound connection.
You can make an application that would both initiate and accept WebSockets connections, but browsers do not do this.
You might look at Pusher App which you could use to build a WebSockets application with multiple clients. The service provides up to 20 simultaneous WebSockets clients for free (you can pay for higher scaling).
Update:
The WebRTC protocol and API is making rapid progress and allows a Data Channel to be established between two peers (you still may need a STUN/TURN server for the initial NAT traversal and setup).
In theory it is possible with WebRTC DataChannel:
RTCDataChannel is a WebRTC API for high performance, low latency,
peer-to-peer communication of arbritary data. The API is
simple—similar to WebSocket—but communication occurs directly between
browsers, so RTCDataChannel can be much faster than WebSocket even if
a relay (TURN) server is required (when 'hole punching' to cope with
firewalls and NATs fails).
"In theory" because it isn't supported by a stable browser yet and you still need a relay server (TURN) if one of the browsers is behind a symmetric NAT. Nevertheless, it is a really promising feature.
Update: Chrome 26 and Firefox 22 support RTCDataChannel by default and Firefox 19-21 if you enable WebRTC by setting media.peerconnection.enabled to true (about:config).
I was reading about websocket and peer 2 peer and found PeerJS.
I still haven't made anything though, but by the examples it looks promising.
Now days it's possible, currently only Chrome,FF and Opera support it (desktop).
There's some libraries starting to pop up around the web right now, such as PeerJS and js-platform-p2p which pretty much simplifies things.
Simple and reliable cross browser supported way is to use http://httprelay.io with AJAX calls. It is also implements one to many communication what could be useful for game development.