How is it possible to send UDP packets to browser? - javascript

I know there are already similar questions in forum, but I didn't really find a direct answer for my question there.
What I'm trying to do:
I have a pub/sub middleware that uses UDP multicast to send data to other hosts and I want to be able to visualise that data with JavaScript im Browser. Best case scenario for me would be to receive the UDP packets directly in JavaScript (like just "rewriting my subscriber code in JS"), but I understand this is not possible for security reasons. So what other way is there to somehow get to these UDP packets from the browser?
I read that, under specific conditions, it's possible to communicate with the browser over UDP using WebRTC, but I don't understand what these conditions are. If this would be your suggestion, it would be very nice if you could explain that. I'm kinda new to the whole real-time-data-transfer-to-browser topic.
Thank you in advance! :)

WebRTC provides Datachannels, this allows a browser to send/receieve datagrams. These datagrams will be carried over UDP, but also use SCTP and DTLS.
To get your packets into the browser you will need to write a UDP -> WebRTC bridge. This will not run in the browser, but the browser will connect to it. You have lots of choices when writing this bridge Python, C/C++, Go, node.js, Rust and more.
Since you are writing a bridge you could also use Websockets or even HTTP poll. But today there is no way to directly get UDP into the browser. A Raw Sockets API was proposed, but AFAIK is not going to happen.

Not for raw-UDP, but for coap(s), there are http-coap-cross-proxies, which may help.
Request out:
Browser -- HTTP -> http2coap-cross-proxy -- coap -> coap-server
Response back:
Browser <- HTTP -- http2coap-cross-proxy <- coap -- coap-server
Anyway, that keeps the request/response scheme, so I'm not sure, if that matches your requirements/expectations.

Related

From javascript browser client-side to Postgresql [duplicate]

I have a c# tcp server, I want to connect to the server via a html client page.
The problem: There is no simple way to create TCP sockets in Javascript on a browser side. Although solutions like Websockets allow to create something that resemble sockets, you can use them to connect only to servers that support Websockets. Not to any random servers that know nothing about HTTP.
so is there a solution to connect to my srver.
No. There just isn't. The browser is a tightly locked down environment. The only socket connection that you can open from JavaScript is WebSocket. Since it's your server, adding WebSocket support shouldn't be too complicated, and there are WebSocket libraries available for C#.
Maybe someone else will have an idea for you, but...
The best solution I can think of is for your server to support websockets.
The situation you described - along with connectivity issues for traffic passing through proxies and routers - is one of the reasons Websockets were introduced in the first place.
Bare in mind that Websockets can send and receive binary data. It's just that javascript make it more comfortable to write text based messages.
Also, many NAT routers, Proxies and firewalls will block raw TCP/IP communication while allowing Http communication to pass through. This is why you have a better chance at connection establishment and retention when implementing the Websocket protocol.

Is it possible to send a raw UDP packet directly from the browser to an ip/port?

I've been told WebRTC, in particular data channels, allow one to use UDP on the browser. From looking at some documentation, though, I wasn't able to determine how to do the simple task of sending a raw UDP packet to a specific IP/port (possibly of a server that isn't aware of web at all).
Is it possible, and if so, how?
Not with WebRTC, no. The closest you can do is create a data channel, which uses SCTP over UDP (assuming the endpoints are able to be connect over UDP directly).
One reason WebRTC went with SCTP data channels is because there's a need for congestion control, to prevent applications from generating too much UDP traffic and working badly in parallel with TCP, for instance.
Perhaps not directly through a web app, but you can build a Chrome app/extension to do so.
See examples:
https://developer.chrome.com/apps/app_network
https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/udp.

html javascript connect to raw socket

I have a c# tcp server, I want to connect to the server via a html client page.
The problem: There is no simple way to create TCP sockets in Javascript on a browser side. Although solutions like Websockets allow to create something that resemble sockets, you can use them to connect only to servers that support Websockets. Not to any random servers that know nothing about HTTP.
so is there a solution to connect to my srver.
No. There just isn't. The browser is a tightly locked down environment. The only socket connection that you can open from JavaScript is WebSocket. Since it's your server, adding WebSocket support shouldn't be too complicated, and there are WebSocket libraries available for C#.
Maybe someone else will have an idea for you, but...
The best solution I can think of is for your server to support websockets.
The situation you described - along with connectivity issues for traffic passing through proxies and routers - is one of the reasons Websockets were introduced in the first place.
Bare in mind that Websockets can send and receive binary data. It's just that javascript make it more comfortable to write text based messages.
Also, many NAT routers, Proxies and firewalls will block raw TCP/IP communication while allowing Http communication to pass through. This is why you have a better chance at connection establishment and retention when implementing the Websocket protocol.

Multicast send from JavaScript?

I have a situation where I need to get some information from an application written in JavaScript, to another computer over the network, but neither system knows the ip address of the other. Multicast really seems like the right answer to me. This communication would require small and frequent update messages, perfect for a light multicast stream.
The problem is that I don't think JavaScript can send UDP messages and I don't think JavaScript can trigger external applications to send the messages on its behalf.
How can I solve this problem?

JavaScript WebSockets with UDP?

I'm writing a JavaScript Application that has to receive a huge amount of data from other users. It is not important if some of this data gets lost. Is there some way of using JavaScript WebSockets with UDP instead of TCP?
It sounds like what you're waiting for is WebRTC which is working it's way through the standards process. WebSockets, as other people have pointed out, run over TCP as a result of initiating with an HTTP Upgrade.
No, it's not possible to have UDP communication within JavaScript. Sorry.
Sounds like the question is meant for client-side UDP, but since I ended up here...
You can do UDP in JavaScript on the server using the node.js dgram package.
The WebSockets protocol is over TCP only as currently defined.
You could do UDP with Flash if you are willing to use a RTMFP (Real Time Messaging Flow Protocol) server.
If this question is still pending:
I found a project called JNEXT and there is an example using UDP.
The project itself seems to be paused but at least in Firefox it works (it doesn't work with Chrome and Opera).
May be it is worth to look for it.
I think you can leverage Silverliht 4 technology. You can create a Silverlight 4 application to communicate with server and then enbamdded it to html page. Then your JavaScript can build TCP connections via Silverlight 4 application!
You could write a WebSocket server to serve as proxy/bridge between TCP/UDP.

Categories

Resources