The session should be available as:
Yii::$app->session->get('id')
redis should be available as:
Yii::$app->redis->get('key');
Session and Redis must be defined as components in config
Notify user as:
return Yii::$app->redis->executeCommand('PUBLISH', [
'channel' => 'notification_user_'<THERE_WILL_BE_USER_IDENTIFIER>,
'message' => Json::encode(['name' => $name, 'message' => $message])
]);
Listen on the client as:
$( document ).ready(function() {
var socket = io.connect('http://localhost:8890');
socket.on('notification_user_<THERE_WILL_BE_SAME_USER_IDENTIFIER>', function (data) {
var message = JSON.parse(data);
$( "#notifications" ).prepend( "<p><strong>" + message.name + "</strong>: " + message.message + "</p>" );
});
});
I think this should work
source
share