<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Debojyoti Ganguly</title>
    <description>The latest articles on DEV Community by Debojyoti Ganguly (@mrblueblobguy).</description>
    <link>https://dev.to/mrblueblobguy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3975667%2Fc6af568a-c843-4b0a-b2e8-cedb31cc75e4.jpg</url>
      <title>DEV Community: Debojyoti Ganguly</title>
      <link>https://dev.to/mrblueblobguy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrblueblobguy"/>
    <language>en</language>
    <item>
      <title>Building Peetopee: A Peer-to-Peer Messenger with Bun, libp2p, SQLite, and Modern Cryptography</title>
      <dc:creator>Debojyoti Ganguly</dc:creator>
      <pubDate>Tue, 09 Jun 2026 09:53:09 +0000</pubDate>
      <link>https://dev.to/mrblueblobguy/building-peetopee-a-peer-to-peer-messenger-with-bun-libp2p-sqlite-and-modern-cryptography-n2h</link>
      <guid>https://dev.to/mrblueblobguy/building-peetopee-a-peer-to-peer-messenger-with-bun-libp2p-sqlite-and-modern-cryptography-n2h</guid>
      <description>&lt;p&gt;Most messaging applications rely on centralized infrastructure. Messages travel through company-owned servers, identities are managed by centralized systems, and availability depends on cloud providers.&lt;/p&gt;

&lt;p&gt;I wanted to understand what happens when you remove all of that.&lt;/p&gt;

&lt;p&gt;Instead of reading more documentation about distributed systems, I decided to build one.&lt;/p&gt;

&lt;p&gt;The result was Peetopee, a proof-of-concept peer-to-peer messenger built with Bun, TypeScript, libp2p, SQLite, and WebCrypto.&lt;/p&gt;

&lt;p&gt;The goal wasn't to build a Signal competitor. The goal was to learn how peer discovery, cryptographic identity, key exchange, session management, and encrypted messaging actually work under the hood.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Requirements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before writing any code, I defined a few constraints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No centralized server&lt;/li&gt;
&lt;li&gt;Persistent peer identities&lt;/li&gt;
&lt;li&gt;End-to-end encrypted messages&lt;/li&gt;
&lt;li&gt;Local message history&lt;/li&gt;
&lt;li&gt;Terminal-first interface&lt;/li&gt;
&lt;li&gt;Direct peer-to-peer communication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That gave me a surprisingly complete messaging system architecture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Bun?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I chose Bun mostly because I wanted to see how far its ecosystem had matured.&lt;/p&gt;

&lt;p&gt;A few features made it particularly attractive:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Built-in TypeScript support&lt;/li&gt;
&lt;li&gt;Fast startup times&lt;/li&gt;
&lt;li&gt;Native SQLite support&lt;/li&gt;
&lt;li&gt;WebCrypto APIs&lt;/li&gt;
&lt;li&gt;Minimal tooling setup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since this project involved networking, cryptography, and persistence, Bun covered most of the infrastructure I needed out of the box.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Networking Layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For networking, I used libp2p.&lt;/p&gt;

&lt;p&gt;Instead of worrying about low-level socket management, libp2p provides abstractions for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Peer discovery&lt;/li&gt;
&lt;li&gt;Transport security&lt;/li&gt;
&lt;li&gt;Stream multiplexing&lt;/li&gt;
&lt;li&gt;Peer identification&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Peetopee uses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TCP&lt;/li&gt;
&lt;li&gt;Noise&lt;/li&gt;
&lt;li&gt;Yamux&lt;/li&gt;
&lt;li&gt;mDNS&lt;/li&gt;
&lt;li&gt;Identify&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Communication happens over a custom protocol:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/p2p-chat/1.0.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Messages are serialized as JSON and exchanged over libp2p streams.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Two Identities, Not One&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One design decision I particularly liked was separating network identity from application identity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Network Identity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;libp2p already uses Ed25519 keys to generate Peer IDs.&lt;/p&gt;

&lt;p&gt;These identities are persisted so nodes remain recognizable after restarts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Application Identity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I created a second identity using WebCrypto.&lt;/p&gt;

&lt;p&gt;This identity handles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handshake authentication&lt;/li&gt;
&lt;li&gt;Session establishment&lt;/li&gt;
&lt;li&gt;Message signing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keeping them separate made the architecture much cleaner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building the Handshake&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The cryptographic handshake combines several modern primitives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ed25519 signatures&lt;/li&gt;
&lt;li&gt;P-256 ECDH&lt;/li&gt;
&lt;li&gt;HKDF&lt;/li&gt;
&lt;li&gt;AES-GCM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The process looks roughly like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exchange identities&lt;/li&gt;
&lt;li&gt;Verify signatures&lt;/li&gt;
&lt;li&gt;Exchange ephemeral keys&lt;/li&gt;
&lt;li&gt;Perform ECDH&lt;/li&gt;
&lt;li&gt;Derive session keys with HKDF&lt;/li&gt;
&lt;li&gt;Store the session
Once complete, both peers possess the same symmetric encryption key without transmitting it directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Persistence&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A chat application isn't very useful if it forgets everything after a restart.&lt;/p&gt;

&lt;p&gt;Peetopee stores:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Identities&lt;/li&gt;
&lt;li&gt;Prekeys&lt;/li&gt;
&lt;li&gt;Sessions&lt;/li&gt;
&lt;li&gt;Messages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;in SQLite.&lt;/p&gt;

&lt;p&gt;The storage layer is intentionally boring, and that's a compliment.&lt;/p&gt;

&lt;p&gt;SQLite provides reliability without adding unnecessary complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Challenges&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The hardest part wasn't networking.&lt;/p&gt;

&lt;p&gt;It wasn't cryptography.&lt;/p&gt;

&lt;p&gt;It was ecosystem compatibility.&lt;/p&gt;

&lt;p&gt;Bun has come a long way, but many packages still assume a Node.js runtime. Building around those assumptions required more effort than expected.&lt;/p&gt;

&lt;p&gt;Another challenge was designing a handshake that was educational while remaining understandable. I intentionally avoided implementing something as complex as Signal's Double Ratchet because the goal was learning, not protocol innovation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I Learned&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Three things stood out:&lt;/p&gt;

&lt;p&gt;libp2p removes an enormous amount of complexity from distributed systems.&lt;br&gt;
Identity management is often more important than message transport.&lt;br&gt;
Good architecture matters even in small projects.&lt;/p&gt;

&lt;p&gt;Separating crypto, networking, storage, and services early prevented the project from turning into a tangled mess later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's Next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Potential future improvements include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Double Ratchet support&lt;/li&gt;
&lt;li&gt;NAT traversal&lt;/li&gt;
&lt;li&gt;Group messaging&lt;/li&gt;
&lt;li&gt;Multi-device synchronization&lt;/li&gt;
&lt;li&gt;Better peer discovery&lt;/li&gt;
&lt;li&gt;Rich terminal UI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For now, though, Peetopee accomplished its original goal: helping me understand how decentralized messaging systems actually work.&lt;/p&gt;

&lt;p&gt;The full source code is available on GitHub if you'd like to explore the implementation yourself.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>p2p</category>
      <category>cryptography</category>
      <category>libp2p</category>
    </item>
  </channel>
</rss>
