104 lines
4.0 KiB
Markdown
Raw Normal View History

2016-08-11 11:59:11 +02:00
# js-libp2p-websockets
2016-02-26 11:01:52 +00:00
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
2020-02-26 14:34:27 +01:00
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
2016-03-14 20:25:00 +00:00
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
2019-04-15 17:44:04 +01:00
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
2016-08-11 11:59:11 +02:00
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-libp2p-websockets/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-libp2p-websockets?branch=master)
[![Travis CI](https://travis-ci.org/libp2p/js-libp2p-websockets.svg?branch=master)](https://travis-ci.org/libp2p/js-libp2p-websockets)
[![Circle CI](https://circleci.com/gh/libp2p/js-libp2p-websockets.svg?style=svg)](https://circleci.com/gh/libp2p/js-libp2p-websockets)
2017-10-20 12:27:51 +01:00
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-websockets.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-websockets)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
2016-11-03 09:53:50 +01:00
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)
2018-02-20 10:32:38 +00:00
[![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)
[![](https://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png)](https://github.com/libp2p/interface-connection)
2016-11-03 09:53:50 +01:00
> 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.
2018-02-20 10:32:38 +00:00
## Usage
2018-02-20 10:32:38 +00:00
## 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).
2018-02-20 10:32:38 +00:00
## API
2018-02-20 10:32:38 +00:00
### Transport
2018-02-20 10:32:38 +00:00
[![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)
2018-02-20 10:32:38 +00:00
### Connection
2018-02-20 10:32:38 +00:00
[![](https://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png)](https://github.com/libp2p/interface-connection)