SDK Demo — Swarm, connections, raw messages, and Protomux
What this example does
Demonstrates all core BridgeSwarm SDK features in one page: swarm lifecycle, connection events and peerInfo, raw byte messages, and Protomux (channel with string and binary messages).
Prerequisites
Install the BridgeSwarm extension and native host before opening this example.
How to run
npm run examples
Open http://127.0.0.1:4173/sdk-demo/ (two tabs for P2P). Do not use file://.
Usage
- Click Join topic (default
bridge-swarm-sdk-demo). - Open the same page in another tab and join the same topic.
- Use Send raw to send text as raw bytes; use Send Protomux to send via the Protomux channel. The log shows which path each message used.
- Click Leave and destroy when done.
Files in this directory
- index.html — Page structure (topic, join/leave, raw/protomux inputs and buttons, log).
- style.css — Layout and theme (sections, badges, log).
- app.js — BridgeSwarm and Protomux setup, connection handling, raw and Protomux send/receive.
APIs and concepts
- Swarm/connection as in chat: BridgeSwarm.ready(), new BridgeSwarm(), swarm.join(), swarm.on('connection'), conn.on('data'), conn.write().
- swarm.createProtomux(conn) — Create a Protomux instance on the connection.
- mux.createChannel with protocol 'sdk-demo/v1' — Create a channel.
- channel.addMessage with encoding c.string or c.binary — Add string or binary message handlers; uses window.BridgeSwarmProtomux.c and b4a.
- channel.open(), message.send() — Open channel and send.
- Raw path: conn.write(data) and conn.on('data') for unencoded bytes.
See also
- API reference — Page API and host request types.
- Protomux over BridgeSwarm — Protomux setup and usage.