mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-03-31 07:31:21 +00:00
84 lines
2.8 KiB
Markdown
84 lines
2.8 KiB
Markdown
# js-libp2p-websockets
|
|
|
|
[](http://ipn.io)
|
|
[](http://ipfs.io/)
|
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
|
[](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 pull = require('pull-stream')
|
|
|
|
const mh = multiaddr('/ip4/0.0.0.0/tcp/9090/ws')
|
|
|
|
const ws = new WS()
|
|
|
|
const listener = ws.createListener((socket) => {
|
|
console.log('new connection opened')
|
|
pull(
|
|
pull.values(['hello']),
|
|
socket
|
|
)
|
|
})
|
|
|
|
listener.listen(mh, () => {
|
|
console.log('listening')
|
|
|
|
pull(
|
|
ws.dial(mh),
|
|
pull.collect((err, values) => {
|
|
if (!err) {
|
|
console.log(`Value: ${values.toString()}`)
|
|
} else {
|
|
console.log(`Error: ${err}`)
|
|
}
|
|
|
|
// Close connection after reading
|
|
listener.close()
|
|
}),
|
|
)
|
|
})
|
|
```
|
|
|
|
## API
|
|
|
|
### Transport
|
|
|
|
[](https://github.com/libp2p/interface-transport)
|
|
|
|
### Connection
|
|
|
|
[](https://github.com/libp2p/interface-connection)
|