I’m new here and I’m trying to create a simple chat app but I find myself unable to get live updates for channels and messages.
I have tried using live collections for this and I get the right data on the first call, but for instance, in the case of messages, if I send a message as a different user I don’t see the message listed in the other device unless I call again liveMessages manually.
Is this how it’s supposed to work or I’m missing something?
Are live collections working for everybody else? I’m still stuck because I can’t find a way to make my app update live as I create and send messages from different devices. I’m using Expo for my App. I’d really appreciate if somebody can help me out with this.
I can see how messages in my live channels update inside my Amity Console but websocket events seem to not get triggered inside my RN app. Has anybody else experienced something similar?
Hello @nockingTheArrow To confirm my understanding, you mentioned that you have to call liveMessages again in order to see your messages on another device, is that correct? Could you please provide me with your relevant code and current SDK version?
I thought the app would get subscribed to channel messages updates when calling the liveMessages method but I see now we have to do this manually.
The same is happening for me when trying to keep the channels users are members of listed. If I add or remove users from channels I don’t the list updating live so I guess there’s a topic I need to subscribe to.
Is there somewhere in the docs where I can see the topics I can subscribe to and the events they emit?
I’m in the process of updating the docs regarding such issues. For now I can share a detailed usage for live channels, here.
import { ChannelRepository, subscribeTopic, getChannelTopic } from '@amityco/ts-sdk';
import { FC, useEffect, useState } from 'react';
const disposers: Amity.Unsubscriber[] = [];
const subscribedChannels: Amity.Channel['channelId'][] = [];
const subscribeChannels = (channels: Amity.Channel[]) =>
channels.forEach(c => {
if (!subscribedChannels.includes(c.channelId) && !c.isDeleted) {
subscribedChannels.push(c.channelId);
disposers.push(subscribeTopic(getChannelTopic(c)));
}
});
const GetChannels: FC = () => {
const [channels, setChannels] = useState<Amity.Channel[]>();
useEffect(() => {
/*
* Possible params for liveChannels
* displayName?: string;
* membership?: 'all' | 'member' | 'notMember';
* sortBy?: 'displayName' | 'firstCreated' | 'lastCreated';
* types?: ['live', 'conversation', 'community', 'broadcast'];
* isDeleted?: boolean;
* tags?: ['tag1', 'tag2']
* excludeTags?: ['tag3', 'tag4']
* limit?: number
* }
*/
const unsubscribe = ChannelRepository.getChannels(
{},
({ data: channels, onNextPage, hasNextPage, loading, error }) => {
setChannels(channels);
/*
* this is only required if you want real time updates for each
* channel in the collection
*/
subscribeChannels(channels);
},
);
/*
* if you only wish to get a collection or list of paginated channel without
* any real time updates you can unsubscribe immediately after you call the
* collection.
* ex: unsubscribe()
*/
disposers.push(unsubscribe);
return () => {
disposers.forEach(fn => fn());
};
});
return null;
};
Please reach out if you have further questions.
This is the approach I ended up following for chats and it’s working. I was wondering what topic should I subscribe users to if I want them to receive updates when they’re added to new chats. Currently this works for keeping the chat list they get on their first query updated but I don’t think they’ll have new chats pop up in their list as they’re added.
Edit: I want to know if there’s a topic I can subscribe to in order to have users’ list of live channels updated whenever they get added or removed from any.
This was the first problem I was trying to solve, I solved it by subscribing to the subchannel topic as you pointed out in a different thread and then I started getting them as they were created. Currently it seems like there are some issues with subscriptions because I can’t subscribe to any topic, not even the topics it automatically subscribes to when you login users to Amity.
I get that and it’s what I’m currently doing and it works fine for the first query, but then, as I’m using channels of type “Live”, I don’t get any update on the list when adding or removing users from them. I wonder if I can do this without having to subscribe to each channel retrieved in the list one by one.