Stream Chat Unreal SDK
Loading...
Searching...
No Matches
StreamChatClientComponent.h
1// Copyright 2026 Stream.IO, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Channel/ChannelProperties.h"
6#include "Channel/ChannelSortOption.h"
7#include "Channel/ChatChannel.h"
8#include "Channel/Filter.h"
9#include "ChannelFlags.h"
10#include "ChatSocketEvents.h"
11#include "Components/ActorComponent.h"
12#include "CoreMinimal.h"
13#include "Engine/LatentActionManager.h"
14#include "Event/Notification/NotificationMutesUpdatedEvent.h"
15#include "IChatSocket.h"
16#include "Moderation/BanPaginationOptions.h"
17#include "PaginationOptions.h"
18#include "Templates/IsInvocable.h"
19#include "User/OwnUser.h"
20#include "User/User.h"
21
22#include "StreamChatClientComponent.generated.h"
23
24struct FBan;
25class FChatApi;
26class FTokenManager;
27class ITokenProvider;
28class UChatChannel;
29struct FUserRef;
32struct FMessage;
33struct FDevice;
35
41UCLASS(ClassGroup = (Stream), meta = (BlueprintSpawnableComponent))
42class STREAMCHAT_API UStreamChatClientComponent final : public UActorComponent
43{
44 GENERATED_BODY()
45
46public:
49
57 void ConnectUser(const FUser& User, TUniquePtr<ITokenProvider> TokenProvider, TFunction<void(const FOwnUser&)> Callback = {});
58
66 void ConnectUser(const FUser& User, const FString& Token, TFunction<void(const FOwnUser&)> Callback = {});
67
73 void ConnectAnonymousUser(TFunction<void(const FOwnUser&)> Callback = {});
74
81 void ConnectGuestUser(const FUser& User, TFunction<void(const FOwnUser&)> Callback = {});
82
86 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
87 void DisconnectUser();
88
92 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
93 void UpsertUsers(const TArray<FUser>& Users);
94
96 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
97 UChatChannel* NewChat();
99 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
100 void CancelNewChat();
101
113 void QueryChannels(
114 TOptional<FFilter> Filter = {},
115 const TArray<FChannelSortOption>& SortOptions = {},
117 const FPaginationOptions& PaginationOptions = {},
118 TFunction<void(const TArray<UChatChannel*>&)> Callback = {});
119
126 void CreateChannel(const FChannelProperties& ChannelProperties, TFunction<void(UChatChannel*)> Callback);
127
135 void WatchChannel(const FChannelProperties& ChannelProperties, TFunction<void(UChatChannel*)> Callback);
136
145 void QueryChannel(const FChannelProperties& ChannelProperties, const EChannelFlags Flags, TFunction<void(UChatChannel*)> Callback = {});
146
154 void QueryAdditionalChannels(int32 Limit = 10, TFunction<void()> Callback = {});
155
165 void QueryUsers(
166 const FFilter& Filter = {},
167 const TArray<FUserSortOption>& Sort = {},
168 bool bPresence = true,
169 const TOptional<FPaginationOptions> PaginationOptions = {},
170 TFunction<void(const TArray<FUserRef>&)> Callback = {}) const;
171
184 void SearchMessages(
185 const FFilter& ChannelFilter = {},
186 const TOptional<FString>& Query = {},
187 const TOptional<FFilter>& MessageFilter = {},
188 const TArray<FMessageSortOption>& Sort = {},
189 TOptional<uint32> MessageLimit = {},
190 TFunction<void(const TArray<FMessage>&)> Callback = {}) const;
191
195 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client")
196 void MarkAllRead() const;
197
198 UFUNCTION(BlueprintPure, Category = "Stream Chat|Client")
199 const TArray<UChatChannel*>& GetChannels() const;
200
201 UFUNCTION(BlueprintPure, Category = "Stream Chat|Client")
202 static FString DevToken(const FString& UserId);
203
209 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
210 void AddDevice(const FString& DeviceId, EPushProvider PushProvider) const;
211
216 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
217 void RemoveDevice(const FString& DeviceId) const;
218
223 void ListDevices(TFunction<void(TArray<FDevice>)> Callback) const;
224
225 UPROPERTY(EditAnywhere, Config, Category = "Stream Chat", meta = (DisplayName = "API Key"))
226 FString ApiKey;
227
228 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FChannelsUpdatedDelegate, const TArray<UChatChannel*>&, Channels);
229
231 UPROPERTY(BlueprintAssignable, Category = "Stream Chat|Channel")
232 FChannelsUpdatedDelegate ChannelsUpdated;
233
234private:
235 // Called when the game starts
236 virtual void BeginPlay() override;
237
238 void ConnectUserInternal(const FUser& User, TFunction<void(const FOwnUser&)> Callback);
239 UChatChannel* CreateChannelObject(const FChannelStateResponseFieldsDto&);
240
241 void OnConnectionRecovered(const FConnectionRecoveredEvent&);
242 void OnUserPresenceChanged(const FUserPresenceChangedEvent&);
243 void OnNewMessage(const FMessageNewEvent&);
244 void OnMutesUpdated(const FNotificationMutesUpdatedEvent&);
245
246 void AddChannels(const TArray<UChatChannel*>& InChannels);
247
248 TSharedPtr<FTokenManager> TokenManager;
249 TSharedPtr<FChatApi> Api;
250
251 UPROPERTY(Transient)
252 TArray<UChatChannel*> Channels;
253
254#pragma region Blueprint
259public:
266 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
267 void ConnectUser(const FUser& User, const FString& Token, const UObject* WorldContextObject, FLatentActionInfo LatentInfo, FOwnUser& OutUser);
268
273 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
274 void ConnectAnonymousUser(const UObject* WorldContextObject, FLatentActionInfo LatentInfo, FOwnUser& OutUser);
275
281 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
282 void ConnectGuestUser(const FUser& User, const UObject* WorldContextObject, FLatentActionInfo LatentInfo, FOwnUser& OutUser);
283
294 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
295 void QueryChannels(
296 FFilter Filter,
297 const TArray<FChannelSortOption>& SortOptions,
298 const UObject* WorldContextObject,
299 FLatentActionInfo LatentInfo,
300 TArray<UChatChannel*>& OutChannels,
301 UPARAM(meta = (Bitmask, BitmaskEnum = EChannelFlags)) const int32 Flags = 3);
302
310 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo, AdvancedDisplay = Id))
311 void WatchChannel(const FChannelProperties& ChannelProperties, const UObject* WorldContextObject, FLatentActionInfo LatentInfo, UChatChannel*& OutChannel);
312
314#pragma endregion Blueprint
315
316#pragma region Events
321public:
322 template <class TEvent>
323 using TEventMulticastDelegate = TMulticastDelegate<void(const TEvent& Event)>;
324 template <class TEvent>
325 using TEventDelegate = typename TEventMulticastDelegate<TEvent>::FDelegate;
326 template <class TEvent, class UserClass>
327 using TEventDelegateUObjectMethodPtr = typename TEventDelegate<TEvent>::template TMethodPtr<UserClass>;
328 template <class TEvent, class UserClass>
329 using TEventDelegateSpMethodPtr = typename TEventDelegate<TEvent>::template TMethodPtr<UserClass>;
330
332 template <class TEvent>
333 FDelegateHandle On(TEventDelegate<TEvent> Callback);
334
342 template <class TEvent, class UserClass>
343 typename TEnableIf<TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type On(
344 UserClass* Obj,
345 TEventDelegateUObjectMethodPtr<TEvent, UserClass> Method);
346
354 template <class TEvent, class UserClass>
355 typename TEnableIf<!TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type On(
356 UserClass* Obj,
357 TEventDelegateSpMethodPtr<TEvent, UserClass> Method);
358
367 template <class TEvent, typename FunctorType, typename... VarTypes>
368 typename TEnableIf<TIsInvocable<FunctorType, const TEvent&, VarTypes...>::Value, FDelegateHandle>::Type On(FunctorType&& Functor, VarTypes... Vars);
369
370 template <class TEvent>
371 bool Unsubscribe(FDelegateHandle) const;
372
373private:
374 TSharedPtr<IChatSocket> Socket;
375
377#pragma endregion Events
378
379#pragma region Moderation
384public:
394 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client|Moderation", DisplayName = "Ban User")
395 void BanUserBP(const FUserRef& User, FTimespan Timeout, const FString& Reason, bool bIpBan) const;
396 void BanUser(const FUserRef& User, const TOptional<FTimespan>& Timeout = {}, const TOptional<FString>& Reason = {}, bool bIpBan = false) const;
397
402 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client|Moderation")
403 void UnbanUser(const FUserRef& User) const;
404
412 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client|Moderation", DisplayName = "Shadow Ban User")
413 void ShadowBanUserBP(const FUserRef& User, FTimespan Timeout) const;
414 void ShadowBanUser(const FUserRef& User, const TOptional<FTimespan>& Timeout = {}) const;
415
420 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client|Moderation")
421 void ShadowUnbanUser(const FUserRef& User) const;
422
432 void QueryBannedUsers(
433 const FFilter& Filter,
434 const TArray<FBanSortOption>& SortOptions = {},
435 const TOptional<FBanPaginationOptions> PaginationOptions = {},
436 const TFunction<void(const TArray<FBan>&)> Callback = {});
437
443 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client")
444 void FlagMessage(const FMessage& Message) const;
445
451 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client")
452 void FlagUser(const FUserRef& User) const;
453
460 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client", DisplayName = "Mute User")
461 void MuteUserBP(const FUserRef& User, FTimespan Timeout) const;
462 void MuteUser(const FUserRef& User, const TOptional<FTimespan>& Timeout = {}) const;
463
469 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
470 void UnmuteUser(const FUserRef& User) const;
471
480 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client|Moderation")
481 void BlockUser(const FUserRef& User) const;
482
488 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client|Moderation")
489 void UnblockUser(const FUserRef& User) const;
490
496 void GetBlockedUsers(TFunction<void(const TArray<FString>&)> Callback) const;
497
499#pragma endregion Moderation
500};
501
502#pragma region Events
503
504template <class TEvent>
505FDelegateHandle UStreamChatClientComponent::On(TEventDelegate<TEvent> Callback)
506{
507 return Socket->Events().SubscribeLambda<TEvent>(
508 [this, Callback](const TEvent& Event)
509 {
510 // TODO can we enforce client-only events?
511 Callback.ExecuteIfBound(Event);
512 });
513}
514
515template <class TEvent, class UserClass>
516typename TEnableIf<TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type UStreamChatClientComponent::On(
517 UserClass* Obj,
518 TEventDelegateUObjectMethodPtr<TEvent, UserClass> Method)
519{
520 const TEventDelegate<TEvent> Delegate = TEventDelegate<TEvent>::CreateUObject(Obj, Method);
521 return On<TEvent>(Delegate);
522}
523
524template <class TEvent, class UserClass>
525typename TEnableIf<!TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type UStreamChatClientComponent::On(
526 UserClass* Obj,
527 TEventDelegateSpMethodPtr<TEvent, UserClass> Method)
528{
529 const TEventDelegate<TEvent> Delegate = TEventDelegate<TEvent>::CreateSP(Obj, Method);
530 return On<TEvent>(Delegate);
531}
532
533template <class TEvent, typename FunctorType, typename... VarTypes>
534typename TEnableIf<TIsInvocable<FunctorType, const TEvent&, VarTypes...>::Value, FDelegateHandle>::Type UStreamChatClientComponent::On(
535 FunctorType&& Functor,
536 VarTypes... Vars)
537{
538 const TEventDelegate<TEvent> Delegate = TEventDelegate<TEvent>::CreateLambda(Forward<FunctorType>(Functor), Vars...);
539 return On<TEvent>(Delegate);
540}
541
542template <class TEvent>
543bool UStreamChatClientComponent::Unsubscribe(const FDelegateHandle Handle) const
544{
545 return Socket->Events().Unsubscribe<TEvent>(Handle);
546}
547#pragma endregion Events
All Stream Chat REST API requests can be made via this object.
Definition ChatApi.h:63
The client-side representation of a Stream Chat channel.
Definition ChatChannel.h:75
A component which allows for connecting to the Stream Chat API. Should be added to a client-side acto...
Definition StreamChatClientComponent.h:43
TEnableIf<!TIsDerivedFrom< UserClass, UObject >::IsDerived, FDelegateHandle >::Type On(UserClass *Obj, TEventDelegateSpMethodPtr< TEvent, UserClass > Method)
void ConnectUser(const FUser &User, const FString &Token, TFunction< void(const FOwnUser &)> Callback={})
Create a connection to the API for the given user and credentials.
void WatchChannel(const FChannelProperties &ChannelProperties, TFunction< void(UChatChannel *)> Callback)
Create a channel if it does not exist yet (if this user has the right permissions),...
void ConnectUser(const FUser &User, TUniquePtr< ITokenProvider > TokenProvider, TFunction< void(const FOwnUser &)> Callback={})
Create a connection to the API for the given user and using a custom token provider.
FDelegateHandle On(TEventDelegate< TEvent > Callback)
Subscribe to a client event using your own delegate object.
Definition StreamChatClientComponent.h:505
void ConnectAnonymousUser(TFunction< void(const FOwnUser &)> Callback={})
Create a connection to the API anonymously.
EChannelFlags
Additional channel actions to perform when requesting channel data from API.
Definition ChannelFlags.h:15
@ State
Return channel state in response.
@ Watch
Start watching the channel (receive events regarding this channel via WebSocket)
@ UserId
The user ID of the user that sent the message.
@ Id
Message ID.
@ Type
Type of the message.
@ Query
Matches values by performing text search with the specified value.
A banned user.
Definition Ban.h:17
The desired sort options for ban queries.
Definition ChannelSortOption.h:188
Encapsulates the static properties of a channel.
Definition ChannelProperties.h:21
The desired sort options for Channel API queries.
Definition ChannelSortOption.h:44
#/components/schemas/ChannelStateResponseFields
Definition ChannelStateResponseFieldsDto.h:19
Sent when the connection is recovered.
Definition ConnectionRecoveredEvent.h:17
A push notification device.
Definition Device.h:16
A filter used for querying channels.
Definition Filter.h:75
Represents a Stream Chat message.
Definition Message.h:66
A new message was created.
Definition MessageNewEvent.h:17
Notification when a new message was created.
Definition NotificationMutesUpdatedEvent.h:18
The currently connected Stream Chat user.
Definition OwnUser.h:18
Options for pagination.
Definition PaginationOptions.h:15
A Stream Chat user.
Definition User.h:20
#/components/schemas/UserPresenceChangedEvent
Definition UserPresenceChangedEvent.h:18
A reference to a unique User.
Definition UserRef.h:23