mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-04-25 09:02:30 +00:00
81 lines
2.9 KiB
Markdown
81 lines
2.9 KiB
Markdown
# js-libp2p-websockets
|
|
|
|
[](http://ipn.io)
|
|
[](http://libp2p.io/)
|
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
|
[](https://discuss.libp2p.io)
|
|
[](https://coveralls.io/github/libp2p/js-libp2p-websockets?branch=master)
|
|
[](https://travis-ci.org/libp2p/js-libp2p-websockets)
|
|
[](https://circleci.com/gh/libp2p/js-libp2p-websockets)
|
|
[](https://david-dm.org/libp2p/js-libp2p-websockets)
|
|
[](https://github.com/feross/standard)
|
|

|
|

|
|
|
|
[](https://github.com/libp2p/interface-transport)
|
|
[](https://github.com/libp2p/interface-connection)
|
|
|
|
> JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport interface
|
|
|
|
## Lead Maintainer
|
|
|
|
[Jacob Heun](https://github.com/jacobheun)
|
|
|
|
## Description
|
|
|
|
`libp2p-websockets` is the WebSockets implementation compatible with libp2p.
|
|
|
|
## Usage
|
|
|
|
## Install
|
|
|
|
### npm
|
|
|
|
```sh
|
|
> npm i libp2p-websockets
|
|
```
|
|
|
|
### Example
|
|
|
|
```js
|
|
const WS = require('libp2p-websockets')
|
|
const multiaddr = require('multiaddr')
|
|
const pipe = require('it-pipe')
|
|
const { collect } = require('streaming-iterables')
|
|
|
|
const addr = multiaddr('/ip4/0.0.0.0/tcp/9090/ws')
|
|
|
|
const ws = new WS({ upgrader })
|
|
|
|
const listener = ws.createListener((socket) => {
|
|
console.log('new connection opened')
|
|
pipe(
|
|
['hello'],
|
|
socket
|
|
)
|
|
})
|
|
|
|
await listener.listen(addr)
|
|
console.log('listening')
|
|
|
|
const socket = await ws.dial(addr)
|
|
const values = await pipe(
|
|
socket,
|
|
collect
|
|
)
|
|
console.log(`Value: ${values.toString()}`)
|
|
|
|
// Close connection after reading
|
|
await listener.close()
|
|
```
|
|
|
|
## API
|
|
|
|
### Transport
|
|
|
|
[](https://github.com/libp2p/interface-transport)
|
|
|
|
### Connection
|
|
|
|
[](https://github.com/libp2p/interface-connection)
|