I am currently in the process of developing a chat application. At this stage, users are able to send messages to each other and I have successfully stored these messages in my database. Now, my objective is to display the stored messages from the database when the user opens up the page to engage in conversation with another user.
Below is an example of what my onOpen function entails:
public function onOpen(ConnectionInterface $conn)
{
$query = $conn->httpRequest->getUri()->getQuery();
preg_match_all('!\d+!', $query, $matches);
$my_id = $matches[0][0];
$friend_id = $matches[0][1];
$conn->resourceId = $my_id;
$this->users[$conn->resourceId] = $conn;
$messages = Private_message::where([
['user1',$my_id],
['user2',$friend_id]
])->orWhere([
['user1',$friend_id],
['user2',$my_id]
])->get()->toArray();
}
My goal is to pass the $messages
array to my privateChat.blade.php
view for rendering purposes.
I attempted methods such as
return view('privateChat',['messages' => $messages]);
and @dd($messages)
within my privateChat.blade.php
, but encountered an error indicating that the variable was undefined. I also experimented with using return $messages
and displaying them in my onOpen JavaScript function, without success. Can anyone advise on the correct approach to accomplish this?