{"action":"closed","issue":{"url":"https://api.github.com/repos/walkor/phpsocket.io/issues/82","repository_url":"https://api.github.com/repos/walkor/phpsocket.io","labels_url":"https://api.github.com/repos/walkor/phpsocket.io/issues/82/labels{/name}","comments_url":"https://api.github.com/repos/walkor/phpsocket.io/issues/82/comments","events_url":"https://api.github.com/repos/walkor/phpsocket.io/issues/82/events","html_url":"https://github.com/walkor/phpsocket.io/issues/82","id":221878631,"number":82,"title":"broadcast","user":{"login":"devyaz","id":20435017,"avatar_url":"https://avatars2.githubusercontent.com/u/20435017?v=3","gravatar_id":"","url":"https://api.github.com/users/devyaz","html_url":"https://github.com/devyaz","followers_url":"https://api.github.com/users/devyaz/followers","following_url":"https://api.github.com/users/devyaz/following{/other_user}","gists_url":"https://api.github.com/users/devyaz/gists{/gist_id}","starred_url":"https://api.github.com/users/devyaz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/devyaz/subscriptions","organizations_url":"https://api.github.com/users/devyaz/orgs","repos_url":"https://api.github.com/users/devyaz/repos","events_url":"https://api.github.com/users/devyaz/events{/privacy}","received_events_url":"https://api.github.com/users/devyaz/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2017-04-14T19:53:12Z","updated_at":"2017-04-20T07:11:10Z","closed_at":"2017-04-20T07:11:10Z","body":"hi i modified the chat example so i sends messages only on one on one(private chat)\r\nbut when i try to  use the node style `io.sockets.emit()` to`$io->$sockets->emit()` ,i get error that $sockets is undeclared, so i use `$socket->broadcast->emit()` which works but will broadcast connected users only to all except the new connection, here is the code\r\n```php\r\n<?php\r\nuse Workerman\\Worker;\r\nuse Workerman\\WebServer;\r\nuse Workerman\\Autoloader;\r\nuse PHPSocketIO\\SocketIO;\r\n\r\n// composer autoload\r\nrequire_once __DIR__ . '/../../vendor/autoload.php';\r\n\r\n$io = new SocketIO(2020);\r\n$names=[];$ids=[]; $id;\r\n$io->on('connection', function($socket){\r\n   \r\n    $socket->addedUser = false;\r\n   // $socket->names;\r\n  //  $socket->ids;\r\n    \r\n    // when the client emits 'new message', this listens and executes\r\n    $socket->on('message', function ($message,$indx,$user)use($socket){\r\n     global $names, $ids,$id;\r\n           \r\n$id=array_search($indx, $ids);//index of usr socket id\r\n        var_dump($id);\r\n        $name=$names[$id];\r\n        var_dump($name);\r\n        $socket->broadcast->to($indx)->emit('message',$user, $message);\r\n        // we tell the client to execute 'new message'\r\n        /*$socket->broadcast->emit('new message', array(\r\n            'username'=> $socket->username,\r\n            'message'=> $data,'id'=>$socket->id\r\n        ));*/\r\n    });\r\n\r\n    // when the client emits 'new_client', this listens and executes\r\n    $socket->on('new_client', function ($username,$cb) use($socket){\r\n       \r\n        global $names, $ids,$id;\r\n \r\n        var_dump($username.'  is online');\r\n        \r\n        if(in_array($username,$names)){\r\n          $cb(false);\r\n        }else{\r\n        $cb(true);\r\n        // we store the username in the socket session for this client\r\n        $socket->username = $username;\r\n        // add the client's username to the global list\r\n        array_push($names,$username);\r\n         array_push($ids,$socket->id);\r\n        //++$numUsers;\r\n        //tell connections the users list\r\n     ** $socket->emit('client',$names,$ids);**\r\n           var_dump($names);\r\n              var_dump($ids);\r\n        $socket->addedUser = true;\r\n       /* $socket->emit('login', array(\r\n            'numUsers' => $numUsers\r\n        ));*/\r\n        }\r\n        \r\n        // echo globally (all clients) that a person has connected\r\n       /* $socket->broadcast->emit('user joined', array(\r\n            'username' => $socket->username,\r\n            'numUsers' => $numUsers\r\n        ));*/\r\n    });\r\n\r\n    // when the client emits 'typing', we broadcast it to others\r\n    $socket->on('typing', function () use($socket) {\r\n        $socket->broadcast->emit('typing', array(\r\n            'username' => $socket->username\r\n        ));\r\n    });\r\n\r\n    // when the client emits 'stop typing', we broadcast it to others\r\n    $socket->on('stop typing', function () use($socket) {\r\n        $socket->broadcast->emit('stop typing', array(\r\n            'username' => $socket->username\r\n        ));\r\n    });\r\n\r\n    // when the user disconnects.. perform this\r\n    $socket->on('disconnect', function () use($socket) {\r\n       global $names, $ids,$id;\r\n        // remove the username from global usernames list\r\n       // if($socket->addedUser) {\r\n       $id=array_search($socket->id, $ids);\r\n       // array_slice($names, $id, count($names) - 1);\r\n       // array_slice($ids, $id, count($ids) - 1);\r\n           // --$numUsers;\r\nvar_dump($id);\r\n           // echo globally that this client has left\r\n           $socket->emit('client',$names,$ids);\r\n        //}\r\n   });\r\n\r\n\r\n   \r\n});\r\n\r\nif (!defined('GLOBAL_START')) {\r\n    Worker::runAll();\r\n}\r\n```\r\nand on the client side, its like the event isn't listening if not using `$socket->broadcast->emit` if i use `$socket->emit()` like in the code above, only the newly connected client gets the updated users list\r\nalso the on disconect function gives error, ill eventually use mysql for storage of connected users but in the mean time i want to use memory please help "}}
{"actor":{"display_login":"devyaz"}}