Realtime Groups
Realtime groups are used to send realtime data to multiple users at once. This can be used to create group chats, group notifications, etc.
Joining a Group
Users can join a group by calling joinRealtime()
method.
params argument takes the following parameters:
group
: The name of the group to join.
Group name can be anything you want. If the group does not exist, it will be created automatically, otherwise the user will be joined to the existing group.
skapi.joinRealtime({ group: 'HelloWorld' }).then(res => {
console.log(res.message) // Joined realtime message group: "HelloWorld".
});
When the user is joined to the group successfully, the method will return the following object:
{
type: 'success',
message: 'Joined realtime message group: "HelloWorld"'.
}
For more detailed information on all the parameters and options available with the joinRealtime()
method, please refer to the API Reference below:
joinRealtime(params): Promise<{ type: 'success', message: string }>
TIP
Even if the user has joined the group, they can still receive realtime data sent individually to them.
Sending Data to a Group
Once the user have joined the group, user can send any JSON data over to a group by using postRealtime()
method. Any users in the group will receive the data.
The example below shows how to send realtime data to a group named "HelloWorld":
<form onsubmit="skapi.postRealtime(event, 'HelloWorld').then(u=>console.log(u))">
<input name="msg" required><input type="submit" value="Send">
</form>
skapi.postRealtime({ msg: "Hello World!" }, 'HelloWorld').then(res => console.log(res));
For more detailed information on all the parameters and options available with the postRealtime()
method, please refer to the API Reference below:
postRealtime(message, recipient): Promise<{ type: 'success', message: string }>
WARNING
The user must be joined to the group to send data to the group.
Leaving, Changing Groups
Users can join only one group at a time. If you want your user to change groups, you can call joinRealtime()
method with a different params.group
value.
Also, if you want to leave the group, you can call joinRealtime()
method with a params.group
value as empty string
or null
.
Listing Groups
Users can get a list of realtime groups by calling getRealtimeGroups()
method.
Example below shows listing all groups existing in your service:
skapi.getRealtimeGroups().then(res => {
console.log(res.list) // [{ group: 'HelloWorld', number_of_users: 1 }, ...]
});
You can search groups by the name. Below example shows how to search group names that start with "Hello":
skapi.getRealtimeGroups({ searchFor: 'group', value: 'Hello', condition: '>=' }).then(res => {
console.log(res.list) // [{ group: 'HelloWorld', number_of_users: 1 }, ...]
});
You can also list groups that have more than 10 users:
skapi.getRealtimeGroups({ searchFor: 'number_of_users', value: 10, condition: '>=' }).then(res => {
console.log(res.list) // [{ group: 'HelloUniverse', number_of_users: 11 }, ...]
});
For more detailed information on all the parameters and options available with the getRealtimeGroups()
method, please refer to the API Reference below:
getRealtimeGroups(params?, fetchOptions?): Promise<DatabaseResponse<{ group: string; number_of_users: number; }>>
Listing Users in a Group
Users can get a list of users in a group by calling getRealtimeUsers()
method.
Example below shows listing all users in the "HelloWorld" group:
skapi.getRealtimeUsers({ group: 'HelloWorld' }).then(res => {
console.log(res.list) // [{user_id: 'user_a', connection_id: 'user_cid'}, ...]
});
You can also search users in the group by their user ID. This is useful if you want to check if the user is in the group.
skapi.getRealtimeUsers({ group: 'HelloWorld', user_id: 'user_a' }).then(res => {
console.log(res.list) // [{user_id: 'user_a', connection_id: 'user_cid'}]
});
For more detailed information on all the parameters and options available with the getRealtimeUsers()
method, please refer to the API Reference below: