p5.party provides a way for clients in the same room to send messages to each other. You can use messages to communicate about important events in your game and app. For example a client might send a message when it joins the game, or before it leaves. You might send a message when a round should begin, when a player acquires an achievement, when a character shoots, etc.

All clients can send messages, and they will be received by clients in the same room that have subscribed to that type of message.

Message Names and Data

When you emit or subscribe to messages you provide a name. The name should identify the type of message you are sending. Names should start with a letter and contain only letters, numbers, and underscores. Example names: playSound playerFired clientConnected achievementUnlocked

Messages can also optionally contain data. The data has the same requirements as data on a shared objects.

Messages vs Shared Objects

Both messages and shared objects are used to communicate in p5.party. Messages are best communicating events that can be acted on and forgotten. Shared objects are best for communicating state that needs to be remembered.

The Sound Board example uses messaging to communicate when the user clicks. When the user clicks, it sends a “playSound” message to every client in the room (including itself). Connected clients play the sound and that’s it—the work is done and there is no need to remember anything about the event.

In contrast the Idle Clicker keeps track of user clicks using shared objects. Each time a user clicks, their click count is incremented by one. Each client shows the count on the canvas, and that count is remembered. Even if a client connects mid-game it can see how many clicks have happened.

Using Messaging

To send a message use partyEmit().

To receive and respond to messages use partySubscribe().

To stop receiving messages use partyUnsubscribe().

partyEmit()

partySubscribe()

partyUnsubscribe()