Group Chats like WhatsApp

Post here your questions about the Flash / Flex / Air API for SFS2X

Moderators: Lapo, Bax

im.aditya
Posts: 3
Joined: 03 Oct 2015, 06:31

Group Chats like WhatsApp

Postby im.aditya » 03 Oct 2015, 07:32

Consider a whatsapp like app which provides one-on-one private chats as well as group chats. I have following questions regarding the approach I am using to build the app :

1. I guess, a lot of developer would use XMPP for creating such app but since I am working with adobe air (Android & iOS) & as3, I am not able to find any supported library for xmpp in as3. I can create a native extension but that too will take some time & currently for version 1 I have decided to try smartfox apis for these functionalities. Is that a wrong choice ?? I know smartfox is used for a lot bigger projects such as MMOs & multiplayer games so just using it for chatting might be an overkill !!

2. So considering I CAN use smartfox for making a simple chat application (with 1-1 & group chat) this is how I am handling 1-1 chat :
- every user has a unique userid, and with that userid I login the user (as a guest) into the server
- all the users join a single room say my_app_room
- now to send a chat message to a single user (1-1 chat), I just send a private message to the user, since I know the userId of the user which is the userName of the user in smartfox server room
- this whole thing is working fine, my question is : Is this the right approach ? Consider that if the app will have 100s or 1000s of users online will it cause an performance issues or lag in message delivery ?

3. I have also tried buddy list api. I can add a user's friends as his/her buddies and then send buddy messages. But I think the above approach is quite simple & straight forward in a one-one chat scenario !!

4. Last question : I have no idea how to create a group chat !! If I create a room for every group a user has (similar to whatsapp) there will be a LOT of rooms. And a user will be involved only in 4-5 rooms at a time. Also, how am I going to create all the rooms : As the user logins, I will have to check if all the group chats that the user is a part of has a room or not, if not this user will create the room & join them. When other members of the room comes online, they will check if the room exists, & join the rooms or create if its not there. Then I can broadcast messages in a single room !!!! Is this the right approach to implement a group chat ? Can I run a loop and send private message to all the members of a group chat, without creating any rooms ??

5.Is there any method which can broadcast a message to an array of userids ?? That can solve the above issue !!

Thanks
User avatar
Lapo
Site Admin
Posts: 19676
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Group Chats like WhatsApp

Postby Lapo » 05 Oct 2015, 07:22

im.aditya wrote:Consider a whatsapp like app which provides one-on-one private chats as well as group chats. I have following questions regarding the approach I am using to build the app :

1. I guess, a lot of developer would use XMPP for creating such app but since I am working with adobe air (Android & iOS) & as3, I am not able to find any supported library for xmpp in as3. I can create a native extension but that too will take some time & currently for version 1 I have decided to try smartfox apis for these functionalities. Is that a wrong choice ?? I know smartfox is used for a lot bigger projects such as MMOs & multiplayer games so just using it for chatting might be an overkill !!

No, I don't think so. SmartFoxServer is a light application that can run with 64MB of RAM on almost any hardware, so definitely not an overkill :)

2. So considering I CAN use smartfox for making a simple chat application (with 1-1 & group chat) this is how I am handling 1-1 chat :
- every user has a unique userid, and with that userid I login the user (as a guest) into the server
- all the users join a single room say my_app_room
- now to send a chat message to a single user (1-1 chat), I just send a private message to the user, since I know the userId of the user which is the userName of the user in smartfox server room
- this whole thing is working fine, my question is : Is this the right approach ? Consider that if the app will have 100s or 1000s of users online will it cause an performance issues or lag in message delivery ?

I am not sure why you would need to join all users in the same Room.
Only people in the same "group chat" should go in the same Room, so that they can read each other messages.

I would recommend using the Buddy List API to keep track of user's presence and one-on-one messaging.
http://docs2x.smartfoxserver.com/Advanc ... y-list-api

3. I have also tried buddy list api. I can add a user's friends as his/her buddies and then send buddy messages. But I think the above approach is quite simple & straight forward in a one-one chat scenario !!

This is much better because it doesn't require anyone to join any Room which is unnecessary unless you want to chat with a group of people.

I suggest to use BuddyLists to keep track of all your friends and one-on-one chats. When a group of people want to chat together than you can join them into a private Room(password protected) so that only they can access it.

4. Last question : [b]I have no idea how to create a group chat !! If I create a room for every group a user has (similar to whatsapp) there will be a LOT of rooms. And a user will be involved only in 4-5 rooms at a time. Also, how am I going to create all the rooms : As the user logins, I will have to check if all the group chats that the user is a part of has a room or not, if not this user will create the room & join them. When other members of the room comes online, they will check if the room exists, & join the rooms or create if its not there. Then I can broadcast messages in a single room !!!! Is this the right approach to implement a group chat ? Can I run a loop and send private message to all the members of a group chat, without creating any rooms ??

I think Rooms is a good way to manage the group chat. Rooms are very lightweight objects, they take up very little memory and you can literally have tens of thousands of them without any worries.

What you have described makes sense to me. At runtime you can check if a certain chat Room is available and if not you can create it and then join people inside.

5.Is there any method which can broadcast a message to an array of userids ?? That can solve the above issue !!

Well from server side it is pretty easy from server side code,

Code: Select all

int[] arrayOfUserIds = new int[] {10,20,30,40,50};
List<User> targets = new LinkedList<User>();

for (int i=0; i < arrayOfUserIds.length; i++)
{
   User user = getParentZone().getUserById(arrayOfUserIds[i]);
   
   if (user != null)
      targets.add(user);
      
   SFSObject messageObj = new SFSObject();
   messageObj.putUtfString("text", "Hello everyone!");
      
   send("message", messageObj, targets);
}


But I don't think this entirely solves the problem of not using Rooms.
Rooms allow you to know how has joined the group chat because you join them and leave them, thus generating their respective events. This is quite useful to see who has joined, who has left etc... Also the server already provides you with a Room List system and even with Room Groups which allow you to hide/unhide certain Rooms from the user's view.

I still think it is much easier to use Rooms and I don't see downsides to this approach.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
im.aditya
Posts: 3
Joined: 03 Oct 2015, 06:31

Re: Group Chats like WhatsApp

Postby im.aditya » 08 Jan 2016, 15:08

Sorry for the late reply.. I got caught up in some other modules of the app and I have started working on the Group Chat module now. I am having some issues, hence I am adding here.

This is my current scenario :

- every user has a list of room ids (corresponding to every group chat he is part of) which are unique strings.
- now as the user goes online and login the server, I run a loop and check if there exists a room with the same roomId, if yes then the user joins the room otherwise it creates the room and joins it.
- hence at the end of the loop the user would have joined all the rooms
- now the other user's will follow the same steps & finally they can have a group chat through public messages in a same room

Here is the pseudo code on the client side :

Code: Select all

for (var j:int = 0; j < groupChatRoomIds.length; j++)
{
   var id:String = groupChatRoomIds[j];
   
   if (_sfx.roomManager.containsRoom(id))
   {
      //Room exists | Joining..
      for (var k:int = 0; k < _sfx.joinedRooms.length; k++)
      {
         if ((_sfx.joinedRooms[k] as Room).name == id)
         {
            //Already joined..
            return;
         }
      }
      
      _sfx.send(new JoinRoomRequest(id, null, -1));
   }
   else
   {
      //Creating new room..
      createRoom(id);
   }
}

private function createRoom(roomID:String):void
{
   if (_sfx.roomManager.containsRoom(roomID))
   {
      //Room exists..
      return;
   }
   
   var settings:RoomSettings = new RoomSettings(roomID);
   _sfx.send(new CreateRoomRequest(settings, true));
}


Problem :

- Sometimes the containsRoom() function does not show/reflect all the existing rooms, hence it goes to 'createRoom' function and I get an error, saying the room already exists.
- I have a list of 50 or more roomIds to (create or if exists) join, so is this the right approach to call the 'joinRoomRequest' function in a loop. Is there any other way where I can pass an array of roomIds which I can join simultaneously ???
User avatar
Lapo
Site Admin
Posts: 19676
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Group Chats like WhatsApp

Postby Lapo » 11 Jan 2016, 08:40

Hi,
- Sometimes the containsRoom() function does not show/reflect all the existing rooms, hence it goes to 'createRoom' function and I get an error, saying the room already exists.

It would be best to run this code on the server side rather than the client side, otherwise you will have a hard time synchronizing the room creation phase.
What I mean is that two users running the same code almost at the same time will both notice that a certain "Room X" doesn't exist and concurrently send the same creation request. This will result in one of the two users to get a creation error (because the other user's request got the server first)

On the server side you can either synchronize the task using a lock or handle the creation error by simply skipping it and continue with the creation.

As regards joining Rooms in bulk there isn't such option, but you could do it via Extension. You send the list of IDs to your server code which in turn loops through all of them and joins the user in each Room. It will save you a considerable amount of bandwidth from the server side.

hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
im.aditya
Posts: 3
Joined: 03 Oct 2015, 06:31

Re: Group Chats like WhatsApp

Postby im.aditya » 11 Jan 2016, 17:36

Thanks for the reply Lapo.

Return to “SFS2X ActionScript 3 API”

Who is online

Users browsing this forum: No registered users and 3 guests