Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    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!

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

    Did you enjoy this article?

    Continue reading about Towards a WebRTC mesh network with RTCDataChannel

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

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

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    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 ❤️