I am working on a real time chat system in which one user can send/receive message to a certain user. I am using Laravel and in the controller I map the connection object against the resourceId.
$this->connections[$conn->resourceId]=$conn;
To find the resource_Id of a specific user i save the resource_Id of that user in database against his unique id. I this way i can find the resource_Id of a user with its unique_id and then i can find the Connection Interface object,right.
When user_1 send message to user_2, the user_2 successfully get this message.
What is my problem???
Suppose user2 open a tab say tab1 and a connection is created, he receive message from user_1 successfully. User_2 open another tab then a new connection is created and now if he receive message it will only appear on tab_2 but i want it to be appear on both tabs.
I know why it appear on tab_2 as when the second connection is created it overwrites the already present resource_id in the database as i store only one resource_id against each user.
Is there a way that the both tabs are handled by same connection object.
I have a solution. I think I have to store multiple resource ids against a user_id in database, so that i can access all the connections of a particular user and then send message to all the connections. Is it the right approach to do this in my case?
here is the code of onOpen function of controller
function onOpen(ConnectionInterface $conn){
$this->connections[$conn->resourceId]=$conn;
$data=['action'=>'map_resource_id','resourceid'=>($conn->resourceId)];
//send the resource id to client to map this id against user_id
//the client send an ajax request to store the resource id against
//user_id
$conn->send(json_encode($data));
echo "New connection! ({$conn->resourceId})
";
}
Code for onClose method is here
function onClose(ConnectionInterface $conn){
unset($this->connections[$conn->resourceId]);
$status=DB::update('update sockets set resource_id=0 where resource_id='.$conn->resourceId);
echo "Connection {$conn->resourceId} has disconnected
";
}