{"config":{"indexing":"full","lang":["en"],"min_search_length":3,"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"nim-libp2p documentation Welcome to the nim-libp2p documentation! Here, you'll find tutorials to help you started, as well as examples and the full reference .","title":"Introduction"},{"location":"#nim-libp2p-documentation","text":"Welcome to the nim-libp2p documentation! Here, you'll find tutorials to help you started, as well as examples and the full reference .","title":"nim-libp2p documentation"},{"location":"tutorial_1_connect/","text":"Simple ping tutorial Hi all, welcome to the first nim-libp2p tutorial! This tutorial is for everyone who is interested in building peer-to-peer applications. No Nim programming experience is needed. To give you a quick overview, Nim is the programming language we are using and nim-libp2p is the Nim implementation of libp2p , a modular library that enables the development of peer-to-peer network applications. Hope you'll find it helpful in your journey of learning. Happy coding! ;) Before you start The only prerequisite here is Nim , the programming language with a Python-like syntax and a performance similar to C. Detailed information can be found here . Install Nim via their official website . Check Nim's installation via nim --version and its package manager Nimble via nimble --version . You can now install the latest version of nim-libp2p : nimble install libp2p@#master A simple ping application We'll start by creating a simple application, which is starting two libp2p switch , and pinging each other using the Ping protocol. You can extract the code from this tutorial by running nim c -r tools/markdown_runner.nim examples/tutorial_1_connect.md in the libp2p folder! Let's create a part1.nim , and import our dependencies: import chronos import libp2p import libp2p / protocols / ping chronos the asynchronous framework used by nim-libp2p Next, we'll create an helper procedure to create our switches. A switch needs a bit of configuration, and it will be easier to do this configuration only once: proc createSwitch ( ma : MultiAddress , rng : ref HmacDrbgContext ): Switch = var switch = SwitchBuilder . new () . withRng ( rng ) # Give the application RNG . withAddress ( ma ) # Our local address(es) . withTcpTransport () # Use TCP as transport . withMplex () # Use Mplex as muxer . withNoise () # Use Noise as secure manager . build () return switch This will create a switch using Mplex as a multiplexer, Noise to secure the communication, and TCP as an underlying transport. You can of course tweak this, to use a different or multiple transport, or tweak the configuration of Mplex and Noise, but this is some sane defaults that we'll use going forward. Let's now start to create our main procedure: proc main () {. async , gcsafe .} = let rng = newRng () localAddress = MultiAddress . init ( \"/ip4/0.0.0.0/tcp/0\" ). tryGet () pingProtocol = Ping . new ( rng = rng ) We created some variables that we'll need for the rest of the application: the global rng instance, our localAddress , and an instance of the Ping protocol. The address is in the MultiAddress format. The port 0 means \"take any port available\".tryGetisprocedurewhichispartofnim-result,thatwillthrowanexceptionifthesuppliedMultiAddressisinvalid.Wecannowcreateourtwoswitches:letswitch1=createSwitch(localAddress,rng)switch2=createSwitch(localAddress,rng)switch1.mount(pingProtocol)awaitswitch1.start()awaitswitch2.start()We'vemountedthepingProtocolonourfirstswitch.Thismeansthatthefirstswitchwillactuallylistenforanypingrequestscomingin,andhandlethemaccordingly.Nowthatwe'vestartedthenodes,theyarelisteningforincomingpeers.Wecanfindoutwhichportwasattributed,andtheresultinglocaladdresses,byusingswitch1.peerInfo.addrs.We'lldialthefirstswitchfromthesecondone,byspecifyingit'sPeerID,it'sMultiAddressandthePingprotocolcodec:letconn=awaitswitch2.dial(switch1.peer