Socket io connection from one page in Yii2

Viewed 3093

I am using Yii advanced project with socket io and nodejs. My server.js is located in yii2advanced/nodejs.

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
var connectedUsers = {};

server.listen(8890);

io.of('frontend/views/project/index').on('connection', function (socket) {

    socket.on('usersocket', function(data) {
        connectedUsers[data] = socket;
        console.log("new client with Id: " + data + " added to CLIENTS list");
    });
});

My client.js is located in yii2advanced/frontend/web/js.

$( document ).ready(function() { 
    var socket = io.connect('http://localhost:8890/frontend/views/project/index');

    socket.on('connect', function () {
        socket.emit('usersocket', user_id);
});

I need to allow socket to connect only from frontend/views/project/index.php page. Now socket connects even from frontend/web/index.php I followed an example from socket docs ('Restricting yourself to a namespace') but I am not sure if I specify the namespaces correctly.

1 Answers
Related