Do websockets allow for p2p (browser to browser) communication?

JavascriptAjaxWebsocketP2p

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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).

Solution 2 - Javascript

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).

Solution 3 - Javascript

I was reading about websocket and peer 2 peer and found [PeerJS][1].

I still haven't made anything though, but by the examples it looks promising.

[1]: http://peerjs.com/ "peerJS"

Solution 4 - Javascript

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.

Solution 5 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXavierView Question on Stackoverflow
Solution 1 - JavascriptkanakaView Answer on Stackoverflow
Solution 2 - JavascriptJoelView Answer on Stackoverflow
Solution 3 - JavascriptshadownrunView Answer on Stackoverflow
Solution 4 - Javascripteric.itzhakView Answer on Stackoverflow
Solution 5 - JavascriptJonasView Answer on Stackoverflow