Yii2 node how to get redis session key by user id

I am using Yii-2-real-time-chat-app-with-nodejs-socketio-and-redisio . I need to send a notification to a specific user in order to use the redis key. How do I get the redis session key by user ID?

Trying in yii \ redis \ Session

    protected function calculateKey($id)
        {
            return $this->keyPrefix ."".$_SESSION['__id']."".               
            md5(json_encode([__CLASS__, $id]));
        }

but authorization does not work

+4
source share
1 answer

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

0
source

Source: https://habr.com/ru/post/1621060/


All Articles