Skip to content

Commit 8cc0217

Browse files
committed
chore: add chat example
1 parent a9d67a7 commit 8cc0217

File tree

15 files changed

+317
-17
lines changed

15 files changed

+317
-17
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
fail-fast: false
2121
matrix:
2222
project:
23+
- js-libp2p-example-chat
2324
- js-libp2p-example-circuit-relay
2425
defaults:
2526
run:
@@ -68,6 +69,7 @@ jobs:
6869
fail-fast: true
6970
matrix:
7071
project:
72+
- js-libp2p-example-chat
7173
- js-libp2p-example-circuit-relay
7274
steps:
7375
- uses: convictional/trigger-workflow-and-wait@f69fa9eedd3c62a599220f4d5745230e237904be
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This project is dual licensed under MIT and Apache-2.0.
2+
3+
MIT: https://www.opensource.org/licenses/mit
4+
Apache-2.0: https://www.apache.org/licenses/license-2.0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# @libp2p/example-chat <!-- omit in toc -->
2+
3+
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
4+
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
5+
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-examples.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-examples)
6+
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p-examples/ci.yml?branch=main\&style=flat-square)](https://github.com/libp2p/js-libp2p-examples/actions/workflows/ci.yml?query=branch%3Amain)
7+
8+
> An example chat app using libp2p
9+
10+
## Table of contents <!-- omit in toc -->
11+
12+
- [Setup](#setup)
13+
- [Running](#running)
14+
- [License](#license)
15+
- [Contribution](#contribution)
16+
17+
## Setup
18+
19+
1. Install example depenedencies and build the project
20+
```console
21+
$ npm install
22+
$ npm run build
23+
```
24+
2. Open 2 terminal windows in the `./src` directory.
25+
26+
## Running
27+
28+
1. Run the listener in window 1, `node listener.js`
29+
2. Run the dialer in window 2, `node dialer.js`
30+
3. Type a message in either window and hit *enter*
31+
4. Tell yourself secrets to your hearts content!
32+
33+
## License
34+
35+
Licensed under either of
36+
37+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
38+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
39+
40+
## Contribution
41+
42+
Unless you explicitly state otherwise, any contribution intentionally submitted
43+
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
44+
dual licensed as above, without any additional terms or conditions.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@libp2p/example-chat",
3+
"version": "0.0.0",
4+
"description": "An example chat app using libp2p",
5+
"license": "Apache-2.0 OR MIT",
6+
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/examples/chat#readme",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/libp2p/js-libp2p-examples.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/libp2p/js-libp2p-examples/issues"
13+
},
14+
"type": "module",
15+
"scripts": {
16+
"test": "test-node-example test/*"
17+
},
18+
"dependencies": {
19+
"@chainsafe/libp2p-noise": "^13.0.0",
20+
"@chainsafe/libp2p-yamux": "^5.0.0",
21+
"@libp2p/peer-id-factory": "^3.0.0",
22+
"@libp2p/tcp": "^8.0.0",
23+
"@libp2p/websockets": "^7.0.0",
24+
"@multiformats/multiaddr": "^12.1.5",
25+
"@nodeutils/defaults-deep": "^1.1.0",
26+
"D": "^1.0.0",
27+
"it-length-prefixed": "^9.0.1",
28+
"it-map": "^3.0.3",
29+
"it-pipe": "^3.0.1",
30+
"libp2p": "^0.46.0",
31+
"p-defer": "^4.0.0",
32+
"uint8arrays": "^4.0.6"
33+
},
34+
"devDependencies": {
35+
"test-ipfs-example": "^1.1.0"
36+
},
37+
"private": true
38+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-disable no-console */
2+
3+
import { createFromJSON } from '@libp2p/peer-id-factory'
4+
import { multiaddr } from '@multiformats/multiaddr'
5+
import { createLibp2p } from './libp2p.js'
6+
import peerIdDialerJson from './peer-id-dialer.js'
7+
import peerIdListenerJson from './peer-id-listener.js'
8+
import { stdinToStream, streamToConsole } from './stream.js'
9+
10+
async function run () {
11+
const [idDialer, idListener] = await Promise.all([
12+
createFromJSON(peerIdDialerJson),
13+
createFromJSON(peerIdListenerJson)
14+
])
15+
16+
// Create a new libp2p node on localhost with a randomly chosen port
17+
const nodeDialer = await createLibp2p({
18+
peerId: idDialer,
19+
addresses: {
20+
listen: ['/ip4/0.0.0.0/tcp/0']
21+
}
22+
})
23+
24+
// Output this node's address
25+
console.log('Dialer ready, listening on:')
26+
nodeDialer.getMultiaddrs().forEach((ma) => {
27+
console.log(ma.toString())
28+
})
29+
30+
// Dial to the remote peer (the "listener")
31+
const listenerMa = multiaddr(`/ip4/127.0.0.1/tcp/10333/p2p/${idListener.toString()}`)
32+
const stream = await nodeDialer.dialProtocol(listenerMa, '/chat/1.0.0')
33+
34+
console.log('Dialer dialed to listener on protocol: /chat/1.0.0')
35+
console.log('Type a message and see what happens')
36+
37+
// Send stdin to the stream
38+
stdinToStream(stream)
39+
// Read the stream and output to console
40+
streamToConsole(stream)
41+
}
42+
43+
run()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { noise } from '@chainsafe/libp2p-noise'
2+
import { yamux } from '@chainsafe/libp2p-yamux'
3+
import { tcp } from '@libp2p/tcp'
4+
import { webSockets } from '@libp2p/websockets'
5+
import defaultsDeep from '@nodeutils/defaults-deep'
6+
import { createLibp2p as create } from 'libp2p'
7+
8+
export async function createLibp2p (_options) {
9+
const defaults = {
10+
transports: [
11+
tcp(),
12+
webSockets()
13+
],
14+
streamMuxers: [
15+
yamux()
16+
],
17+
connectionEncryption: [
18+
noise()
19+
]
20+
}
21+
22+
return create(defaultsDeep(_options, defaults))
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* eslint-disable no-console */
2+
3+
import { createFromJSON } from '@libp2p/peer-id-factory'
4+
import { createLibp2p } from './libp2p.js'
5+
import peerIdListenerJson from './peer-id-listener.js'
6+
import { stdinToStream, streamToConsole } from './stream.js'
7+
8+
async function run () {
9+
// Create a new libp2p node with the given multi-address
10+
const idListener = await createFromJSON(peerIdListenerJson)
11+
const nodeListener = await createLibp2p({
12+
peerId: idListener,
13+
addresses: {
14+
listen: ['/ip4/0.0.0.0/tcp/10333']
15+
}
16+
})
17+
18+
// Log a message when a remote peer connects to us
19+
nodeListener.addEventListener('peer:connect', (evt) => {
20+
const remotePeer = evt.detail
21+
console.log('connected to: ', remotePeer.toString())
22+
})
23+
24+
// Handle messages for the protocol
25+
await nodeListener.handle('/chat/1.0.0', async ({ stream }) => {
26+
// Send stdin to the stream
27+
stdinToStream(stream)
28+
// Read the stream and output to console
29+
streamToConsole(stream)
30+
})
31+
32+
// Output listen addresses to the console
33+
console.log('Listener ready, listening on:')
34+
nodeListener.getMultiaddrs().forEach((ma) => {
35+
console.log(ma.toString())
36+
})
37+
}
38+
39+
run()

examples/js-libp2p-example-chat/src/peer-id-dialer.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)