Swizec Teller - a geek with a hatswizec.com

    Towards a WebRTC mesh network with RTCDataChannel

    This is a Livecoding Recap – an almost-weekly post about interesting things discovered while livecoding. Usually shorter than 500 words. Often with pictures. Livecoding happens almost every Sunday at 2pm PDT on multiple channels. You should subscribe to My Youtube channel to catch me live.

    It didn't work. We don't have a mesh network yet. But we're so close that I can almost smell it.

    The ultimate goal is to build a JavaScript library that can connect arbitrary browsers through a mesh network using a sort of blockchain as the algorithm to ensure distributed data correctness. Why? Intellectual curiosity.

    This slide from my WeAreDevelopers talk 👇

    Previously we've figured out how to connect two browsers running on different computers with a video connection. Signaling server helps with the initial handshake, then browsers communicate directly using WebRTC. No more server.

    WebRTC also supports so called data channels using RTCDataChannel.

    It works like this:

    1. Use signaling server to establish a peer connection
    2. Add a data channel to said connection
    3. Send from each side

    Data can be anything. We're using "Hello World" strings for now, serialized JSON objects later. Rumors say you could send whole files.

    This all sounds very simple. Even when you look at the code that's required.

    We add a RTCDataChannel to our peer connection and attach a bunch of event handlers. Errors for complaining, messages for printing, and we try to send a message when the channel opens.

    The cool part is that we can attach this channel before a connection is fully established.

    See, both clients finish with saying Data channel open.

    But no data is sent or received, and there's no error. 🧐

    I suspect this is because while the data channel is open, the peer connection itself is not established. That pesky WebRTC lifecycle error about doing some thing or another in the wrong step.

    This part used to work. Therefore, I must have broken something when deleting a bunch of code that seemed unnecessary.

    We also generally spent a lot of time mucking around making this lifecycle more robust because it was hella flaky before.

    Getting close!

    Did you enjoy this article?

    Published on May 30th, 2018 in Front End, Technical

    Learned something new?
    Want to become an expert?

    Here's how it works 👇

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

    ~ Ashish Kumar

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

    Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created by Swizec with ❤️