{"id":16461517,"url":"https://github.com/thenlabs/socket-server","last_synced_at":"2025-10-27T10:31:00.536Z","repository":{"id":57068333,"uuid":"415910291","full_name":"thenlabs/socket-server","owner":"thenlabs","description":"An useful library for creating asynchronous network applications with PHP.","archived":false,"fork":false,"pushed_at":"2022-04-19T14:54:36.000Z","size":179,"stargazers_count":2,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"1.1","last_synced_at":"2025-02-01T05:01:50.024Z","etag":null,"topics":["network","network-programming","networks","php","php-library","socket","socket-io","socket-programming","sockets","socketserver"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thenlabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-11T12:09:33.000Z","updated_at":"2023-01-19T01:26:59.000Z","dependencies_parsed_at":"2022-08-24T10:20:37.747Z","dependency_job_id":null,"html_url":"https://github.com/thenlabs/socket-server","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thenlabs%2Fsocket-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thenlabs%2Fsocket-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thenlabs%2Fsocket-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thenlabs%2Fsocket-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thenlabs","download_url":"https://codeload.github.com/thenlabs/socket-server/tar.gz/refs/heads/1.1","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238480614,"owners_count":19479521,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["network","network-programming","networks","php","php-library","socket","socket-io","socket-programming","sockets","socketserver"],"created_at":"2024-10-11T11:08:28.023Z","updated_at":"2025-10-27T10:30:55.199Z","avatar_url":"https://github.com/thenlabs.png","language":"PHP","readme":"\n# SocketServer\n\nAn useful library for creating asynchronous network applications with PHP.\n\n\u003eIf you like this project gift us a ⭐.\n\n## Features.\n\n- Asynchronous connections.\n- Multiple configuration options.\n- Event system to implement the application logic.\n- Logs support.\n\n## Installation.\n\n    $ composer require thenlabs/socket-server\n\n## Usage.\n\nThe below code show a bare network application that accept multiple connections and forward each incoming message to the rest of the connections.\n\n\u003eThat application can be found in the `tests/Functional/hub.php` file. For test it you can run `php tests/Functional/hub.php`.\n\nCan be seen that the `SocketServer` class offers the necessary events for react at the differents connection status.\n\n\u003eCheck the `SocketServer` API for knows all his possibilities.\n\n```php\n\u003c?php\n/**\n * What this program does is accept multiple connections and forward\n * each incoming message to the rest of the connections.\n */\n\nrequire_once __DIR__.'/../../bootstrap.php';\n\nuse ThenLabs\\SocketServer\\Event\\ConnectionEvent;\nuse ThenLabs\\SocketServer\\Event\\DataEvent;\nuse ThenLabs\\SocketServer\\Event\\DisconnectionEvent;\nuse ThenLabs\\SocketServer\\SocketServer;\n\nclass HubServer extends SocketServer\n{\n    protected $connections = [];\n\n    public function onConnection(ConnectionEvent $event): void\n    {\n        foreach ($this-\u003econnections as $connection) {\n            $connection-\u003ewriteLine(\"New connection.\");\n        }\n\n        $this-\u003econnections[] = $event-\u003egetConnection();\n    }\n\n    public function onData(DataEvent $event): void\n    {\n        $data = $event-\u003egetData();\n\n        switch ($data) {\n            case 'exit':\n                $event-\u003egetConnection()-\u003eclose();\n                break;\n\n            case 'stop':\n                $event-\u003egetServer()-\u003estop();\n                break;\n\n            default:\n                foreach ($this-\u003econnections as $connection) {\n                    if ($connection != $event-\u003egetConnection()) {\n                        $connection-\u003ewriteLine($data);\n                    }\n                }\n                break;\n        }\n    }\n\n    public function onDisconnection(DisconnectionEvent $event): void\n    {\n        foreach ($this-\u003econnections as $id =\u003e $connection) {\n            if ($connection == $event-\u003egetConnection()) {\n                unset($this-\u003econnections[$id]);\n                break;\n            }\n        }\n    }\n}\n\n$server = new HubServer(['socket' =\u003e $argv[1] ?? 'tcp://127.0.0.1:9000']);\n$server-\u003estart();\n```\n\nThe above example works as follows:\n\n![](demo.gif)\n\n## Development.\n\n### Running the tests.\n\nFor run the tests, runs the next command:\n\n    $ ./vendor/bin/pyramidal","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthenlabs%2Fsocket-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthenlabs%2Fsocket-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthenlabs%2Fsocket-server/lists"}