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/ChatThread.h"
9#include "Channel/Filter.h"
10#include "ChannelFlags.h"
11#include "ChatSocketEvents.h"
12#include "Components/ActorComponent.h"
13#include "CoreMinimal.h"
14#include "Engine/LatentActionManager.h"
15#include "Event/Notification/NotificationMutesUpdatedEvent.h"
16#include "IChatSocket.h"
17#include "Moderation/BanPaginationOptions.h"
18#include "Moderation/UserBlock.h"
19#include "PaginationOptions.h"
20#include "Templates/IsInvocable.h"
21#include "User/OwnUser.h"
22#include "User/User.h"
23
24#include "StreamChatClientComponent.generated.h"
25
26struct FBan;
27class FChatApi;
28class FTokenManager;
29class ITokenProvider;
30class UChatChannel;
31struct FUserRef;
34struct FMessage;
35struct FDevice;
37
43UCLASS(ClassGroup = (Stream), meta = (BlueprintSpawnableComponent))
44class STREAMCHAT_API UStreamChatClientComponent final : public UActorComponent
45{
46 GENERATED_BODY()
47
48public:
51
59 void ConnectUser(const FUser& User, TUniquePtr<ITokenProvider> TokenProvider, TFunction<void(const FOwnUser&)> Callback = {});
60
68 void ConnectUser(const FUser& User, const FString& Token, TFunction<void(const FOwnUser&)> Callback = {});
69
75 void ConnectAnonymousUser(TFunction<void(const FOwnUser&)> Callback = {});
76
83 void ConnectGuestUser(const FUser& User, TFunction<void(const FOwnUser&)> Callback = {});
84
88 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
89 void DisconnectUser();
90
94 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
95 void UpsertUsers(const TArray<FUser>& Users);
96
98 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
99 UChatChannel* NewChat();
101 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
102 void CancelNewChat();
103
115 void QueryChannels(
116 TOptional<FFilter> Filter = {},
117 const TArray<FChannelSortOption>& SortOptions = {},
119 const FPaginationOptions& PaginationOptions = {},
120 TFunction<void(const TArray<UChatChannel*>&)> Callback = {});
121
128 void CreateChannel(const FChannelProperties& ChannelProperties, TFunction<void(UChatChannel*)> Callback);
129
137 void WatchChannel(const FChannelProperties& ChannelProperties, TFunction<void(UChatChannel*)> Callback);
138
147 void QueryChannel(const FChannelProperties& ChannelProperties, const EChannelFlags Flags, TFunction<void(UChatChannel*)> Callback = {});
148
156 void QueryAdditionalChannels(int32 Limit = 10, TFunction<void()> Callback = {});
157
167 void QueryUsers(
168 const FFilter& Filter = {},
169 const TArray<FUserSortOption>& Sort = {},
170 bool bPresence = true,
171 const TOptional<FPaginationOptions> PaginationOptions = {},
172 TFunction<void(const TArray<FUserRef>&)> Callback = {}) const;
173
186 void SearchMessages(
187 const FFilter& ChannelFilter = {},
188 const TOptional<FString>& Query = {},
189 const TOptional<FFilter>& MessageFilter = {},
190 const TArray<FMessageSortOption>& Sort = {},
191 TOptional<uint32> MessageLimit = {},
192 TFunction<void(const TArray<FMessage>&)> Callback = {}) const;
193
210 void QueryThreads(
211 const TOptional<FFilter>& Filter = {},
212 const TArray<FThreadSortOption>& SortOptions = {},
213 TOptional<uint32> Limit = {},
214 TOptional<uint32> ReplyLimit = {},
215 const TOptional<FString>& Next = {},
216 TFunction<void(const TArray<FChatThread>& Threads, const FString& NextPage)> Callback = {}) const;
217
228 void GetThread(
229 const FString& ParentMessageId,
230 TOptional<uint32> ReplyLimit = {},
231 TOptional<uint32> ParticipantLimit = {},
232 TFunction<void(const FChatThread&)> Callback = {}) const;
233
237 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client")
238 void MarkAllRead() const;
239
240 UFUNCTION(BlueprintPure, Category = "Stream Chat|Client")
241 const TArray<UChatChannel*>& GetChannels() const;
242
243 UFUNCTION(BlueprintPure, Category = "Stream Chat|Client")
244 static FString DevToken(const FString& UserId);
245
251 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
252 void AddDevice(const FString& DeviceId, EPushProvider PushProvider) const;
253
258 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
259 void RemoveDevice(const FString& DeviceId) const;
260
265 void ListDevices(TFunction<void(TArray<FDevice>)> Callback) const;
266
267 UPROPERTY(EditAnywhere, Config, Category = "Stream Chat", meta = (DisplayName = "API Key"))
268 FString ApiKey;
269
270 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FChannelsUpdatedDelegate, const TArray<UChatChannel*>&, Channels);
271
273 UPROPERTY(BlueprintAssignable, Category = "Stream Chat|Channel")
274 FChannelsUpdatedDelegate ChannelsUpdated;
275
276private:
277 // Called when the game starts
278 virtual void BeginPlay() override;
279
280 void ConnectUserInternal(const FUser& User, TFunction<void(const FOwnUser&)> Callback);
281 UChatChannel* CreateChannelObject(const FChannelStateResponseFieldsDto&);
282
283 void OnConnectionRecovered(const FConnectionRecoveredEvent&);
284 void OnUserPresenceChanged(const FUserPresenceChangedEvent&);
285 void OnNewMessage(const FMessageNewEvent&);
286 void OnMutesUpdated(const FNotificationMutesUpdatedEvent&);
287
288 void AddChannels(const TArray<UChatChannel*>& InChannels);
289
290 TSharedPtr<FTokenManager> TokenManager;
291 TSharedPtr<FChatApi> Api;
292
293 UPROPERTY(Transient)
294 TArray<UChatChannel*> Channels;
295
296#pragma region Blueprint
301public:
308 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
309 void ConnectUser(const FUser& User, const FString& Token, const UObject* WorldContextObject, FLatentActionInfo LatentInfo, FOwnUser& OutUser);
310
315 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
316 void ConnectAnonymousUser(const UObject* WorldContextObject, FLatentActionInfo LatentInfo, FOwnUser& OutUser);
317
323 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
324 void ConnectGuestUser(const FUser& User, const UObject* WorldContextObject, FLatentActionInfo LatentInfo, FOwnUser& OutUser);
325
336 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
337 void QueryChannels(
338 FFilter Filter,
339 const TArray<FChannelSortOption>& SortOptions,
340 const UObject* WorldContextObject,
341 FLatentActionInfo LatentInfo,
342 TArray<UChatChannel*>& OutChannels,
343 UPARAM(meta = (Bitmask, BitmaskEnum = EChannelFlags)) const int32 Flags = 3);
344
352 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo, AdvancedDisplay = Id))
353 void WatchChannel(const FChannelProperties& ChannelProperties, const UObject* WorldContextObject, FLatentActionInfo LatentInfo, UChatChannel*& OutChannel);
354
356#pragma endregion Blueprint
357
358#pragma region Events
363public:
364 template <class TEvent>
365 using TEventMulticastDelegate = TMulticastDelegate<void(const TEvent& Event)>;
366 template <class TEvent>
367 using TEventDelegate = typename TEventMulticastDelegate<TEvent>::FDelegate;
368 template <class TEvent, class UserClass>
369 using TEventDelegateUObjectMethodPtr = typename TEventDelegate<TEvent>::template TMethodPtr<UserClass>;
370 template <class TEvent, class UserClass>
371 using TEventDelegateSpMethodPtr = typename TEventDelegate<TEvent>::template TMethodPtr<UserClass>;
372
374 template <class TEvent>
375 FDelegateHandle On(TEventDelegate<TEvent> Callback);
376
384 template <class TEvent, class UserClass>
385 typename TEnableIf<TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type On(
386 UserClass* Obj,
387 TEventDelegateUObjectMethodPtr<TEvent, UserClass> Method);
388
396 template <class TEvent, class UserClass>
397 typename TEnableIf<!TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type On(
398 UserClass* Obj,
399 TEventDelegateSpMethodPtr<TEvent, UserClass> Method);
400
409 template <class TEvent, typename FunctorType, typename... VarTypes>
410 typename TEnableIf<TIsInvocable<FunctorType, const TEvent&, VarTypes...>::Value, FDelegateHandle>::Type On(FunctorType&& Functor, VarTypes... Vars);
411
412 template <class TEvent>
413 bool Unsubscribe(FDelegateHandle) const;
414
415private:
416 TSharedPtr<IChatSocket> Socket;
417
419#pragma endregion Events
420
421#pragma region Moderation
426public:
436 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client|Moderation", DisplayName = "Ban User")
437 void BanUserBP(const FUserRef& User, FTimespan Timeout, const FString& Reason, bool bIpBan) const;
438 void BanUser(const FUserRef& User, const TOptional<FTimespan>& Timeout = {}, const TOptional<FString>& Reason = {}, bool bIpBan = false) const;
439
444 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client|Moderation")
445 void UnbanUser(const FUserRef& User) const;
446
454 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client|Moderation", DisplayName = "Shadow Ban User")
455 void ShadowBanUserBP(const FUserRef& User, FTimespan Timeout) const;
456 void ShadowBanUser(const FUserRef& User, const TOptional<FTimespan>& Timeout = {}) const;
457
462 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client|Moderation")
463 void ShadowUnbanUser(const FUserRef& User) const;
464
474 void QueryBannedUsers(
475 const FFilter& Filter,
476 const TArray<FBanSortOption>& SortOptions = {},
477 const TOptional<FBanPaginationOptions> PaginationOptions = {},
478 const TFunction<void(const TArray<FBan>&)> Callback = {});
479
490 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client|Moderation")
491 void FlagMessage(const FMessage& Message, const FString& Reason, const TMap<FString, FString>& CustomData) const;
492
495 void FlagMessage(const FMessage& Message, const FString& Reason = TEXT("")) const;
496
502 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client|Moderation")
503 void UnflagMessage(const FMessage& Message) const;
504
512 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client|Moderation")
513 void FlagUser(const FUserRef& User, const FString& Reason, const TMap<FString, FString>& CustomData) const;
514
516 void FlagUser(const FUserRef& User, const FString& Reason = TEXT("")) const;
517
523 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client|Moderation")
524 void UnflagUser(const FUserRef& User) const;
525
532 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client", DisplayName = "Mute User")
533 void MuteUserBP(const FUserRef& User, FTimespan Timeout) const;
534 void MuteUser(const FUserRef& User, const TOptional<FTimespan>& Timeout = {}) const;
535
541 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
542 void UnmuteUser(const FUserRef& User) const;
543
552 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client|Moderation")
553 void BlockUser(const FUserRef& User) const;
554
560 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client|Moderation")
561 void UnblockUser(const FUserRef& User) const;
562
572 void GetBlockedUsers(TFunction<void(const TArray<FUserBlock>&)> Callback = {}) const;
573
575#pragma endregion Moderation
576};
577
578#pragma region Events
579
580template <class TEvent>
581FDelegateHandle UStreamChatClientComponent::On(TEventDelegate<TEvent> Callback)
582{
583 return Socket->Events().SubscribeLambda<TEvent>(
584 [this, Callback](const TEvent& Event)
585 {
586 // TODO can we enforce client-only events?
587 Callback.ExecuteIfBound(Event);
588 });
589}
590
591template <class TEvent, class UserClass>
592typename TEnableIf<TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type UStreamChatClientComponent::On(
593 UserClass* Obj,
594 TEventDelegateUObjectMethodPtr<TEvent, UserClass> Method)
595{
596 const TEventDelegate<TEvent> Delegate = TEventDelegate<TEvent>::CreateUObject(Obj, Method);
597 return On<TEvent>(Delegate);
598}
599
600template <class TEvent, class UserClass>
601typename TEnableIf<!TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type UStreamChatClientComponent::On(
602 UserClass* Obj,
603 TEventDelegateSpMethodPtr<TEvent, UserClass> Method)
604{
605 const TEventDelegate<TEvent> Delegate = TEventDelegate<TEvent>::CreateSP(Obj, Method);
606 return On<TEvent>(Delegate);
607}
608
609template <class TEvent, typename FunctorType, typename... VarTypes>
610typename TEnableIf<TIsInvocable<FunctorType, const TEvent&, VarTypes...>::Value, FDelegateHandle>::Type UStreamChatClientComponent::On(
611 FunctorType&& Functor,
612 VarTypes... Vars)
613{
614 const TEventDelegate<TEvent> Delegate = TEventDelegate<TEvent>::CreateLambda(Forward<FunctorType>(Functor), Vars...);
615 return On<TEvent>(Delegate);
616}
617
618template <class TEvent>
619bool UStreamChatClientComponent::Unsubscribe(const FDelegateHandle Handle) const
620{
621 return Socket->Events().Unsubscribe<TEvent>(Handle);
622}
623#pragma endregion Events
All Stream Chat REST API requests can be made via this object.
Definition ChatApi.h:67
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:45
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:581
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.
@ ParentMessageId
The ID of the message which started the thread.
@ 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:238
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
Represents a Stream Chat thread: a message and the replies hanging off it.
Definition ChatThread.h:26
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:70
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
One user blocking another.
Definition UserBlock.h:23
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