WebRTC for client-server communication

338 views
Skip to first unread message

Andreas Paulus

unread,
Aug 11, 2015, 10:54:12 AM8/11/15
to Dart Misc
Hi,

I want to make a realtime multiplayer game using WebRTC. I do not want to go peer2peer but client-server and wanted to use dart for this.
Unfortunatelly the server console application cannot use dart:html and thus cannot access the RtcPeerConnection class. I know it is implemented in the browser so it is not possible to port it over to dart:io and it will never happen (https://github.com/dart-lang/sdk/issues/18426).

I could try to work around this using a web-based dart app running in dartium which forwards all data received from WebRTC clients to my dart console application via websocket but I don't like this approach because
- I don't think there is a headless mode so I cannot deploy this easily
- I don't think is scales well because of the single threaded context in which the RtcPeerConnection lives inside dartium.

There must be a better way.

I am really hesitant to use native libraries (http://www.webrtc.org/native-code/development) because it sounds like a lot of work to integrate it into my dart command line app.


Any advice?

Günter Zöchbauer

unread,
Aug 11, 2015, 11:13:49 AM8/11/15
to Dart Misc
You might take a look at https://pub.dartlang.org/packages/webrtc_utils
I don't know WebRTC but I know the author of the package dived in quite deep.
You can try to contact him if the code of the package doesn't answer your questions.

Andreas Paulus

unread,
Aug 11, 2015, 11:17:49 AM8/11/15
to Dart Misc
After some more research I found that content_shell could allow me to create a RtcPeerConnection which gets connected to every client via a signalling server. I guess I could even use the signalling server as a kind of load balancer and have multiple RtcPeerConnections on different ports of the same machine or even machines in the same LAN to increase the throughput and utilize additional CPU cores?
The data received from the RtcPeerConnections inside those content_shell instances would then be forwarded to my main game server (a console dart app) via websocket and vice versa.

I am quite curious if I am missing something here because I could avoid having to use the native WebRTC implementations this way and stay completely in dart code ;)
Any input is appreciated!

Andreas Paulus

unread,
Aug 11, 2015, 11:21:47 AM8/11/15
to Dart Misc
Thanks for the input :)
Unfortunately this library is based on dart:html which cannot be used on dart console apps. See the comment from Robert Hartung over here: https://groups.google.com/a/dartlang.org/forum/#!msg/misc/7imhJDbuCmc/uqNDU-jBwOoJ

Natalie Weizenbaum

unread,
Aug 11, 2015, 2:12:27 PM8/11/15
to General Dart Discussion

Why not just implement WebRTC yourself on top of dart:io's socket infrastructure? The original bug was closed with that recommendation.

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Andreas Paulus

unread,
Aug 12, 2015, 11:21:38 AM8/12/15
to Dart Misc
I guess I have to try something like this. However the C++ implementation is quite intimidating: https://chromium.googlesource.com/external/webrtc/+/master/talk/app/webrtc
Maybe I can just implement a minimum piece of the spec (http://w3c.github.io/webrtc-pc/) which is needed for establishing a DataChannel because the whole thing looks like a lot of work, too.

I'll give it a try on the weekend... any tips on where I should start or reference implementations are welcome ;)

Btw I don't think the content_shell based approach would work (content_shell seems to be unstable, it crashes on startup, at least on my windows 8.1x64 machine) and I also don't think I can somehow use the native implementations. Implementing it on top of dart:io seems to be the only way.

Andreas Reiter

unread,
Aug 17, 2015, 8:45:20 AM8/17/15
to mi...@dartlang.org

I had the exact same problem, and looked into implementing WebRTC on top
of dart:io, but this seems to be A LOT of work with all the associated
ICE, STUN, TURN and so on protocols.

My (nasty) workaround uses a nodejs instance running on the same machine
which exposes the WebRTC API (based on node-wrtc) over TCP.
Dart connects to the exposed TCP port and provides a wrapper.
You can seamlessly switch between browser and nodejs implementation, and
use it the same way as the browser implementation.

Find it at: https://github.com/areiter/nodejs-dart-webrtc-wrapper

Best regards,
Andreas
> --
> For other discussions, see https://groups.google.com/a/dartlang.org/
>
> For HOWTO questions, visit http://stackoverflow.com/tags/dart
>
> To file a bug report or feature request, go to http://www.dartbug.com/new
>
> To unsubscribe from this group and stop receiving emails from it, send
> an email to misc+uns...@dartlang.org
> <mailto:misc+uns...@dartlang.org>.



Andreas Paulus

unread,
Aug 18, 2015, 12:36:51 PM8/18/15
to Dart Misc
Thank you very much, Andreas :)

You are right, implementing all of those protocols is a lot of work. The only thing I may have tried was to implement a native wrapper around http://opentools.homeip.net/webrtc but I'm glad you came up with this node wrapper.
I think it's not so bad:
- latency increase should be less than 2ms when running on the same machine
- nodejs is single threaded but very efficient with async io so I guess this node wrapper should at least be able to deal with a few hundred clients with every client sending and receiving around 10 small messages per second via data channel. I got to do some load test to prove that ;)

Again, thanks for sharing your wrapper!

Cheers,
Andreas
Reply all
Reply to author
Forward
0 new messages