2016-08-11 11:59:11 +02:00
|
|
|
# js-libp2p-websockets
|
2016-02-26 11:01:52 +00:00
|
|
|
|
|
|
|
[](http://ipn.io)
|
2016-08-11 11:59:11 +02:00
|
|
|
[](http://ipfs.io/)
|
2016-03-14 20:25:00 +00:00
|
|
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
2019-04-15 17:44:04 +01:00
|
|
|
[](https://discuss.libp2p.io)
|
2016-08-11 11:59:11 +02:00
|
|
|
[](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)
|
2017-10-20 12:27:51 +01:00
|
|
|
[](https://david-dm.org/libp2p/js-libp2p-websockets)
|
|
|
|
[](https://github.com/feross/standard)
|
2016-11-03 09:53:50 +01:00
|
|
|

|
|
|
|

|
|
|
|
|
2018-02-20 10:32:38 +00:00
|
|
|
[](https://github.com/libp2p/interface-transport)
|
|
|
|
[](https://github.com/libp2p/interface-connection)
|
2016-11-03 09:53:50 +01:00
|
|
|
|
2016-06-19 06:35:31 +01:00
|
|
|
> JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport interface
|
2016-09-05 19:07:00 -04:00
|
|
|
|
2018-06-26 18:02:14 +02:00
|
|
|
## Lead Maintainer
|
|
|
|
|
|
|
|
[Jacob Heun](https://github.com/jacobheun)
|
|
|
|
|
2016-09-05 19:07:00 -04:00
|
|
|
## Description
|
|
|
|
|
|
|
|
`libp2p-websockets` is the WebSockets implementation compatible with libp2p.
|
|
|
|
|
2018-02-20 10:32:38 +00:00
|
|
|
## Usage
|
2016-09-05 19:07:00 -04:00
|
|
|
|
2018-02-20 10:32:38 +00:00
|
|
|
## Install
|
2016-09-05 19:07:00 -04:00
|
|
|
|
|
|
|
### npm
|
|
|
|
|
|
|
|
```sh
|
|
|
|
> npm i libp2p-websockets
|
|
|
|
```
|
|
|
|
|
2018-02-20 10:32:38 +00:00
|
|
|
### Example
|
2016-09-05 19:07:00 -04:00
|
|
|
|
2019-01-08 21:30:54 +01:00
|
|
|
```js
|
|
|
|
const WS = require('libp2p-websockets')
|
|
|
|
const multiaddr = require('multiaddr')
|
2019-09-30 12:14:28 +02:00
|
|
|
const pipe = require('it-pipe')
|
|
|
|
const { collect } = require('streaming-iterables')
|
2019-01-08 21:30:54 +01:00
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
const addr = multiaddr('/ip4/0.0.0.0/tcp/9090/ws')
|
2019-01-08 21:30:54 +01:00
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
const ws = new WS({ upgrader })
|
2019-01-08 21:30:54 +01:00
|
|
|
|
|
|
|
const listener = ws.createListener((socket) => {
|
|
|
|
console.log('new connection opened')
|
2019-09-30 12:14:28 +02:00
|
|
|
pipe(
|
|
|
|
['hello'],
|
2019-01-08 21:30:54 +01:00
|
|
|
socket
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
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()
|
2016-09-05 19:07:00 -04:00
|
|
|
```
|
|
|
|
|
2018-02-20 10:32:38 +00:00
|
|
|
## API
|
2016-09-06 09:37:27 -04:00
|
|
|
|
2018-02-20 10:32:38 +00:00
|
|
|
### Transport
|
2016-09-06 09:37:27 -04:00
|
|
|
|
2018-02-20 10:32:38 +00:00
|
|
|
[](https://github.com/libp2p/interface-transport)
|
2016-09-05 19:07:00 -04:00
|
|
|
|
2018-02-20 10:32:38 +00:00
|
|
|
### Connection
|
2016-09-05 19:07:00 -04:00
|
|
|
|
2018-02-20 10:32:38 +00:00
|
|
|
[](https://github.com/libp2p/interface-connection)
|