{"id":18746092,"url":"https://github.com/diloabininyeri/socket","last_synced_at":"2026-03-18T00:48:27.967Z","repository":{"id":215518657,"uuid":"739135256","full_name":"diloabininyeri/socket","owner":"diloabininyeri","description":"A library with socket channels where you can broadcast and manage channels, such as send a message to the bank channel and send a message to the software channel.","archived":false,"fork":false,"pushed_at":"2024-05-03T04:22:30.000Z","size":93,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-31T14:41:06.293Z","etag":null,"topics":["broadcast","channels","client","notification","server","server-client","socket","socket-io","stream","websockets"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/diloabininyeri.png","metadata":{"files":{"readme":"readme.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-04T21:17:44.000Z","updated_at":"2025-02-13T02:00:17.000Z","dependencies_parsed_at":"2024-05-03T05:41:13.866Z","dependency_job_id":null,"html_url":"https://github.com/diloabininyeri/socket","commit_stats":null,"previous_names":["diloabininyeri/socket"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/diloabininyeri/socket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diloabininyeri%2Fsocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diloabininyeri%2Fsocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diloabininyeri%2Fsocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diloabininyeri%2Fsocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/diloabininyeri","download_url":"https://codeload.github.com/diloabininyeri/socket/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diloabininyeri%2Fsocket/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30637765,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-18T00:09:27.587Z","status":"ssl_error","status_checked_at":"2026-03-18T00:09:26.123Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["broadcast","channels","client","notification","server","server-client","socket","socket-io","stream","websockets"],"created_at":"2024-11-07T16:20:52.216Z","updated_at":"2026-03-18T00:48:27.948Z","avatar_url":"https://github.com/diloabininyeri.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n### Socket Channels\n\nThis is a socket library, it is an advanced library, the purpose of which is to subscribe to channels on the socket and send messages to channels, as well as leaving channels...\n\nfor install\n```console\ncomposer require zeus/pusher\n```\n\n### Socket Server Management\nWe manage the entire socket server with an object inherited from the AbstractSocketClientHandler object...\nIn the example below, it is the ClientHandler object, but this is entirely up to your wishes.\nLet's, For example, let's send the message to everyone.\n\n\n\nserver.php\n```php\nuse Zeus\\Pusher\\AbstractSocketClientHandler;\nuse Zeus\\Pusher\\SocketServer;\n\n\nclass ClientHandler extends AbstractSocketClientHandler\n{\n\n\n    #[\\Override]\n    public function run(): void\n    {\n        $this-\u003esendTo()-\u003eeveryone($this-\u003egetMessage());\n    }\n}\n\n\n$socketServer = new SocketServer(ClientHandler::class);\n$socketServer-\u003eserve('127.0.0.1', 8080);\n\n```\nRun in the Terminal\n```console\nphp server.php\n```\njs websocket code\n\n```html\n\n\u003cscript\u003e\n    const socket = new WebSocket('ws://127.0.0.1:8080');\n\n    socket.addEventListener('open', (event) =\u003e {\n        console.log('WebSocket connection opened');\n        socket.send('test message');\n    });\n\n    socket.addEventListener('message', (event) =\u003e {\n        const message = event.data;\n        alert(`Received message: ${message}`);\n    });\n\n    socket.addEventListener('close', (event) =\u003e {\n        console.log('socket is closed', JSON.stringify(event));\n    });\n\n\n\u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Works with the wildcard\n\nYou can use wildcard in channel management.\nHere, the join method is subscribing to the channel, at the same time, you can use the leave method to cancel the subscription to the channel, you can subscribe to the channels and receive notifications, in fact, the main logic is like a real scenario and if we want to give up notifications to the channels, we use the leave method, that's all.\n\nAdditionally, there are more methods besides methods such as hasJoin, \njoin and leave,this is purely for managing channels well...\n\nIn the example below, we subscribe to `it.backend` and `it.frontend` channels and the beginning is `it.` We send notifications to all channels that start,\nthe purpose here is to group the channels.\n\nFirst of all, we use the `$this-\u003egetClient()` method to get the connected client...\n\n```php\nclass Handler extends AbstractSocketClientHandler\n{\n\n    #[\\Override]\n    public function run(): void\n    {\n\n        $this-\u003ejoin('it.frontend', $this-\u003egetClient());\n        $this-\u003ejoin('it.backend', $this-\u003egetClient());\n\n        $this-\u003esendTo()-\u003echannel('it.*', 'hello world');\n\n\n    }\n}\n\n\n$socketServer = new SocketServer(Handler::class);\n$socketServer-\u003eserve('127.0.0.1', 8080);\n```\n\n### Clients of a channel\n\u003eYou can get all the clients subscribed to a channel for example by default, all clients are subscribed to the channel named public.\n\n\u003eYou can receive all channels in the system and attract clients connected to these channels and perform transactions.\n```php\n\nclass Handler extends AbstractSocketClientHandler\n{\n\n    #[\\Override]\n    public function run(): void\n    {\n      //get all channels\n       $channels=$this-\u003egetChannels();\n       \n       //get all clients of the public channel\n       $clients=$this-\u003efindChannel('public')-\u003egetClients();\n       foreach($clients as $client){\n           $ip=$this-\u003egetRemoteHost($client);\n           if('x.x.x.x'===$ip){\n               $this-\u003edisconnect($client);\n           }\n       }\n       \n    }\n}\n\n\n$socketServer = new SocketServer(Handler::class);\n$socketServer-\u003eserve('127.0.0.1', 8080);\n```\n**Some methods for the channel, but don't forget there are more**\n\n```php\n\nclass Handler extends AbstractSocketClientHandler\n{\n\n    #[\\Override]\n    public function run(): void\n    {\n        $this-\u003eleave('it.backend', $this-\u003egetClient());\n        $this-\u003ehasJoin('it.backend', $this-\u003egetClient());\n        $this-\u003ehasChannel('it.backend');\n        $this-\u003efindChannel('it.backend');\n        $this-\u003egetChannels();\n        //and more\n    }\n}\n```\n\n\u003e note: leave method does not support wildcard for now.\n\u003e it will not working,I thought this was not appropriate due to its completely irreversible effects\n```php\n$this-\u003eleave('it.*');\n```\n### Works with the json\nLet's subscribe to the channels with the json coming from the frontend and send messages to the channels.\njs code\n\nThis example subscribes to the channel according to the json data coming from the clients\nand sends the message to the subscribed channel.\n\n**In the frontend**\n```javascript\n\nconst message = document.querySelector('#message');\nconst channel = document.querySelector('#channel');\nconst data = {channel: channel.value, message: message.value};\nsocket.send(JSON.stringify(data));\n\n```\n**In the Backend**\n```php\n\n/**\n * This is a PHP code that extends\n * the AbstractSocketClientHandler class\n * and implements the run() method.\n * It checks if the received message is in JSON format,\n * joins the specified channel, and sends the message to that channel.\n */\n\nclass Handler extends AbstractSocketClientHandler\n{\n\n    /**\n     * @throws JsonException\n     */\n    #[\\Override]\n    public function run(): void\n    {\n        if ($this-\u003eisMessageJson()) {\n\n            $this-\u003ejoin(\n                $this-\u003egetJsonValue('channel'),\n                $this-\u003egetClient()\n            );\n\n            $this-\u003esendTo()-\u003echannel(\n                $this-\u003egetJsonValue('channel'),\n                $this-\u003egetJsonValue('message')\n            );\n        }\n    }\n}\n\n\n$socketServer = new SocketServer(Handler::class);\n$socketServer-\u003eserve('127.0.0.1', 8080);\n```\n\n### The client\n\nThe php client does not listen to the socket, so what is the purpose?\n\nIt allows sending messages to connected connections.\nYou can even make this client compatible with the API and send messages to language-independent sockets.\n\nThis code forwards the message to the connected server and closes the connection.\nIn this way, it provides incredible flexibility,\nyou can even create an API socket server, yes, it sounds nice, doesn't it?\n\n\n\n```php\nuse Zeus\\Pusher\\SocketClient;                                     \n                                                                  \n                                                                  \n$socketClient = new SocketClient('0.0.0.0', 8080);                \n                                                                  \n$socketClient-\u003esleep(1);                                          \n                                                                  \n$message=json_encode([\n    'channel'=\u003e'backend',\n    'message'=\u003e'a new backend developer has applied'\n    ]);    \n         \n$socketClient-\u003esend($message);                                    \n                                                                  \nreturn $socketClient-\u003eread();                                     \n```\n### Send a custom message\nYou can send a message to a special client, for example, let's send a message only to the currently connected client.\n```php\nclass Handler extends AbstractSocketClientHandler\n{\n\n    #[\\Override]\n    public function run(): void\n    {\n\n        //send yourself a message\n        $this-\u003esendTo()-\u003eclient($this-\u003egetClient(),'Hello, world');\n        //send it to everyone except himself\n        $this-\u003esendTo()-\u003eexceptClient($this-\u003egetClient(),'Hello world');\n    }\n}\n\n\n$socketServer = new SocketServer(Handler::class);\n$socketServer-\u003eserve('127.0.0.1', 8080);\n```\n\n### Send a message by socket id\nThe socket ID can be used to send a custom message. Below is an example of sending a message to a specific socket ID.\nA feature designed especially for sending personal messages.\n```php\nclass Handler extends AbstractSocketClientHandler\n{\n\n    /**\n     */\n    #[\\Override]\n    public function run(): void\n    {\n        $this-\u003esendTo()-\u003eid($this-\u003egetId(), 'hello');\n    }\n}\n```\n\n### Websocket routes\nYou can create a namespace using js websocket and route path, below are examples of js and php codes.\n\n```php\n\nclass Handler extends AbstractSocketClientHandler\n{\n\n    /**\n     */\n    #[\\Override]\n    public function run(): void\n    {\n        $this-\u003esendTo()-\u003eroute('/chat', 'hello world');\n    }\n}\n\n\n$socketServer = new SocketServer(Handler::class);\n$socketServer-\u003eserve('0.0.0.0',8080);\n```\njavascript websocket \n\n```js\nconst socket = new WebSocket('ws://0.0.0.0:8080/chat');\n\nsocket.onopen = function (ev) {\n    console.log('opened connection');\n    console.log(ev);\n};\n\nsocket.onmessage = function (ev) {\n    console.log(ev);\n    alert(ev.data);\n};\n\n```\n\n**is Route and hasRoute methods**\n\n\n1. **hasRoute:**\n   Is there any client connecting via /chat route?.\n\n2. **isRoute:**\n   Is the current client connected via the /chat route?.\n\n\n\n```php\nclass Handler extends AbstractSocketClientHandler\n{\n\n    /**\n     */\n    #[Override]\n    public function run(): void\n    {\n        $this-\u003ehasRoute('/chat');//Is there any client connecting via /chat route?\n        $this-\u003eisRoute('/chat'); //Is the current client connected via the /chat route?\n    }\n}\n\n\n$socketServer = new SocketServer(Handler::class);\n$socketServer-\u003eserve(\n  '0.0.0.0',\n   8080\n);\n```\n\n### The host address of the client\nLet's disconnect the socket connection of an ip address\n\u003cbr/\u003e\nYou can get the host or IP address of the connected client.\nIn this way, you can deny an IP address, for example, let's disconnect a client whose IP value is x.x.x.x.\n\n```php\n\nclass Handler extends AbstractSocketClientHandler\n{\n\n    /**\n     */\n    #[\\Override]\n    public function run(): void\n    {\n        $host=$this-\u003egetRemoteHost();\n        if('x.x.x.x'===$host){\n            $this-\u003edisconnect($this-\u003egetClient());\n        }\n    }\n}\n```\n### Get the status of the client\n\nYou can get any client status\n\n```php\n\nclass Handler extends AbstractSocketClientHandler\n{\n\n    /**\n     */\n    #[\\Override]\n    public function run(): void\n    {\n        //type 1 using \n        \n        $status=$this-\u003egetStatus();\n        \n        //type 2 using\n        \n        $clients=$this-\u003efindChannel('it.backend')-\u003egetClients();\n        foreach($clients as $client){\n            $status=$this-\u003egetStatus($client);\n            print_r($status);\n        }\n      \n    }\n}\n\n### More\n\n```\n### For more information\nNow, if you want to add Channel Broadcast and Send classes, there is a way to do this. For example, let's create a method for Send and use it.\n```php\n\nclass Handler extends AbstractSocketClientHandler\n{\n\n    /**\n     */\n    #[\\Override]\n    public function run(): void\n    {\n        Send::method(\n            'test',\n            static fn(Socket $client) =\u003e socket_write($client, Message::encode('test'))\n        );\n\n\n        Channel::method(\n            'empty',\n            static fn() =\u003e $this-\u003eclients=[])\n        );\n\n        $this-\u003esendTo()-\u003etest($this-\u003egetClient());\n        $this-\u003efindChannel('it.backend')-\u003eempty();\n    }\n}\n```\nIf you pay attention, the 'test' and 'empty' methods do not actually exist, we simulated them as if they existed.\n\n\nto be continued...\n\u003cbr\u003e\n[|||||||||||||||]\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiloabininyeri%2Fsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiloabininyeri%2Fsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiloabininyeri%2Fsocket/lists"}