Spaces are virtual environments that users join to send messages to other users. With PubNub, you can create spaces and assign them common names, descriptions, and any custom information needed to make your application work effectively.
Direct Spaces - spaces that represent a direct chat if only two users belong to that space. These spaces can be set as private so no one else has access to read messages within that space.
Group Spaces - spaces that represent a group chat if multiple users belong to that space. These spaces can be set as public, or set as private to only allow users to join the space if they have permissions to do so.
For more information on managing public and private permissions, refer to Managing User Permissions.
Spaces vs Channels
A PubNub channel is simply a virtual path between publishing and subscribing clients. A space is a superset of a channel's functionality, allowing you to store common names, descriptions, and any custom information your application requires.
There is no pre-built association between spaces and PubNub channels. In most cases, you'll want to set the
spaceId equal to the
channel. Then, if you subscribe to a channel, you'll also get updates about the space with that ID.
Subscribe to channels
subscribe() operation to connect to spaces and begin listening for messages on a single socket connection. The client will be notified of connectivity via an event listener. Your application should wait for a
PNConnectedCategory status event before publish a message to avoid a potential race conditions.
Clients can also receive events when spaces are updated or deleted by subscribing to the same channels as the spaceId. You can enable or disable space events from your key settings in the PubNub Dashboard.
You don't need to create a space or create memberships to subscribe to channels and start exchanging messages. You only need to create a space if you wish to store additional properties for the chat room that the channel represents.
Users can connect to up to 50 spaces using multiplexing. To connect to more spaces, refer to Managing channel groups.
Create a space
Creates a space with the specified properties. Returns the created space object, optionally including the space's custom data object.
|Yes||The space's unique identifier. must not be empty, and may contain up to 92 UTF-8 byte sequences.|
|Yes||The space's name. must not be empty, or consist only of whitespace characters.|
|No||The description of the space.|
|No||JSON object of key-value pairs with supported data types. Values must be scalar only; arrays or objects are not supported.|
Get a space
Returns the specified space object, optionally including the space's custom data object.
Get all spaces
Returns the paginated list of space objects, optionally including the space's custom data object.
Update a space
Updates a space with the specified properties. Returns the updated space object, optionally including the space's custom data object.
You can change all the space object's properties, except its
id. If you update the custom property, you must completely replace it; partial updates aren't supported.
Delete a space
Space deletion is a server-side operation. Refer to Creating and Managing Data for details.