Demos Network Specifications
  • Introduction
    • What is Demos Network
    • Demos Network Architecture
  • FAQ
  • Cookbook
    • Project setup
      • Run the project (MacOS)
      • Run the project (Windows)
        • WSL 2 Setup on Windows (10 and 11 only)
        • Issue Troubleshooting
      • Run the project (Ubuntu)
  • SDK
    • Getting Started
    • WebSDK
      • Authentication
        • FIDO2 Passkeys
          • Under the Hood: FIDO2 Passkeys
      • NodeCalls
      • Transactions
        • Creating a transaction
        • Signing a transaction
        • Broadcasting a transaction
      • L2PS SDK
        • The l2ps module
        • Interacting with the L2PS
        • L2PS Messaging System
      • Instant Messaging
        • What is the Instant Messaging Protocol?
        • Architecture Overview
        • Encryption
        • Quickstart
        • Message Types
        • API Reference
        • FAQ
    • Cross Chain
      • General layout of the XM SDKs
      • EVM
      • BTC
      • Solana
      • MultiversX (EGLD)
      • NEAR
      • IBC
      • TON
      • XRPL
      • The XMScript
      • Identities
    • Demoswork
    • Cookbook
      • Demoswork
        • Creating work steps
        • Conditional Operation
        • Base Operation
        • Signing and broadcasting
      • Transactions
        • Crosschain Transaction
        • Native Transactions
      • SWAP
        • Crosschain SWAP
    • Web2
      • Quick Start
      • DAHR API Reference
        • Types
      • Making Requests
      • Identities
        • Twitter
        • GitHub
    • API Reference
    • Bridges
      • Rubic Bridge Test
    • Post Quantum Cryptography
  • Backend
    • Internal Mechanisms
      • Network Time Synchronization
      • Cross Context Identities
    • Global Change Registry
      • GCR Structure
      • How is GCR Synced?
    • Consensus Mechanism
      • Unparalleled Scalability
      • Decentralization in PoR-BFT
      • Enhanced Security
      • Comparative Advantage
      • Addressing Potential Criticisms
      • Conclusion
    • Communications Stack
    • L2PS (Subnet) Framework
      • How are L2PS transactions handled?
    • Miscellaneous
      • Browsing the Postgres DB via psql
    • Bridges
      • Rubic Bridge
    • Developers Testbed
      • Setting up the environment
      • Setting up the repository
      • Installing dependencies
      • Node Configuration
      • Running the node
  • Frontend
    • Demos Providers Discovery Mechanism
Powered by GitBook
On this page
  1. SDK
  2. WebSDK
  3. Instant Messaging

Quickstart

Quick Start Example

import { instantMessaging } from "@kynesyslabs/demosdk"
import { encryption } from "@kynesyslabs/demosdk";

const unifiedCrypto = encryption.ucrypto

async function setupMessenger() {
    // Generate identities
    /* NOTE: This part is not necessary when using a SDK instance 
    that has already generated identities */
    const masterSeed = crypto.randomBytes(128)
    await unifiedCrypto.generateAllIdentities(masterSeed)
    const mlKemAes = await unifiedCrypto.getIdentity("ml-kem-aes")
    
    // Create and connect peer
    const peer = new instantMessaging.MessagingPeer({
        serverUrl: "ws://your-signaling-server:3005",
        clientId: "user-" + Date.now(),
        publicKey: mlKemAes.publicKey,
    })
    
    await peer.connect()
    
    // Set up message handler
    peer.onMessage((message, fromId) => {
        console.log(`Message from ${fromId}:`, message)
    })
    
    // Discover available peers
    const peers = await peer.discoverPeers()
    console.log("Available peers:", peers)
    
    return peer
}

// Use the messenger
const messenger = await setupMessenger()

Core Concepts

Connection Lifecycle

  1. Initialization: Create a MessagingPeer instance with configuration

  2. Connection: Connect to the signaling server

  3. Registration: Register with the server using your client ID and public key (NOTE: in this step, an automatic sign-verify mechanism is enforced to link signing and encryption keys for a specific handle)

  4. Discovery: Discover other available peers

  5. Communication: Exchange messages with other peers

  6. Disconnection: Disconnect from the server when done

PreviousEncryptionNextMessage Types

Last updated 15 days ago