Android - Correctly Pairing And Connecting Two Users In A Random Chat App Using Parse And Pubnub
I'm currently creating a random chat application where the user presses a button and is paired with another user and then they can chat.I plan on using Parse for user control and f
Solution 1:
This is a cool use case. Here's some insight into how to get started with this design.
Setup
- Each user will be assigned a unique channel name and a unique channel group, for example,
ch_user123
andcg_user123
, respectively. - There will be three server managed channel groups named
cg_idle
,cg_searching
,cg_active
. This channel group will contain all the unique user channel names of users that are not actively engaged in a chat and not searching for a chat partner.
User Login
When a user logs in (successfully), your server will add that user's unique channel to the channel group idle
and to the user's unique channel group (IOW - add ch_user123
to cg_user123
and to cg_idle
Search of Chat Partner
When a user clicks the search for chat partner button, your server app will
- remove their user unique channel from the
cg_idle
channel group - add their user unique channel to the
cg_searching
channel group list_channels
of thecg_idle
to get a list of chat partner candidates- select a user channel from the list of
cg_idle
candidates - check to make sure the selected candidate is still idle
- if the user is no longer idle then they are active or searching - pick another user from the idle list (need to list the channels of
cg_idle
again to get an updated list) - IOW, go back to step 4 - if user still idle, remove that user's channel from
cg_idle
and add it tocg_active
- remove the searching user's channel from
cg_searching
and add it tocg_active
- add a newly generated channel name (you can use the UUID API to generate a UUID format name) to both users' unique channel groups. For example, add new channel name
1234-5678-9ABC
tocg_user123
andcg_user456
. These two users are not subscribe to the same channel to begin their chatting adventure with each other. - your server can now publish a message to this new channel introducing the users to each other (your client app can display avatar, user info, or even start the video stream if you are doing that).
- if either user clicks the leave button, both users' unique channels are removed from
cg_active
, added tocg_idle
, and the share chat channel is removed from both users' unique channel groups.
I can think of several details and features that would need to be addressed above and race conditions that your service would have control but this should expose how you can use channel groups to control the state of the users and a way of creating a name directory of users.
Post a Comment for "Android - Correctly Pairing And Connecting Two Users In A Random Chat App Using Parse And Pubnub"