puter.peer.connect()

Websites Puter Apps Node.js Workers

Connects to a peer server and returns a PuterPeerConnection instance.

On websites, Puter.js may prompt the user to authenticate before connecting. To let someone join without an account, pass anonToken — and a turnGrant from the host, so the connection can still use Puter's relays. See puter.peer.createGuestGrant().

Syntax

const conn = await puter.peer.connect(inviteCode);
const conn = await puter.peer.connect(inviteCode, options);

Parameters

inviteCode (required)

A string invite code created by puter.peer.serve().

options (optional)

options is an object with the following properties:

  • iceServers (RTCIceServer[]) Custom ICE servers (STUN/TURN) to use instead of the Puter-managed relays.
  • forceRelay (boolean) Whether to force connections to route through a relay instead of attempting peer-to-peer (default). Metering charges may apply.
  • anonToken (String) Join without a Puter session. Any uuid — it identifies this guest for the duration of the session, and no sign-in prompt is shown. The host sees the guest as anonymous, so anything you want to call them is yours to send over the connection.
  • turnGrant (String) A grant from puter.peer.createGuestGrant(). Lets a guest use the Puter-managed relays on the host's account. Without one, a guest connects only where a direct connection is possible; with forceRelay, a guest needs one.
  • recoveryTimeout (Number) How long, in milliseconds, the connection keeps trying to restore a broken link before it closes, counted from when the link first showed trouble. Defaults to 60000.

Return value

A Promise that resolves to a PuterPeerConnection instance, which carries send() and close() methods and the open, message, close, and error events.

Example

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            const inviteCode = prompt('Enter invite code');
            const conn = await puter.peer.connect(inviteCode);

            conn.addEventListener('open', () => {
                conn.send('Hello from the client!');
            });
            conn.addEventListener('message', (msg) => {
                puter.print('Server says:', msg.data);
            });
        })();
    </script>
</body>
</html>