Search code examples
javascriptclient-sidestatic-site

Communicate with devices on local network using client-side Javascript


I'm trying to build a website that allows you to transfer files to devices connected on your local network. This website is static, and will be hosted on GitHub pages.

Is it possible to use Javascript to communicate with (i.e. transfer files/text) other devices on the local network? The IP addresses of the devices are already known and I'm looking for a peer-to-peer connection here.

Note: As this website is static, there is no server side code that can be controlled.

Thanks


Solution

  • Yes, it's possible with caveats, depending on the specifics of your situation.

    WebRTC

    With WebRTC, you can establish a real peer-to-peer connection between two clients on a network. They can send data (binary or strings), and media streams (like webcams and microphones). This can even work from a static page.

    The catch is that you need some sort of server to help coordinate the connection. Because of the way the WebRTC standard was originally set up, there is some back-and-forth that must occur to set up that peer-to-peer connection. Some method of communicating the signalling between the clients must exist, and this is usually done via web sockets and an intermediary server.

    There are some novel alternatives.

    In the future, ORTC may solve this issue, allowing a one-shot method for setting up the call, making the server-side requirements easier.

    Embedded HTTP Server

    You didn't elaborate on the specifics of what device you want to communicate with on your network, so maybe this is a possibility as well. There's nothing stopping your browser from communicating with devices on your LAN. Your static web page can use the Fetch API or AJAX to retrieve data from devices.