Make a P2P connection in 10 minutes

carloslfu profile image Carlos Galarza Updated on ・2 min read

Distributed Network

This is a short guide on how to make a P2P chat. My aim is to give you only a taste if you like I could write a deeper article explaining how all these things work. That said, let's start.


The only thing you will need is the latest NodeJS and your favorite editor, mine is Visual Studio Code. Now create a folder for our project, open a command line, initialize an npm repo with npm init and install some libraries we will need with:

npm i --save discovery-swarm dat-swarm-defaults portfinder get-port

Our first P2P connection

Let's start by doing a simple P2P connection, thanks to some great libraries from the Node.js community it is too simple:

Run this code in two consoles and this is it!. Now you can run it in other device right in your LAN and should work even without internet, also you can share and run this script in any other computer or device with internet around the world and it just works, so nice!

How it works?

This code uses the technology behind the Dat Project. All communications are decentralized and work over a P2P network.


I love feedback. Ask me questions, comment on your experience with P2P technologies and share your ideas! Thanks for reading :)

This series is possible thanks to the awesome tools delivered by the DatProject, Webtorrent and the NodeJS community.

Follow me on here and on Twitter if you want to know more about tech and distributed P2P networks: https://twitter.com/carloslfu

Posted on by:

carloslfu profile

Carlos Galarza


I like Software Engineering, and enjoy creating useful tools for helping people to be more productive. "The best way to predict the future is to invent it." - Alan Kay


markdown guide

hi Carlos Galarza this is great as i expected. but i have small problem here. when i run this script on two different computers this will not works as expected. can you plz help me.


Of course, I will email you.


Could you share this concept for me as well, I would like to use your solution between some valid computers IP on the internet or a bigger network.

I this code doesn't work for you I recommend you to use dev.to/carloslfu/comment/68ih. You don't need to put the computers IP in the code. Computers are connected with each other using the channel name:

 * The channel we are connecting to.
 * Peers should discover other peers in this channel

Hope it helps

Thanks for replying me Carlos.


Hi Carlos,

I am trying to use Peer to Peer Connection to send live stream Kinect joint data from one computer to another. Both computers are connected to the same server. When I run the program I am able to view the live data that is coming from the Kinect and I can see my Peer ID. I wanted to know if you can guide me on how to tranfer that data to another computer.

Picture: thepracticaldev.s3.amazonaws.com/i...



Of course, Rinay, email me with more details or some code I can find a moment to help you. Sorry for the late response I last month I was busy.


Great Article! Very brief and got right to the point, loved it. How would you go about creating a P2P connection on remote (non-local) devices?


Hi Jake, thanks! If you run the script in two remote computer with internet it works. I have updated the article for clarifying it. I am glad you liked the article :)


Not work, any example of how peer_id: TCP_Connection works?


If this implementation sometimes doesn't work is because of some issues with the discovery-swarm library. Fortunately, Hyperswarm was released recently. It implements a more reliable way of doing peer discovery. So, if you are experimenting issues try to use it now, this article pfrazee.hashbase.io/blog/hyperswarm about Hyperswarm includes a code example and here is the GitHub repo with some docs: github.com/hyperswarm/discovery.


Hi Carlos! I have a problem. after I start the 2 consoles, both say that they connected, but I can't write any messages. Please help me!


Hi Carlos I want build p2p connection .which package are suitable? Tell me.

Sloan, the sloth mascot Comment marked as low quality/non-constructive by the community View code of conduct

Great Article Carlos! Hello, I would like to know how to create a private node, I noticed that it connects to a public node ... This is for a decentralized private information sharing project. Thank you in advance.