7

I wanted to be allow users to play p2p in a multiplayer game that I'm developing, but to be able to do that, javascript needs to be able to create a socket server in the browser. Is that even possible? I don't know of any API that let clients connect to other clients in javascript. Is there any other way? Like using a hidden flash element?

I am asking for something that doesn't require a server at all. The packets need to travel from client to client directly

Tim
  • 41,901
  • 18
  • 127
  • 145
  • Take a look at [websockets](http://dev.w3.org/html5/websockets/) ([tutorial](http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/)). You'll need a socket server anyway, to centralize communications between clients. Also: browser support is still limited. – bfavaretto Apr 22 '12 at 23:44
  • Yeah, but I need p2p, specifically without a centralized server. –  Apr 22 '12 at 23:47
  • Well, that's just not possible. – bfavaretto Apr 22 '12 at 23:59

7 Answers7

6

In short no, p2p in a browser is not possible.

The closest you can get is using NodeJS (for potentially p2p JS) or a centralised server (or several servers) and websockets (for sockets in a browser)

tobyodavies
  • 27,347
  • 5
  • 42
  • 57
6

This question is old, but I can now give an answer: YES, there is finally a way to do p2p communication between browsers! Thanks to the new standard WebRTC, modern browsers got support for Data Channels, something much more powerful than WebSockets.

Take a look here:

WebRTC Data Channels

Online Example: Banana Bread 3D is a First Person Shooter game compiled to JS+WebGL, using WebRTC data channels in multiplayer mode:

BananaBread 3D Multiplayer online fps game

Emanuele Sabetta
  • 1,571
  • 12
  • 34
4

Interesting question, but probably a duplicate:

i know for sure this can not be done using only javascript(in every browser). According to another answer on Stackoverflow in above topic you might be able do this using rtmfp-api.

This project expose Rtmfp protocol (provided by Flash version 10) to javascript application throught a hidden flash applet. The protocol allow multiple clients to communicate directly. See the references for more details about the protocol.

Looking quickly at the site you still need a rtmfpUrl-server in the middle, which i totally understand because the clients need to be be able to find each other(IPs). But I assume after that it will be p2p. Doing a quick search I also found open-source rtmfp-server(s).

I haven't tried this out myself, but I maybe this will help you achieve your goal.

Some other links:

Community
  • 1
  • 1
Alfred
  • 60,935
  • 33
  • 147
  • 186
0

While this is a shopping question, i'd look into APE

http://www.ape-project.org/

At the very least you could check out how they've structured it.

FlavorScape
  • 13,301
  • 12
  • 75
  • 117
0

In order to implement such a game, your JavaScript client must communicate with the server. The server then runs the game logic, and sends the result back to the client.

  • JavaScript receives user input and sends it to the server
  • Server ensures that the input is valid (to prevent cheating) and updates the game with the new input
  • Server periodically sends the game state to JavaScript (either by long polling or by having JS request it at an interval).

Basically, never trust anything coming from JavaScript as it is extremely easy to modify. Everything should be done server-side.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • You didn't answer my question at all. I need peer-to-peer, simple as that. I've developed enough centralized multiplayer games (including a RTS and a MMORPG) to know what you posted already. You barely read my question. –  Apr 22 '12 at 23:48
  • "your JavaScript client **must** communicate with the server". Meaning it cannot do p2p. At all. Imagine if it could, everything you do is suddenly at risk of being communicated to someone else... – Niet the Dark Absol Apr 22 '12 at 23:58
0

Here's a solution with mobl (but I haven't tried it yet).

http://zef.me/3391/moving-the-server-to-the-browser

ndrizza
  • 3,285
  • 6
  • 27
  • 41
0

It is possible to go serverless with Flash. This is doable with Adobe Flash's Peer to Peer capabilities. I once wrote a peer to peer chat with it. The drawback is Actionscript is a dying language and may not be supported much in the future.

Here is the raw class. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetGroup.html

Here is resources if you don't want to write your own. http://www.as3gamegears.com/category/multiplayer/

If you want a Server option that is light on the server side. Try this node.js extension.
http://socket.io/

I recommend using a java socket server of some sort. Electroserver used to be one of the leaders in the field, it had Unity support and was scalable to hundreds of thousands. Although I think they have fallen on hard times. The Electroserver site has not been accessible for sometime. I know there are others out there but Electroserver is the only one I have used.

Mardok
  • 1,360
  • 10
  • 14