Chat object
Once you initialize the Unity Chat SDK and create its instance, you'll get immediate access to the Chat
entity from which you can call the PubNub server.
To communicate with PubNub, you can use various methods. For example, you can use DeleteChannel()
to remove a given channel (chat.DeleteChannel("support")
) or WherePresent()
to check which channels a given user is subscribed to (chat.WherePresent("support_agent_15")
).
By calling methods on the Chat
entity, you create chat objects like Channel
, User
, Message
, Membership
, ThreadChannel
, and ThreadMessage
. These objects also expose Chat API under various methods, letting you perform CRUD operations on messages, channels, users, the related user-channel membership, and many more.
Each of these entities comes with a set of "read-only" parameters you define when creating, modifying, and deleting specific channels, users, messages, or memberships in your chat app.
Properties
The Chat
object has the following properties:
public Chat(PubnubChatConfig config)
...
public class PubnubChatConfig
{
public string PublishKey { get; set; }
public string SubscribeKey { get; set; }
public string UserId { get; set; }
public string AuthKey { get; set; }
}
Parameter | Type | Description |
---|---|---|
PublishKey | string | publishKey from the Admin Portal (only required if publishing). |
SubscribeKey | string | subscribeKey from the Admin Portal. |
UserId | string | A unique identifier for the user or the device that connects to PubNub. It's a UTF-8 encoded string of up to 92 alphanumeric characters. |
AuthKey | string | Authentication key for Access Manager. Use it for all requests made to PubNub APIs to authenticate users in your application and grant them access to PubNub resources (other users' metadata and channels) |
To initialize the Unity Chat SDK, you must provide these three parameters: PublishKey
, SubscribeKey
, and UserId
.
Events
The Chat
object has the following events:
// Event triggered when a potentially offensive message is reported by a user
public event Action<ReportEvent> OnReportEvent;
// Event triggered when a user is banned or muted or when these restrictions are lifted by an admin
public event Action<ModerationEvent> OnModerationEvent;
// Event triggered when a user is typing a message on a channel
public event Action<ChatEvent> OnTypingEvent;
// Event triggered when others have read a messages you published on a channel
public event Action<ChatEvent> OnReceiptEvent;
// Event triggered when a user is mentioned in a published message
public event Action<ChatEvent> OnMentionEvent;
// Event triggered when a user is invited to join a channel
public event Action<ChatEvent> OnInviteEvent;
// Event triggered for a custom action
public event Action<ChatEvent> OnCustomEvent;
// Base event that is generated each time any other event type is generated
show all 16 linesExample
Get an event that is triggered when a user reports a potentially offensive message.
chat.OnReportEvent += (chat) =>
{
Console.WriteLine("New message reported!");
};
Methods
You can call the following methods on the Chat
object.
Click on each method for more details.
CreateUser()
CreateDirectConversation()
CreateGroupConversation()
CreatePublicConversation()
DeleteChannel()
DeleteUser()
EmitEvent()
GetEventsHistory()
TryGetChannel()
GetChannelMemberships()
GetChannels()
GetChannelSuggestions()
GetCurrentUserMentions()
GetUserMemberships()
TryGetThreadChannel()
TryGetCurrentUser()
TryGetUser()
GetUsers()
GetUserSuggestions()
getUnreadMessagesCounts()
IsPresent()
StartListeningForUpdates
StartListeningForCustomEvents()
StartListeningForInviteEvents()
StartListeningForReportEvents()
StartListeningForMentionEvents()
StartListeningForModerationEvents()
markAllMessagesAsRead
SetRestriction()
UpdateChannel()
UpdateUser()
WherePresent()
WhoIsPresent()
Use case
For example, you can use the Chat
object methods to: