Store channel and user data
Signaling storage is a powerful feature that enables you to store and manage channel and user attributes in a serverless storage system. It acts as an extension of your database, offering synchronization across all participants. By using signaling storage, you can develop innovative, reliable, and scalable applications without the hassle of setting up your own database. This seamless integration ensures that data is readily available and up-to-date for all users, making it easier to build real-time and collaborative applications.
Your app receives metadata modification events in real-time enabling you to update the front-end accordingly. You can leverage user and channel metadata in Signaling apps to:
-
Maintain search history: Enable users to quickly search for specific messages, users, or topics within the app.
-
Customize notifications: Enable users to customize their notification settings, such as choosing notifications to receive and setting custom alert tones.
-
Enhance security: Enforce security measures, such as verifying user identity and encrypting messages.
Understand the tech
Using Signaling storage you associate metadata with a particular channel or a specific user:
-
Channel metadata: store and distribute contextual channel data in your app, such as props, announcements, member lists, and relationship chains. When channel properties are set, updated, or deleted by a user, an event notification is triggered, and other users in the channel receive this information within 100ms.
-
User metadata: store and distribute contextual data about users in your app. A user has one set of metadata with one or more attributes. When an attribute is set, updated or deleted, an event notification is triggered, and users who subscribe to this users metadata will receive this information within 100ms.
This section presents an overview of the steps required to integrate Signaling storage into your app. The following figure shows the basic workflow you implement to read and write channel and user metadata:
Prerequisites
To follow this page, you must have:
- Setup the Signaling SDK reference app.
Implement storage
In the storage reference app, after the local user logs in to Signaling, you set their metadata in the form of key-value pairs. When the user modifies a value stored as metadata, you call the update method to save the new value. You show a list of all users currently in the channel. When the local user selects another user from the list, you retrieve and display their metadata. You subscribe to the metadata of remote users to be notified of changes.
Implement the user interface
To show a list of users in the channel and enable the local user to update their status, you add the following elements to the user interface:
- A
TextView
to display a caption - A
Button
to switch the local user status - A
LinearLayout
inside aScrollView
to display a list of users in the channel and their status
To update the UI, in /app/res/layout/activity_main.xml
, replace the entire <ScrollView>...</ScrollView>
block with the following:
Handle the system logic
This section describes the steps required to use the relevant libraries and declare the necessary variables.
-
Import the required Agora and Android libraries
To read and write metadata and manage UI elements, add the following statements after the last
import
statement in/app/java/com.example.<projectname>/MainActivity
: -
Define variables to manage user
TextView
s and the user availability statusIn
/app/java/com.example.<projectname>/MainActivity
, add the following declarations to theMainActivity
class:
Read and write metadata
To implement the use of metadata, take the following steps:
-
Set local user metadata
After a user successfully logs in to Signalling, you clear unwanted metadata from a previous login, and add fresh metadata that you want to share with other users. To do this:
-
Add the following method to the
MainActivity
class: -
Call
setLocalUserMetaData
after login succeeds.In
onClickLogin
, add the following to theonSuccess
callback ofmRtmClient.login
:
-
-
Update local user metadata
When the local user taps the button to switch their status, you update the user's metadata. To do this, add the following method to the
MainActivity
class: -
Handle the
onUserMetadataUpdated
callbackWhen you receive notification of change in a user's metadata, you update the status of the user in the user list. In
onCreate
, replace theonUserMetadataUpdated
method undernew RtmClientListener() {
with the following: -
Show the list of users in a channel
When the local user successfully joins a Signalling channel, you call
mRtmChannel.getMembers
to obtain a list of users in the channel and add each user to the user list. To do this:-
In the
MainActivity
class, add the following method: -
Call
updateChannelMemberList
after the user successfully joins a channelIn
onClickJoin
, add the following to theonSuccess
callback ofmRtmChannel.join
:
-
-
Display a user in the list
To fill the channel user list, you create a
TextView
for each user. At this time, you subscribe to the user's metadata to receive notification of changes. To add or update an item in the user list:-
Add the following method to the
MainActivity
class: -
Add the following to the
onMemberJoined
callback inonClickJoin
:
-
-
Remove a user from the list
When a remote user leaves the channel, you remove the corresponding
TextView
from the user list. To do this:-
Add the following method to the
MainActivity
class: -
Add the following to the
onMemberLeft
callback inonClickJoin
:
-
-
Retrieve and show metadata for a selected user
When the local user selects a user from the list, you retrieve metadata for that user and display the key-value pairs in a
Toast
message. To do this, add the following method to theMainActivity
class: -
Clear the user list when the local user leaves the channel
When the local user leaves the channel, you clear the user list. To do this, in
onClickLeave
, add the following lines to theonSuccess
callback ofmRtmChannel.leave
: -
Clear metadata on logout
When the local user logs out of Signalling, you clear the local user metadata. To do this, in
onClickLogout
, add the following lines to theonSuccess
callback ofmRtmClient.logout
:
Test storage
This section explains how to run the storage
reference app and test channel and user metadata features. To run the project, take the following steps:
To test this functionality:
-
Set the APP ID
In
agora-manager/res/raw/config.json
, setappId
to the AppID of your project. -
Set the authentication method
Choose one of the following authentication methods:
- Temporary token:
- Set
rtcToken
with the value of your temporary token. - Set
channelName
- with the name of a channel you used to create the token.
- Set
- Authentication server:
- Setup an Authentication server
- In
config.json
, set:channelName
with the name of a channel you want to join.rtcToken
to an empty string.serverUrl
to the base URL of your authentication server. For example,https://agora-token-service-production-1234.up.railway.app
.
- Temporary token:
-
Start the Android reference app
-
In Android Studio, connect a physical Android device to your development machine.
-
Click Run to start the app.
A moment later you see the project installed on your device. If this is the first time you run the project, you need to grant microphone and camera access to your app.
-
-
In Android Studio, open
app/java/com.example.<projectname>/MainActivity
, and updateappId
,channelName
andtoken
with the values for your temporary token. -
Connect a physical Android device to your development device.
-
In Android Studio, click Run app. A moment later you see the project installed on your device.
If this is the first time you run the project, grant microphone and camera access to your app.
- Click Join to start a call.
You see your app starts the proxy service and magically connects to the Agora SD-RTN™ which was not possible in a restricted network environment.
Reference
This section contains additional information that either supplements the content on this page or directs you to documentation that covers other aspects of this product.
Metadata API
Use the following metadata API to implement your Signalling solution:
User metadata
Creates an RtmMetadataItem
instance.
Method
Basic Use
Response
Returns a RtmMetadataItem
instance. RtmMetadataItem
is a basic unit item of a UserMetadata
and
ChannelMetadata
. It contains the following properties:
Property Name | Type | Description |
---|---|---|
void setKey(String key) | function | Set key for current RtmMetadataItem |
String getKey() | function | Get key for current RtmMetadataItem |
void setValue(String value) | function | Set value for current RtmMetadataItem |
String getValue() | function | Get value for current RtmMetadataItem |
void setRevision(long revision) | function | Set revision for current RtmMetadataItem |
long getRevision() | function | Get revision for current RtmMetadataItem |
long getLastUpdateTs() | function | Get updated time for current RtmMetadataItem |
String getAuthorUserId() | function | Get the uid of who update this record for current RtmMetadataItem |
Get user metadata
Gets all metadataItems of a specified user.
Method
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
userId | String | yes | Unique user identifier. | |
resultCallback | function | yes | callback function |
Basic Use
Response
Returns a type RtmMetadata
for the specific uid
which contains the following properties:
Property Name | Type | Description |
---|---|---|
items | List<RtmMetadataItem> | RtmMetadataItem type array which contains a single Key-Value metadata item for user, see RtmMetadataItem for more information |
majorRevision | number | The major revision for this user metadata |
Set user metadata
Set the local user’s metadata.
Method
Property Name | Type | Description |
---|---|---|
items | List<RtmMetadataItem> | RtmMetadataItem type list which contains a single Key-Value metadata item for user, see RtmMetadataItem for more information |
options | RtmMetadataOptions | add optional propeties for current user metadata, see RtmMetadataOptions for more information |
resultCallback | function | callback function |
RtmMetadataOptions
is a set of optional properties for operations, it contains the following properties:
Property Name | Type | Description |
---|---|---|
majorRevision | number | Revison control parametes. when the majorRevision you supplied is as same as the one in the storage, this operation will success. |
enableRecordTs | bool | When it is set to true , the final RtmMetadataItem will record the updating time automatically |
enableRecordUserId | bool | When it is set to true , the final RtmMetadataItem will record who have updated this item automatically |
Basic Use
Response
None
caution: This operation will reset all current meatadata and set a new one.
Add user metadata
Adds metadata items to local user’s metadata.
Method
Property Name | Type | Description |
---|---|---|
items | List< RtmMetadataItem > | RtmMetadataItem type array which contains a single Key-Value metadata item for user, see RtmMetadataItem for more information |
options | RtmMetadataOptions | add optional propeties for current user metadata, see RtmMetadataOptions for more information |
resultCallback | function | callback function |
Basic Use
Response
None
Caution: This operation will add new metadata items for current user. It will report errors if the key of new
RtmMetadataItem
has already existed in the user metadata.
Clear user metadata
Delete all the local user’s metadata items.
Method
Property Name | Type | Description |
---|---|---|
options | RtmMetadataOptions | add optional propeties for current user metadata, see RtmMetadataOptions for more information |
resultCallback | function | callback function |
Basic Use
Response
None
Caution: This operation will clean all user metadata.
Update user metadata
Update the local user’s metadata items.
Method
Property Name | Type | Description |
---|---|---|
items | List<RtmMetadataItem> | RtmMetadataItem type array which contains a single Key-Value metadata item for user, see RtmMetadataItem for more information |
options | RtmMetadataOptions | add optional propeties for current user metadata, see RtmMetadataOptions for more information |
resultCallback | function | callback function |
Basic Use
Response
None
Caution : This operation can only valid for updating the existing metadata items, or it will report errors.
Delete user metadata
Delete the local user’s metadata items.
Method
Property Name | Type | Description |
---|---|---|
items | List< RtmMetadataItem > | RtmMetadataItem type array which contains a single Key-Value metadata item for user, see RtmMetadataItem for more information |
options | RtmMetadataOptions | add optional propeties for current user metadata, see RtmMetadataOptions for more information |
resultCallback | function | callback function |
Basic Use
Response
None
Caution:This operation will always excute regardless of the existing of metadata items
Subscribe user metadata
Subscribe to user metadata update events for a specific user.
Method
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
userid | String | yes | Unique user identifier. | |
resultCallback | function | yes | callback function |
Basic Use
Response
None
Unsubscribe user metadata
Unsubscribe to user metadata update events for a specific user.
Method
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
userId | String | yes | Unique user identifier. | |
resultCallback | function | yese | callback function |
Basic Use
Response
None
User metadata event
it will occur when user's metadata are updated(Add
/Set
/Clear
/Update
/Delete
), You need to complete this procedure yourself, and then you can handle this event when you are subscribing user's metadata.
Method
Basic Use
Response
When this event occurs, you can recieve a uid which indicating whose metadata have changed and a type RtmMetadata
for the user which contains the following properties:
Property Name | Type | Description |
---|---|---|
items | Array( RtmMetadataItem ) | RtmMetadataItem type array which contains a single Key-Value metadata item for user |
majorRevision | number | The major revision for this user metadata |
Caution: It should be noted that the returned data contains the full amount of data of the current user,
Add
/Set
/Clear
/Update
/Delete
operation all will trigger this event, and you cannot distinguish which operation caused the current event. Need more features, you can use our new version 2.1.
Channel metadata
Set channel metadata
set the metadata of the channel.
Method
Property Name | Type | Description |
---|---|---|
items | List<RtmMetadataItem > | RtmMetadataItem type array which contains a single Key-Value metadata item for channel, see RtmMetadataItem for more information |
options | RtmMetadataOptions | add optional propeties for current channel metadata, see RtmMetadataOptions for more information |
resultCallback | function | callback function |
The RtmMetadataOptions
is a set of optional properties for operations, it contains the following properties:
Property Name | Type | Description |
---|---|---|
majorRevision | number | Revison control parametes. when the majorRevision you supplied is as same as the one in the storage, this operation will success. |
enableRecordTs | bool | When it is set to true , the final RtmMetadataItem will record the updating time automatically |
enableRecordUserId | bool | When it is set to true , the final RtmMetadataItem will record who have updated this item automatically |
Basic Use
Response
None
caution: This operation will
reset
all current meatadata of channel and set a new one for channel.
Add channel metadata
Add new metadata items to the channel.
Method
Property Name | Type | Description |
---|---|---|
items | List<(RtmMetadataItem > | RtmMetadataItem type array which contains a single Key-Value metadata item for channel, see RtmMetadataItem for more information |
options | RtmMetadataOptions | add optional propeties for current channel metadata, see RtmMetadataOptions for more information |
resultCallback | function | callback function |
Basic Use
Response
None
Caution: This operation will add new metadata items for current channel. It will report errors if the key of new
RtmMetadataItem
has already existed in the channel metadata.
Clear channel metadata
delete all metadata items of the channel.
Method
Property Name | Type | Description |
---|---|---|
options | RtmMetadataOptions | add optional propeties for current channel metadata, see RtmMetadataOptions for more information |
resultCallback | function | callback function |
Basic Use
Response
None
Caution: This operation will clean all metadata of the specific channel.
Update channel metadata
Update metadata items of the channel.
Method
Property Name | Type | Description |
---|---|---|
items | List<RtmMetadataItem> | RtmMetadataItem type array which contains a single Key-Value metadata item for channel, see RtmMetadataItem for more information |
options | RtmMetadataOptions | add optional propeties for current channel metadata, see RtmMetadataOptions for more information |
resultCallback | function | callback function |
Basic Use
Response
None
Caution : This operation can only valid for updating the existing metadata items, or it will report errors.
Delete channel metadata
delete metadata items of the channel.
Method
Property Name | Type | Description |
---|---|---|
items | List<RtmMetadataItem> | RtmMetadataItem type array which contains a single Key-Value metadata item for channel, see RtmMetadataItem for more information |
options | RtmMetadataOptions | add optional propeties for current channel metadata, see RtmMetadataOptions for more information |
resultCallback | function | callback function |
Basic Use
Response
None
Caution:This operation will always excute regardless of the existing of metadata items
Get channel metadata
get all metadata items of the channel.
Method
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
resultCallback | function | yes | callback function |
Basic Use
Response
Returns a type RtmMetadata
for the specific channel
which contains the following properties:
Property Name | Type | Description |
---|---|---|
items | List< RtmMetadataItem > | RtmMetadataItem type array which contains a single Key-Value metadatafor channel, see [RtmMetadataIte](1) for more information |
majorRevision | number | The major revision for this channel metadata |
Caution: If the specific
channel
have not set any user metadata yet, this operation will return a empty .
Listen channel metadata update event
When you join channel ,you will automatically Listen the Channel metadata Update Event
Channel metadata event
This occurs when channel's metadata are updated, You need to complete this procedure yourself, and then you can handle this event when you are in the channel
Method
Basic Use
Response
When this event occurs, you can recieve a type RtmMetadata
for the specific channel
which contains the following properties:
Property Name | Type | Description |
---|---|---|
items | List< RtmMetadataItem > | RtmMetadataItem type array which contains a single Key-Value metadata itemfor channel |
majorRevision | number | The major revision for this channel metadata |
Caution: It should be noted that the returned data contains the full amount of data of the current channel,
Add
/Set
/Clear
/Update
/Delete
operation all will trigger this event, and you cannot distinguish which operation caused the current event. Need more features, you can use our new version 2.1.