What is client-side hosting?

Though p5.party uses a server to synchronize data between clients, sketches made with p5.party don't require (or allow) server side code. All sketch-specific behavior is handled on the client side. This is an unusual structure with two main benefits. First, keeping all the code client simplifies prototyping, making it much faster and easier to try ideas. Second, this structure allows a single p5.party server to host many different sketches doing entirely different things.

A lot of sketches will still need some code to be "in charge". Like a host at a party who can set up the room before other guests arrive, announce activities, and make sure everyone has a good time.

p5.party helps with this by keeping track of every client connected to a specific room and designates exactly one of the clients to be the host at any time. If the room has no clients, it also has no host.

How do you use client-side hosting?

In your p5.party sketch, you can call [partyIsHost()](<https://twisty-alder-868.notion.site/partyIsHost-ceef9466fc8e46979b2c4d99c4bd8674>) to see if the client is currently designated as the host. If there are no clients in the room when a client connects the client is named host immediately. Consequently, you can call [partyIsHost()](<https://twisty-alder-868.notion.site/partyIsHost-ceef9466fc8e46979b2c4d99c4bd8674>) from setup() to see if you are the first client in the room and do initial setup of shared objects, such as resetting game state to starting values.

You can also call [partyIsHost()](<https://twisty-alder-868.notion.site/partyIsHost-ceef9466fc8e46979b2c4d99c4bd8674>) to decide which client should enforce rules, check for win/loss conditions, or step a simulation.

Use [partyIsHost()](<https://twisty-alder-868.notion.site/partyIsHost-ceef9466fc8e46979b2c4d99c4bd8674>) any time something should be done by only one of the clients.

Common Questions

  1. How is the host chosen?

    When a client connects to an empty room that client is designated the host immediately and remains host until it disconnects. When the current host disconnects, another host is chosen automatically.

  2. What do I do if the host isn’t assigned?

    This should not happen. Any room with at least one connected client should always have exactly one host. If every connected client calls partyIsHost() at the same time, one client should receive true, and all the others should receive false.

  3. What happens when the host leaves the app?

    If there are any other connected clients, p5.party assigns one of them to be the host.