{"id":21771130,"url":"https://github.com/initphp/socket","last_synced_at":"2025-04-13T16:41:16.318Z","repository":{"id":56991545,"uuid":"470092810","full_name":"InitPHP/Socket","owner":"InitPHP","description":"PHP Socket (TCP, TLS, UDP, SSL) Server/Client Library","archived":false,"fork":false,"pushed_at":"2024-12-01T11:13:07.000Z","size":22,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-27T07:35:51.858Z","etag":null,"topics":["php","php-socket","php-tcp-client","php-tcp-server","socket","socket-client","socket-server","ssl","ssl-client","ssl-server","tls-client","tls-server","udp-client","udp-server","udp-server-client"],"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/InitPHP.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-15T09:42:31.000Z","updated_at":"2025-02-27T22:57:46.000Z","dependencies_parsed_at":"2022-08-21T10:10:18.573Z","dependency_job_id":null,"html_url":"https://github.com/InitPHP/Socket","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FSocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FSocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FSocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FSocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/InitPHP","download_url":"https://codeload.github.com/InitPHP/Socket/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248747020,"owners_count":21155368,"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":["php","php-socket","php-tcp-client","php-tcp-server","socket","socket-client","socket-server","ssl","ssl-client","ssl-server","tls-client","tls-server","udp-client","udp-server","udp-server-client"],"created_at":"2024-11-26T14:15:09.870Z","updated_at":"2025-04-13T16:41:16.309Z","avatar_url":"https://github.com/InitPHP.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# InitPHP Socket Manager\n\nPHP Socket (TCP, TLS, UDP, SSL) Server/Client Library\n\n[![Latest Stable Version](http://poser.pugx.org/initphp/socket/v)](https://packagist.org/packages/initphp/socket) [![Total Downloads](http://poser.pugx.org/initphp/socket/downloads)](https://packagist.org/packages/initphp/socket) [![Latest Unstable Version](http://poser.pugx.org/initphp/socket/v/unstable)](https://packagist.org/packages/initphp/socket) [![License](http://poser.pugx.org/initphp/socket/license)](https://packagist.org/packages/initphp/socket) [![PHP Version Require](http://poser.pugx.org/initphp/socket/require/php)](https://packagist.org/packages/initphp/socket)\n\n## Requirements\n\n- PHP 7.4 or higher\n- PHP Sockets Extension\n\n## Installation\n\n```\ncomposer require initphp/socket\n```\n\n## Usage\n\n**Supported Types :**\n\n- TCP\n- UDP\n- TLS\n- SSL\n\n### Factory\n\n`\\InitPHP\\Socket\\Socket::class` It allows you to easily create socket server or client.\n\n#### `Socket::server()`\n\n```php \npublic static function server(int $handler = Socket::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \\InitPHP\\Socket\\Interfaces\\SocketServerInterface\n```\n\n- `$handler` : `Socket::SSL`, `Socket::TCP`, `Socket::TLS` or `Socket::UDP` \n- `$host` : Identifies the socket host. If not defined or left blank, it will throw an error.\n- `$port` : Identifies the socket port. If not defined or left blank, it will throw an error.\n- `$argument` : This value is the value that will be sent as 3 parameters to the constructor method of the handler.\n    - SSL or TLS = (float) Defines the timeout period.\n    - UDP or TCP = (string) Defines the protocol family to be used by the socket. \"v4\", \"v6\" or \"unix\"\n\n#### `Socket::client()`\n\n```php \npublic static function client(int $handler = self::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \\InitPHP\\Socket\\Interfaces\\SocketClientInterface\n```\n\n- `$handler` : `Socket::SSL`, `Socket::TCP`, `Socket::TLS` or `Socket::UDP`\n- `$host` : Identifies the socket host. If not defined or left blank, it will throw an error.\n- `$port` : Identifies the socket port. If not defined or left blank, it will throw an error.\n- `$argument` : This value is the value that will be sent as 3 parameters to the constructor method of the handler.\n    - SSL or TLS = (float) Defines the timeout period.\n    - UDP or TCP = (string) Defines the protocol family to be used by the socket. \"v4\", \"v6\" or \"unix\"\n\n### Methods\n\n**`connection()` :** Initiates the socket connection.\n\n```php \npublic function connection(): self;\n```\n\n**`disconnect()` :** Terminates the connection.\n\n```php \npublic function disconnect(): bool;\n```\n\n**`read()` :** Reads data from socket.\n\n```php \npublic function read(int $length = 1024): ?string;\n```\n\n**`write()` :** Writes data to the socket\n\n```php \npublic function write(string $string): ?int;\n```\n\n#### Server Methods\n\n**`live()` :**\n\n```php \npublic function live(callable $callback): void;\n```\n\n**`wait()` :**\n\n```php \npublic function wait(int $second): void;\n```\n\n**`broadcast()` :**\n\n```php\npublic function broadcast(string $message, array|string|int|null $clients = null): bool;\n```\n\n#### Special methods for TLS and SSL.\n\nTLS and SSL work similarly.\n\nThere are some additional methods you can use from TLS and SSL sockets.\n\n**`timeout()` :** Defines the timeout period of the current.\n\n```php\npublic function timeout(int $second): self;\n```\n\n**`blocking()` :** Sets the blocking mode of the current.\n\n```php\npublic function blocking(bool $mode = true): self;\n```\n\n**`crypto()` :** Turns encryption on or off on a connected socket.\n\n```php\npublic function crypto(?string $method = null): self;\n```\n\nPossible values for `$method` are;\n\n- \"sslv2\"\n- \"sslv3\"\n- \"sslv23\"\n- \"any\"\n- \"tls\"\n- \"tlsv1.0\"\n- \"tlsv1.1\"\n- \"tlsv1.2\"\n- NULL\n\n**`option()` :** Defines connection options for SSL and TLS. see; [https://www.php.net/manual/en/context.ssl.php](https://www.php.net/manual/en/context.ssl.php)\n\n```php\npublic function option(string $key, mixed $value): self;\n```\n\n### Socket Server\n\n_**Example :**_\n\n```php\nrequire_once \"../vendor/autoload.php\";\nuse \\InitPHP\\Socket\\Socket;\nuse \\InitPHP\\Socket\\Interfaces\\{SocketServerInterface, SocketServerClientInterface};\n\n$server = Socket::server(Socket::TLS, '127.0.0.1', 8080);\n$server-\u003econnection();\n\n$server-\u003elive(function (SocketServerInterface $socket, SocketServerClientInterface $client) {\n    $read = $client-\u003eread();\n    if (!$read) {\n        return;\n    }\n    if (in_array($read, ['exit', 'quit'])) {\n            $client-\u003epush(\"Goodbye!\");\n            $client-\u003eclose();\n        return;\n    } else if (preg_match('/^REGISTER\\s+([\\w]{3,})$/i', $read, $matches)) {\n        // REGISTER admin\n        $name = trim(mb_substr($read, 9));\n        $socket-\u003eclientRegister($name, $client);\n    } else if (preg_match('/^SEND\\s@([\\w]+)\\s(.*)$/i', $read, $matches)) {\n        // SEND @admin Hello World\n        $pushSocketName = $matches[1];\n        $message = $matches[2];\n        $socket-\u003ebroadcast($message, [$pushSocketName])\n    } else {\n        $message = trim($read);\n        !empty($message) \u0026\u0026 $socket-\u003ebroadcast($message);\n    }\n});\n```\n\n### Socket Client\n\n_**Example :**_\n\n```php\nrequire_once \"../vendor/autoload.php\";\nuse \\InitPHP\\Socket\\Socket;\n\n$client = Socket::client(Socket::SSL, 'smtp.gmail.com', 465);\n\n$client-\u003eoption('verify_peer', false)\n    -\u003eoption('verify_peer_name', false);\n\n$client-\u003econnection();\n\n$client-\u003ewrite('EHLO [127.0.0.1]');\n\necho $client-\u003eread();\n```\n\n_In the above example, a simple smtp connection to gmail is made._\n\n## Credits\n\n- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) \u003c\u003cinfo@muhammetsafak.com.tr\u003e\u003e\n\n## License\n\nCopyright © 2022 [MIT License](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finitphp%2Fsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finitphp%2Fsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finitphp%2Fsocket/lists"}