Number of person to the session - opentok

Is there away to know how many person connects to a session?
I wish to implement a mechanism where only two persons are allowed to connect, and if there is more than two, an alert will be shown.

When you get a sessionConnected event, you get an array of connections telling you the number of people in the session. http://www.tokbox.com/opentok/docs/js/reference/SessionConnectEvent.html
After you are connected, everytime someone joins the session, you get ConnectionEvents for people leaving and joining the session.
These events can give you an accurate count of the # of connections in the session at any given time

Related

How can I create groups of users in a waiting room automatically?

We are trying to enable collaborative chats in an educational application. The general idea is that the users (students) will start an exercise, this will make them join an specific waiting room. And somehow the system should be able to decide it has enough students to create a group of n students (depending on the exercise) according to a given strategy and will send a message to those students to join a chatroom with a generated ID so that they can collaborate.
Right now we are totally blank and cannot decide on how to make the server decide wether to try and create groups or to wait for more students. Our stack is in Spring Boot, Redis and Postgress. Our initial idea was to add the students into a waiting room in Redis and launch a Spring event every time a student joined the waitlist. However, we understand that approach might generate many race conditions, which should be avoided.
Create an exercise_students table which has a SERIAL column on it, call it arrival_order or something. Another column for group_id. As students sign in for the exercise, insert them into this table. By nature SERIAL is atomically auto-incremented, so you avoid race conditions. Regularly query the table for students with no group_id (I assume you have a exercise_group table of some sort that defines how many students are part of a group). When the count reaches n, update them with the group_id and create a new group in the exercise_group table for the next group.
Relational databases are pretty good at this sort of thing. Atomic updating of state is pretty straightforward stuff.

How to find the next placement in a list of voted users? (Caching)

I am looking for a very quick solution to solve the following problem:
On a web server it should be possible that a user can vote there for another user. So one user can always vote for another. This happens by sending a POST /vote/:id. This vote must now move into a list from all already made votes of other users.
This list of the ranking of the already voted users, should then put the user with the most votes on place one. The one with the second most votes on place 2 and so on.
Currently I use Redis for this and save the selected user id in a hash for each user id.
My problem now is to find out in this ranking list of the elected users, to which place now one or more users must be moved, whenever a new election arrives.
How can I know on which number to place the user that has just been elected, out of all those already elected?
What is the fastest and most efficient solution for this?
Currently I was thinking of a Redis List that would then allow me to record the placement with the user id and then filter by that. But is a complexity of O(n).

Socket IO Default Rooms

I would like to create a live notification center by using Web Socket in Python.
I have a feature that send / emit a message to specific user.
I have read about socket id and room. I think there are a couple way to do direct user interaction using both feature from Socket IO.
First:
It is said in the docs that every client / socket connected to the socket server, they were giving an unique socket#id and joined a room identified by id.
I have an idea that I do not need create a room again for a user, and just by using this default room.
By using these, I want to create a mapping of socket#id with userid (logged user id), and store it into redis. Like, if I want to send a message to a user id, I just need to search for it's socket#id.
The question for this first way is, is the socket#id will be the same
if a client disconnect and reconnect soon the connection can be
established ?
Second:
I have read many sources about using room for direct message. For connected client or socket, I just need to create a new room with id of the user id that sent from client (after user login).
The advantages of this way is, if the user is using same user id for several device, I can group them in a room. So, if I want to notify to all device, I just need to send the message for room of id user.
The question of this way, is the user will be moved from default room
from first way or they will be on 2 room (1 default, 1 specified room
by user id) ?
Thank You
This is the way i do it
a users socketid will be different on every page request, and if a user have 3 tabs open he will have a different socketid on every tab, so if you want a user to get the notification in every tab u need to send it to 3 socketids
a solution for this is to make an object of connected users, you can add the userid/username as a key in your object and make an array of all socketids for this user, make sure to remove the socketid when the user disconnect too, and when u want to send a notification u just loop over the array and send to all of the users socketids
The question for this first way is, is the socket#id will be the same if a client disconnect and reconnect soon the connection can be established ?
No. Each connection gets a randomly assigned socket id.
The question of this way, is the user will be moved from default room from first way or they will be on 2 room (1 default, 1 specified room by user id) ?
Users can be in multiple rooms at a time. If you create a room with your application generated user ids, and then put all the socket connections from the user into that room, each of these clients will be in two rooms, the one that matches the socket id and the one that matches your user id. You can then emit a message to each socket individually or to all sockets that belong to the user.

Laravel - Store and Resume data for ALL the sessions

I use the laravel session with the database. I would like to know how to resume the informations are stored for ALL the sessions, that it means between all the row in the session table.
I am developing a booking room system. I use the session in this way:
First step:
A user searches all the available room for a certain date. A list appears with all the available rooms.
Second step:
After the user selects the room, it is redirected to the pay form.
In the first step when the user selects the room, the room id is stored in the session.
The things I would like to do is this:
The session is used to store all the room are chosen by the users, in case two or more users are looking for a same room in the same period. So that the rooms are not available in the search of other users until the first user pays.
Laravel has a payload column where it stores all the key and value but it is only a sequence of letter and number.
Laravel has a payload column where it stores all the key and value but it is only a sequence of letter and number
When you call \Session::put('foo', 'bar') the value es added into an associative array that keeps all data. If you are using database driver, then laravel serialize the array to store it as a string. That is why you only can see a lot of text. So, working with session will be so complicated because you has to serialize/unserialize over again.
How to block the room? Well, there are many ways to do that, all depends from your architecture. For example, you can store selected rooms in other table for 5 minutes (enough time to pay). Lets say you can work with two tables:
selected_rooms
------------------
number | expire_at
and...
rooms
number | beds | busy
The user search for a cool room. The system must ignores all the rooms that have references to selected_rooms table that has not been expired.
When the user select a room, you store it at selected_rooms room table with a expire_at value of now + 5 minutes.
When the user pay the room, you can remove it from selected_rooms. If the user just logout or just leave the pc, it does not matter, because after 5 minutes the room is available again

Laravel - building an auction site

I'm building an auction site using laravel 5 however I'm currently unsure of the best way to go around the bidding process.
I currently have it set-up so that once the user hits a bid button the script runs to place the bid, however if multiple users do this at the same time this causes issues with multiple bids with the same value. I thought about modifying this so that it would queue the bid that way only 1 bid is being processed at once however I believe there will be a better method.
If someone could point me in the correct direction it would be greatly appreciated.
One way to do it is to simply insert the bid with a high precision timestamp immediately, then check the table using a select and see if its actually a leading bid or not. The table should have an auto incrementing id, so even if two bids have the exact same timestamp, sorting also by id will tell you which one was actually received first.

Resources