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.
-
Yes, but no browser has a working implementation, and I believe it's now part of the [stream API.](http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#peer-to-peer-connections) – antimatter15 Nov 07 '10 at 15:38
-
5The W3C now drafts the [WebRTC](http://www.w3.org/TR/webrtc/) spec talking about Peer-to-Peer connections amongst others things. – Pierre de LESPINAY Dec 14 '11 at 08:52
5 Answers
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).

- 49,297
- 16
- 112
- 153

- 70,845
- 23
- 144
- 140
-
6+1. Though I wouldn't recommend third party apps. Much easier to build your own. – Josh K Nov 08 '10 at 17:23
-
Pusher now provides 20 simultaneous connections and 100.000 messages per day for free :) – Tieme Oct 09 '12 at 16:31
-
I think you should update your answer : https://labs.ericsson.com/developer-community/blog/beyond-html5-peer-peer-conversational-video – Mehdi Karamosly Aug 30 '13 at 22:51
-
@MehdiKaramosly, that ericsson blog post is fairly old but is talking about the technology that eventually became WebRTC so I've added an update to reflect that. – kanaka Sep 03 '13 at 14:45
-
2@kanaka that means you still have to go through the server the first time, in order to make a peer 2 peer connection ? – Mehdi Karamosly Sep 03 '13 at 14:58
-
1@MehdiKaramosly, it's theoretically possible (http://blog.printf.net/articles/2013/05/17/webrtc-without-a-signaling-server/) but it's a very uncommon use case since just about everybody is behind a NAT firewall and you will still need some way to setup the signalling (communicate the connection details). – kanaka Sep 03 '13 at 15:19
-
Years later, the internet will grind to a halt as one of the links change: http://www.ericsson.com/research-blog/context-aware-communication/beyond-html5-peer-peer-conversational-video/ – L0j1k Mar 18 '15 at 01:12
-
As of 2017, WebRTC is a standard part of most modern browsers. See https://stackoverflow.com/a/45765379/3672465 – Beejor Aug 18 '17 at 21:47
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).
-
1[Today it's supported by most major browsers except Edge.](https://caniuse.com/#feat=rtcpeerconnection) – Donald Duck May 12 '19 at 16:16
-
2
I was reading about websocket and peer 2 peer and found PeerJS.
I still haven't made anything though, but by the examples it looks promising.

- 5,952
- 3
- 43
- 62

- 364
- 6
- 15
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.

- 15,752
- 26
- 89
- 142
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.

- 4,683
- 4
- 45
- 81