This repository consists of two main packages: correspond and correspond-signaling.
correspond Is the series of React components and helpers for establishing a peer-to-peer WebRTC connection through the correspond-signaling server.
correspond-signaling Establishes a peer-to-peer WebRTC connection through WebSockets initially.
First, Create two projects, a server-side node application that will work as the signaling layer for the WebRTC connection and a React front-end application.
-
Add the signaling module.
yarn add correspond-signaling
(ornpm install correspond-signaling
) -
Create an instance of the signaling module:
var startSignalingServer = require("correspond-signaling");
startSignalingServer(8088); // run on port 8088
- Run the application
node server.js
(or whatever you name the server)
- Add
correspond
to your React project.yarn add correspond
(ornpm install correspond
) - Define a code path for establishing the session as the Host (e.g. something like react/@reach router).
import { Correspondent } from 'correspond'
...
<Correspondent socketAddress={SOCKET_ADDRESS}>
{connectionProps => <Host {...connectionProps} />}
</Correspondent>
- Define another code path for establishing a session as a client
import { Correspondent } from 'correspond'
...
<Correspondent socketAddress={SOCKET_ADDRESS}>
{connectionProps => <Client {...connectionProps} />}
</Correspondent>
- Create Host component using the
Host
container component fromcorrespond
. This Host depends on theconnectionProps
from theCorrespondent
component above.
import { Host } from "correspond";
...
<Host {...props}>
{({
hasSocketConnection,
hasDataChannel,
sendMessage,
accessCode,
subscribe
}) => {
...
}
- Create a Client component using the
Client
container component fromcorrespond
. This Client depends on theconnectionProps
from theCorrespondent
component above.
import { Client } from "correspond";
...
<ClientWrapper {...props}>
{({
hasDataChannel,
sendMessage,
subscribe,
establishConnectionWithKey
}) => ( /* standard component stuff using these props*/ )}
</ClientWrapper>
See the examples under the packages/example-*
directories