How to send a message to the client from the backend server ?
- Dominant language
- PHP
- Stars
- 11.6k
- Forks
- 2.2k
- Avg merge
- 11h 30m
- Merged PRs (30d)
- 3
Description
Hey! First of all, I want to thank you for your library.
I have a problem that I don't know how to solve yet.
I have a chat in which I implemented sending simple messages, everything works fine. Now I want to send files and ran into a problem with large file sizes. I decided not to load the connection socket and send files directly to the backend server via form/data.
BUT. How, after a successful send, initialize the user and send him a socket notification that his file was successfully sent? At the moment my code looks like this:
`
$ws_worker = new Worker('websocket://'.config('websockets.server'));
// Emitted when new connection come
$ws_worker->onConnect = function (TcpConnection $connection) {
$connection->onWebSocketConnect = function($connection) {
if (isset($_GET['token']) && $token = PersonalAccessToken::findToken($_GET['token'])) {
$user = $token->tokenable;
$this->clients[$connection->id] = [
'user' => $user,
'connection' => $connection,
];
}
return;
};
};
$ws_worker->onMessage = function (TcpConnection $connection, $message) {
if (!array_key_exists($connection->id, $this->clients)) {
echo "Error. User not authorized\n";
return;
}
$sender = $this->clients[$connection->id];
$data = json_decode($message, true);
// send message to DB
$connectionClientsUniques = ArrayHelper::arrayColumnsWithSaveKeys($this->clients, 'client_unique');
$findedConnectionsClients = array_keys($connectionClientsUniques, $roomMember['client_unique'], true);
foreach ($findedConnectionsClients as $findedConnectionClient) {
// SEND MESSAGE FOR CONNECTION
}
};
$ws_worker->onClose = function($connection)
{
echo "Close connection ID ".$connection->id."\n";
if (array_key_exists($connection->id, $this->clients)) {
unset($this->clients[$connection->id]);
echo "FINDED CONNECTION ID ".$connection->id." AND LOST CONNECTIONS - ".count($this->clients)." \n";
}
};
// Run worker
Worker::runAll();
`
My problem is that I don't know where to store active user sessions. So that I can access them from other parts of the project. Now they are stored in my clients variable in the same place where the websocket server starts. Should they be stored elsewhere? Where do you recommend?
And also, please tell me how to send notifications to the desired connection from any part of the project. For example, I imagined it like this:
`
send_alert.php
findByConnection(...)->send($data);
`
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the Worker websocket setup and the onConnect, onMessage, and onClose handlers shown in the issue. Trace how the clients variable and connection IDs are scoped, then review the project’s documented process and connection-management APIs. Done means identifying a supported way to address a user connection from another part of the application, with session storage and notification behavior clearly defined.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend, networking
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100