Android - correctly pairing and connecting two users in a random chat app using parse and pubnub

后端 未结 1 1406
名媛妹妹
名媛妹妹 2021-01-03 15:10

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 c

相关标签:
1条回答
  • 2021-01-03 16:01

    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 and cg_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

    1. remove their user unique channel from the cg_idle channel group
    2. add their user unique channel to the cg_searching channel group
    3. list_channels of the cg_idle to get a list of chat partner candidates
    4. select a user channel from the list of cg_idle candidates
    5. check to make sure the selected candidate is still idle
    6. 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
    7. if user still idle, remove that user's channel from cg_idle and add it to cg_active
    8. remove the searching user's channel from cg_searching and add it to cg_active
    9. 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 to cg_user123 and cg_user456. These two users are not subscribe to the same channel to begin their chatting adventure with each other.
    10. 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).
    11. if either user clicks the leave button, both users' unique channels are removed from cg_active, added to cg_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.

    0 讨论(0)
提交回复
热议问题