Files
research/building-tools/p2p-chat.md
T
2026-02-19 10:12:39 +00:00

32 lines
624 B
Markdown

# Tutorial: Simple P2P Chat w/ Hypercore
## Step-by-Step
1. **Cores**:
```js
const corestore = Corestore('./chat')
const messages = corestore.get('messages')
```
1. **Swarm**:
```js
swarm.join(messages.discoveryKey)
swarm.on('connection', c => messages.replicate(c))
```
1. **Send/Recv**:
```js
await messages.append(JSON.stringify({from: me, text}))
for await (const block of messages.createReadStream({live:true})) {
const msg = JSON.parse(block)
display(msg)
}
```
1. **Run**: npm i hypercore hyperswarm corestore; node chat.js [topic]
**Extend**: Corestore namespaces (users/rooms), hyperdrive attachments.