Getting Started with LiteP2P

This guide will help you integrate LiteP2P into your application in just a few minutes. By the end of this guide, you'll have a working P2P connection.

Prerequisites

This guide assumes you have a basic Android or iOS project set up. If you're starting from scratch, check our Installation Guide.

Step 1: Install the SDK

Add LiteP2P to your project using your preferred package manager.

Android (Gradle)

// Add to your app's build.gradle
dependencies {
    implementation 'io.litep2p:litep2p-android:1.0.0'
}

iOS (CocoaPods)

# Add to your Podfile
pod 'LiteP2P', '~> 1.0.0'

iOS (Swift Package Manager)

// Add to Package.swift
dependencies: [
    .package(url: "https://github.com/litep2p/litep2p-ios.git", from: "1.0.0")
]

Step 2: Initialize LiteP2P

Initialize the LiteP2P engine with your application configuration.

Android (Kotlin)

import io.litep2p.LiteP2P
import io.litep2p.PeerConfig

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        val config = PeerConfig.Builder()
            .setAppId("your-app-id")
            .enableEncryption(true)
            .setNetworkMode(NetworkMode.AUTO)
            .build()

        LiteP2P.initialize(this, config)
    }
}

iOS (Swift)

import LiteP2P

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let config = PeerConfig.Builder()
            .setAppId("your-app-id")
            .enableEncryption(true)
            .setNetworkMode(.auto)
            .build()

        LiteP2P.initialize(config: config)

        return true
    }
}

Step 3: Connect to the Network

Connect to the LiteP2P network and start communicating with peers.

Android (Kotlin)

val p2p = LiteP2P.getInstance()

// Connect to the network
p2p.connect { result ->
    when (result) {
        is ConnectionResult.Success -> {
            Log.d("LiteP2P", "Connected! Peer ID: ${result.peerId}")

            // You're now connected to millions of peers!
            discoverPeers()
        }
        is ConnectionResult.Error -> {
            Log.e("LiteP2P", "Connection failed: ${result.message}")
        }
    }
}

// Discover nearby peers
private fun discoverPeers() {
    p2p.discoverPeers { peers ->
        peers.forEach { peer ->
            Log.d("LiteP2P", "Found peer: ${peer.id}")
        }
    }
}

Step 4: Send and Receive Messages

Exchange data with connected peers using the messaging API.

// Send a message to a peer
p2p.sendMessage(peerId, "Hello from LiteP2P!".toByteArray()) { result ->
    when (result) {
        is SendResult.Success -> Log.d("LiteP2P", "Message sent!")
        is SendResult.Error -> Log.e("LiteP2P", "Send failed: ${result.message}")
    }
}

// Listen for incoming messages
p2p.onMessage { message ->
    val content = String(message.data)
    Log.d("LiteP2P", "Received from ${message.peerId}: $content")
}

Step 5: Clean Up

Properly disconnect when your app is closing or going to background.

override fun onDestroy() {
    super.onDestroy()
    LiteP2P.getInstance().disconnect()
}
You're Connected!

Congratulations! You've successfully integrated LiteP2P into your application. Continue reading to learn about advanced features like file transfers, battery optimization, and more.

Next Steps