mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-04-25 04:32:33 +00:00
* feat: custom address filter BREAKING CHANGE: Only DNS+WSS addresses are now returned on filter by default in the browser. This can be overritten by the filter option and filters are provided in the module.
104 lines
4.0 KiB
Markdown
104 lines
4.0 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
|
|
```
|
|
|
|
### Constructor properties
|
|
|
|
```js
|
|
const WS = require('libp2p-websockets')
|
|
|
|
const properties = {
|
|
upgrader,
|
|
filter
|
|
}
|
|
|
|
const ws = new WS(properties)
|
|
```
|
|
|
|
| Name | Type | Description | Default |
|
|
|------|------|-------------|---------|
|
|
| upgrader | [`Upgrader`](https://github.com/libp2p/interface-transport#upgrader) | connection upgrader object with `upgradeOutbound` and `upgradeInbound` | **REQUIRED** |
|
|
| filter | `(multiaddrs: Array<Multiaddr>) => Array<Multiaddr>` | override transport addresses filter | **Browser:** DNS+WSS multiaddrs / **Node.js:** DNS+{WS, WSS} multiaddrs |
|
|
|
|
You can create your own address filters for this transports, or rely in the filters [provided](./src/filters.js).
|
|
|
|
The available filters are:
|
|
|
|
- `filters.all`
|
|
- Returns all TCP and DNS based addresses, both with `ws` or `wss`.
|
|
- `filters.dnsWss`
|
|
- Returns all DNS based addresses with `wss`.
|
|
- `filters.dnsWsOrWss`
|
|
- Returns all DNS based addresses, both with `ws` or `wss`.
|
|
|
|
## Libp2p Usage Example
|
|
|
|
```js
|
|
const Libp2p = require('libp2p')
|
|
const Websockets = require('libp2p-websockets')
|
|
const filters = require('libp2p-websockets/src/filters')
|
|
const MPLEX = require('libp2p-mplex')
|
|
const { NOISE } = require('libp2p-noise')
|
|
|
|
const transportKey = Websockets.prototype[Symbol.toStringTag]
|
|
const node = await Libp2p.create({
|
|
modules: {
|
|
transport: [Websockets],
|
|
streamMuxer: [MPLEX],
|
|
connEncryption: [NOISE]
|
|
},
|
|
config: {
|
|
transport: {
|
|
[transportKey]: { // Transport properties -- Libp2p upgrader is automatically added
|
|
filter: filters.dnsWsOrWss
|
|
}
|
|
}
|
|
}
|
|
})
|
|
```
|
|
|
|
For more information see [libp2p/js-libp2p/doc/CONFIGURATION.md#customizing-transports](https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md#customizing-transports).
|
|
|
|
## API
|
|
|
|
### Transport
|
|
|
|
[](https://github.com/libp2p/interface-transport)
|
|
|
|
### Connection
|
|
|
|
[](https://github.com/libp2p/interface-connection)
|