The P2PT class is defined and exposed by the p2pt
module :
const P2PT = require('p2pt')
In Typescript, you can use
import P2PT from "p2pt";
This is the base class that needs to be instantiated to use this library. It provides the API to implement P2P connections, communicate messages (even large content!) using WebTorrent WebSocket Trackers as the signalling server.
This event is emitted when a new peer connects.
Arguments passed to Event Handler: peer
Object
This event is emitted for every chunk of data received.
Arguments passed to Event Handler: peer
Object, data
Object
This event is emitted once all the chunks are received for a message.
Arguments passed to Event Handler: peer
Object, msg
Object
This event is emitted when a peer disconnects.
Arguments passed to Event Handler: peer
Object
This event is emitted when a successful connection to tracker is made.
Arguments passed to Event Handler: WebSocketTracker
Object, stats
Object
This event is emitted when some error happens with connection to tracker.
Arguments passed to Event Handler: Error
object, stats
Object
Instantiates the class
- Arguments:
- announceURLs:
Array
- Description: List of announce tracker URLs
- Default:
[]
- identifierString:
String
- Description: Identifier used to discover peers in the network
- Default:
''
- announceURLs:
// Find public WebTorrent tracker URLs here : https://github.com/ngosang/trackerslist/blob/master/trackers_all_ws.txt
var trackersAnnounceURLs = [
"wss://tracker.openwebtorrent.com",
"wss://tracker.sloppyta.co:443/",
"wss://tracker.novage.com.ua:443/",
"wss://tracker.btorrent.xyz:443/",
]
// This 'myApp' is called identifier and should be unique to your app
var p2pt = new P2PT(trackersAnnounceURLs, 'myApp')
In Typescript, the P2PT
class accepts an optional type parameter to constrain the type of messages you can pass to the send
function. It doesn't constrain the type of messages you recieve, since any peer could send anything.
type Msg = 'hello' | { goodbye: boolean }
const p2pt = new P2PT<Msg>(trackersAnnounceURLs, 'myApp')
// ... find a peer ...
p2pt.send(peer, 'some_message') // TS typecheck error: Argument of type 'string' is not assignable to parameter of type 'Msg'.
p2pt.send(peer, 'hello') // ok!
p2pt.send(peer, { goodbye: true }) // ok!
Sets the identifier string used to discover peers in the network
- Arguments:
- identifierString:
String
- Description: Identifier used to discover peers in the network
- identifierString:
- Returns:
void
Connects to network and starts discovering peers
- Arguments: None
- Returns:
void
Request More Peers
- Arguments: None
- Returns:
Promise
- resolve(peers)
- peers: Object
- resolve(peers)
- Arguments:
- peer:
Object
- Description: Stores information of a Peer
- msg:
Object
- Description: Message to send
- msgID:
Number
- Description: ID of message if it's a response to a previous message. You won't need to pass this
- Default:
''
- peer:
- Returns:
Promise
- resolve([peer, msg])
- peer:
Object
- msg:
Object
- peer:
- resolve([peer, msg])
Destroy the P2PT Object
- Arguments: None
- Returns:
void