r/HTML • u/Public_Tailor_1276 • Sep 16 '26
Question Is it possible to communicate between two devices when they both have the same HTML file open?
I'm a somewhat beginner web programmer, and I am trying to make a simple game using HTML and JavaScript. I'm wondering if LAN communication is possible to implement, since I don't have the money, equipment, or experience to host a server to route traffic through.
Edit: I am also using JavaScript in the HTML file, as mentioned above.
Edit 2: I accept that this isn't possible.
4
Upvotes
11
u/Neat_Ad_5577 Sep 16 '26
What research have you done so far and what did it say?
3
u/TheChilliPL Sep 16 '26
I believe the only way to communicate between two web clients peer-to-peer (without a server in the middle) is WebRTC.
Now, I haven't used it myself, but seeing the docs and Google results, for fully remote direct communication, you'd have to pass existing servers that would find a way for the two devices to communicate (either directly or through a relay), but the docs do note that:
which seems to mean that communication within LAN would be possible without that.
https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API
5
u/ziggittaflamdigga Sep 17 '26
You can host your own server on LAN if you choose to look in to it eventually
3
u/junvar0 Sep 17 '26
No, it's not possible if you only have 2 html pages running in e.g. 2 tabs on different or even the same devices.
HTML pages are only allowed to send requests, not listen for requests. I.e. they're clients. Imagine if you have 2 phones that can make calls, but not receive calls. They can't call each other. They'd have to call a 3rd 'middle man' phone that is allowed to receive calls and can act as a bridge between the 2.
That receiver would be called a server. It can run on a dedicated machine. E.g. you can set up 1 server on the cloud or in your basement, and then every person who opens the HTML page in their browser could communicate with each other via that 1 central server.
Or alternatively, you can have server run directly on the user's computer, but then it would have to be a standalone application (e.g. .exe or node.js or something) and can't be ran in the browser. That's basically what peer-to-peer game servers are.
2
u/NibbaMoisterlol Sep 17 '26 edited Sep 17 '26
I think the easiest would be to make a Web Socket server.
Since you're also trying to learn JavaScript, I'd recommend that you write the server in JavaScript as well! It's well documented and fairly easy to do.
You will need a runtime to execute JavaScript locally without a browser. I recommend Bun, but you have other alternatives such as Node.js or Deno. Bun has great documentation on how to start with a server: https://bun.com/docs/runtime/http/websockets
Get a server running, figure out how to connect to the server via your frontend JavaScript that's linked to the HTML files, and how you can send data, and then work on the server backend to handle users and other data you want to send between the clients. Mozilla usually has great documentation on JavaScript stuff, here's on how to connect to your server: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications
This does not cost you anything, everything is local, no port-forwarding etc. It is as simple as it can get!
2
u/Similar_Donkey_7286 Sep 17 '26
so assume you send a packet to the other computer (how do they know each other? let's assume you told them each other's ip) , why would the other computer accept said packet? because you opened a html file? that would be a security nightmare and luckily not how it works.
you need an intermediary. something that accepts packets and can act as intermediary. easiest is to store the packets on said intermediary and each html file asks if there's something new.
only do this on your own network but it's a fun exercise.
1
u/HopeOfTheChicken Sep 16 '26
It's definitely possible to do a multiplayer game without a dedicated server. You should be able to find a lot of ressources online on how to do it. Be warned though that multiplayer adds a lot of complexity, so dont feel let down if you experience some difficulties. That's completely normal
1
u/sheriffderek Sep 16 '26
The important thing to know here - is that once the HTML program is read - it’s discarded. It gets transformed into the live dom structure in the users browser. So, if you’re stuck thinking want to play a game - then you’d need to open up connections in some way. You could have a primary sever - that accepts input and updates both players via their browser - or websockets etc.. This is what real-time databases like Firebase were built for. You can use that for free while you learn.
1
u/DirtAndGrass Sep 16 '26
Not at all with HTML, you'll need JavaScript, You'll also need some way to scaffold the connection, you can do purely peer to peer connection with webrtc but you need a way to set up the connection.
You could create and run such a server fairly easily with node, on your local machine, and have other machines on your local network connect to it.
1
u/OppieT Sep 17 '26
Why not use wamp which is free?
1
u/Super_Preference_733 Sep 17 '26
You know there are a number of free hosting providers for low bandwidth stuff.
0
u/infinitenothing Sep 17 '26
Running a server might not be as hard as you're imagining. Welcome to Flask — Flask Documentation (3.1.x)
-2
u/gastapo67 Sep 16 '26
Docker is your friend