Peer Library Usage
The peer library handles all the P2P functionality, including
- local identity generating and persistence,
- peers list manipulation and persistence,
- groups management and persistence in IndexedDB,
- tracking of online statuses of your friends using signal server,
- automatic P2P connection establishment with the friends that are online (signal server is used in the establishment phase, afterwards the communication goes directly P2P),
- automatic retrying of attempts in case of failure or timeout,
- connection cancelling in case of loss of interest and resources deallocation,
- automatic debounced exchange and merging of content changes.
Including the library state into your app state
The whole library is built upon TEA (The Elm Architecture) and its custom implementation for Reason called bs-black-tea. The architecture manages the global state in a manner similar to Redux, but better (because of Reason, of course 😉).
To add the library to your application you need to include its init
, update
and subscriptions
functions, as shown in tree-burst/RootModel.re
:
let init = () => {
let (p2p, p2pCmd) =
PM.init(
PM.InitConfig.make(
~contentInitializer=
crdt =>
/* This function can be used to init content of the default
group. */,
~signalServerUrl=Config.signalServerUrl,
~iceServers=Config.iceServers,
(),
),
);
(
{p2p, /* Rest of app's initial state */},
Cmd.batch([
p2pCmd |> Cmd.map(p2pMsgToMsg),
/* Rest of app's cmds */
]),
);
};
let update = (model, msg) =>
switch (msg) {
| P2PMsg(p2pMsg) =>
let (p2p, p2pCmd) = PM.update(model.p2p, p2pMsg);
({...model, p2p}, p2pCmd |> Cmd.map(p2pMsgToMsg));
| /* Rest of app's messages handling */ => /* do whatever */
| _ => (model, Cmd.none)
};
let subscriptions = model =>
PM.subscriptions(model.p2p) |> Sub.map(p2pMsgToMsg)
First start
Immediately after the first start of your application a new local identity (its private and public keys) is generated and saved into the IndexedDB. First initial group with content specified by the contentInitializer
function (see above) is created and connection with signal server is established.
Interacting with the library
For all information about all the model structure and available fields of the peer library, see the API Reference and sources of TreeBurst example project.