Django-channels connecting 2 random people

268 views
Skip to first unread message

Nikoleta Misheva

unread,
Jan 14, 2017, 3:41:13 PM1/14/17
to Django users
Hello fellows, I started to go into channels and I saw the example for multiplex chat but it is not excactly what I want and I will be happy if someone can point me in the right direction or show me an example.
So I want to have many dynamically created rooms than will be destroyed after the session which the multiplexing example  here takes care of. But want I specifically want to do is to have only 2 people in a room randomly matched and connected. 

Here is an example: 
User1 clicks a button and user2 clicks the button too and so they are in something like a list with people that are online, then I randomly pick the 2 of them to connect to each other in a private room only for them which will be destroyed when  they left

Thank you in advance

Andrew Godwin

unread,
Jan 14, 2017, 3:51:59 PM1/14/17
to django...@googlegroups.com
Your best bet is probably to use a database to track who's currently got an open connection and their reply channel, and then use database rows to match pairs together (in another table).

Your message received code can then look up the other side in the table and forward it to that reply channel. I don't think groups would help too much here.

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fae01772-7a30-4552-a104-0e5f519733de%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nikoleta Misheva

unread,
Jan 14, 2017, 4:59:04 PM1/14/17
to Django users
Would it be a good idea to add everyone that has an open connection to a list of dictionaries and when 2 people are connected to remove them from the list? And how can I guarantee that the channels limit is just 2 and there won't be anyone joining by mistake? Or I just didn't get your idea. Thanks for the spent time :)


събота, 14 януари 2017 г., 22:51:59 UTC+2, Andrew Godwin написа:
Your best bet is probably to use a database to track who's currently got an open connection and their reply channel, and then use database rows to match pairs together (in another table).

Your message received code can then look up the other side in the table and forward it to that reply channel. I don't think groups would help too much here.

Andrew
On 14 Jan 2017 12:41, "Nikoleta Misheva" <nith...@gmail.com> wrote:
Hello fellows, I started to go into channels and I saw the example for multiplex chat but it is not excactly what I want and I will be happy if someone can point me in the right direction or show me an example.
So I want to have many dynamically created rooms than will be destroyed after the session which the multiplexing example  here takes care of. But want I specifically want to do is to have only 2 people in a room randomly matched and connected. 

Here is an example: 
User1 clicks a button and user2 clicks the button too and so they are in something like a list with people that are online, then I randomly pick the 2 of them to connect to each other in a private room only for them which will be destroyed when  they left

Thank you in advance

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Andrew Godwin

unread,
Jan 15, 2017, 10:02:59 PM1/15/17
to django...@googlegroups.com
Kind of, but you need to use a database rather than a dictionary to make sure state is shared properly. If you have one table called users with columns (id, username, reply_channel) and another called pairings with (user1_id, user2_id), then when someone connects, you can:

a) Add them to the users table
b) If there is at least one user in the users table that is not in the pairings table, pick that user and pair it with the one connecting
c) If not, wait for someone else to come along and do (b)

That's a very simplistic version - you would also need cleanup for when people disconnect (you need to remove them from users and unpair them from their partner so they can be re-paired if you want) but that's the basic structure.

Andrew

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages