Reputation: 923
I have created schedule calls. each schedule have 1 unique session. In my case initially schedule session will be empty. If two or more members wants to join in schedule call they will click on start button. at that time i am checking session exists or not in table. If it is empty then i am generating session and updating session in db and returning that session.
But some times, multiple users clicks on start button at same time, they are going to else condition and updating different sessions and returning with respective session id. then they are redirecting to different session rooms. So how can i prevent this and return same session for all users for single schedule calleven they clicks at same time.
Below is my code which resulting different sessions for users when they click on same time.
$schInfo = Schedule::where('status', '=', 1)->where('id', '=', $scheduleId)->first();
if(isset($schInfo ->session) && !empty($schInfo ->session))
{
$schedule_session = $schInfo ->session;
} else
{
$schedule_session = CreateSession();
$res = Schedule::where('id', $scheduleId)->update(['session' => $schedule_session]);
}
return $schedule_session;
I have updated this like below. But i am not sure this is the right solution
$schInfo = Schedule::where('status', '=', 1)->where('id', '=', $scheduleId)->first();
if(isset($schInfo ->session) && !empty($schInfo ->session))
{
$schedule_session = $schInfo ->session;
} else
{
$schedule_session = CreateSession();
sleep(rand(1,5));
$res = Schedule::where('id', $scheduleId)->whereNull('session')->update(['session' => $schedule_session]);
$schInfo = Schedule::where('status', '=', 1)->where('id', '=', $scheduleId)->first();
$schedule_session = $schInfo ->session;
}
return $schedule_session;
In 1st code snippet out of 10 attempts 8 times generating different sessions. Using second snippet out of 10 times 1 or 2 times generating different sessions.
Please let me know is there any way to do?
Upvotes: 1
Views: 351
Reputation: 6693
This is no way a code solution, the code will not be use-able. However, the logic will be. Your issue is that you store the session before queuing the users to join the call. You should first make the user join a queue, then the hosting caller checks the queue every n
seconds for participants (n
seconds will allow time for multiple click issues). Then use the first participant in the calling queue to generate the session and append to the call.
You'll need feedback to the participant joining the call which I have not provided in this, but hopefully you get the gist of what I'm saying.
Front-End example of usage:
$(function() {
// Pull out the first connection in the queue, if any
var checkIncomingCallConnections = setInterval(function() {
$.get('/api/calls/incoming', function(response) {
if(response.hasUser()) {
// Your logic for updating or creating the call interface
// ...
clearInterval(this);
}
});
}, 1000);
$('#call-btn').click(function() {
// Generate the queue...
$.post('/api/calls/outgoing', function(response) {
// Your logic for updating or creating the call interface
// ...
checkIncomingCallConnections();
});
});
})(jQuery);
Back-End example of usage:
$schInfo = Schedule::where('status', '=', 1)->where('id', '=', $scheduleId)->first();
if(isset($schInfo ->session):
$schedule_session = $schInfo->session;
else:
$schedule_session = CreateSession();
$res = Schedule::where('id', $scheduleId)->update(['session' => $schedule_session]);
endif;
return $schedule_session;
API Outgoing:
Route::post('/calls/outgoing', function() {
CallQueue::create(referenceAboveCode()); // Should return $schedule_session
});
Route::get('/calls/incoming', function() {
$queue = CallQueue::list(referenceAboveCode()); // Should return $schedule_session
// $queue[0] joins the call
// update database with this session
});
Upvotes: 1