2016-08-11 11:59:11 +02:00
# js-libp2p-websockets
2016-02-26 11:01:52 +00:00
[](http://ipn.io)
2020-02-26 14:34:27 +01:00
[](http://libp2p.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)
2021-04-09 10:14:22 +02:00

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
```
2020-11-24 10:14:01 +01:00
### Constructor properties
2016-09-05 19:07:00 -04:00
2019-01-08 21:30:54 +01:00
```js
const WS = require('libp2p-websockets')
2020-11-24 10:14:01 +01:00
const properties = {
upgrader,
filter
}
2019-01-08 21:30:54 +01:00
2020-11-24 10:14:01 +01:00
const ws = new WS(properties)
```
2019-01-08 21:30:54 +01:00
2020-11-24 10:14:01 +01:00
| 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:
2019-01-08 21:30:54 +01:00
2020-11-24 10:14:01 +01:00
- `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` .
2019-09-30 12:14:28 +02:00
2020-11-24 10:14:01 +01:00
## Libp2p Usage Example
2019-09-30 12:14:28 +02:00
2020-11-24 10:14:01 +01:00
```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
}
}
}
})
2016-09-05 19:07:00 -04:00
```
2020-11-24 10:14:01 +01:00
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 ).
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)