{"id":20505018,"url":"https://github.com/gemblue/phpwebsocket","last_synced_at":"2025-04-13T21:02:37.120Z","repository":{"id":62509649,"uuid":"263265466","full_name":"gemblue/PHPWebsocket","owner":"gemblue","description":"Simple PHP Websocket Library / Server for Fun","archived":false,"fork":false,"pushed_at":"2020-05-28T05:08:23.000Z","size":19,"stargazers_count":18,"open_issues_count":1,"forks_count":11,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-13T21:02:10.229Z","etag":null,"topics":["php-socket","php-websocket","socket-programming","websocket"],"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/gemblue.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}},"created_at":"2020-05-12T07:32:05.000Z","updated_at":"2024-10-01T04:39:00.000Z","dependencies_parsed_at":"2022-11-02T12:32:47.660Z","dependency_job_id":null,"html_url":"https://github.com/gemblue/PHPWebsocket","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/gemblue%2FPHPWebsocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gemblue%2FPHPWebsocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gemblue%2FPHPWebsocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gemblue%2FPHPWebsocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gemblue","download_url":"https://codeload.github.com/gemblue/PHPWebsocket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248782274,"owners_count":21160717,"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-socket","php-websocket","socket-programming","websocket"],"created_at":"2024-11-15T19:42:34.434Z","updated_at":"2025-04-13T21:02:37.088Z","avatar_url":"https://github.com/gemblue.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHPWebsocket\n\nSimple PHP Websocket Library / Server for Fun. Just making a wrapper from PHP Socket API. Modifying source code from \nreference and making simple OOP for a clean code.\n\n## Dependency\n\nPHP Socket Library\nhttps://www.php.net/manual/en/book.sockets.php\n\n## Install\n\nCreate a exercise folder. Open it and run composer require.\n\n```\ncomposer require gemblue/php-websocket\n```\n\n## Run Server\n\n- Make server file executable\n\n```\nsudo chmod +x ./vendor/gemblue/php-websocket/bin/server\n```\n\n- Run websocket server with port option\n\n```\n./vendor/gemblue/php-websocket/bin/server port:3000\n```\n\nThen it will show success output like \n\n```\nListening incoming request on port 3000 ..\n```\n\n## Prepare client.\n\nCreate a HTML file for websocket client, for example `index.html` :\n\n```html\n\u003clink rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css\"\u003e\n\n\u003cdiv class=\"container\"\u003e\n\n\t\u003cdiv class=\"card mt-5 mb-3\"\u003e\n\t\t\u003cdiv class=\"card-body\"\u003e\n\t\t\t\u003cdiv id=\"output\"\u003e\u003c/div\u003e\n\t\t\u003c/div\u003e\n\t\u003c/div\u003e\n\n\t\u003cdiv class=\"form-group\"\u003e\n\t\t\u003clabel\u003eNama\u003c/label\u003e\n\t\t\u003cinput id=\"name\" class=\"form-control\"/\u003e\n\t\u003c/div\u003e\n\n\t\u003cdiv class=\"form-group\"\u003e\n\t\t\u003clabel\u003ePesan\u003c/label\u003e\n\t\t\u003ctextarea id=\"message\" class=\"form-control\"\u003e\u003c/textarea\u003e\n\t\u003c/div\u003e\n\n\t\u003cbutton id=\"btn-send\" class=\"btn btn-success\"\u003eSend\u003c/button\u003e\n\n\u003c/div\u003e\n\n\u003cscript src=\"https://code.jquery.com/jquery-3.5.1.min.js\"\u003e\u003c/script\u003e\n\n\u003cscript\u003e  \n\tfunction showMessage(messageHTML) {\n\t\t$('#output').append(messageHTML);\n\t}\n\n\t$(document).ready(function(){\n\t\tvar websocket = new WebSocket(\"ws://127.0.0.1:3000\");\n\t\twebsocket.onopen = function(event) {\n\t\t\tshowMessage(\"\u003cdiv class='text-success'\u003eBerhasil masuk room ..\u003c/div\u003e\");\t\t\n\t\t}\n\t\twebsocket.onmessage = function(event) {\n\t\t\tvar Data = JSON.parse(event.data);\n\t\t\tshowMessage(\"\u003cdiv\u003e\"+Data.message+\"\u003c/div\u003e\");\n\t\t\t$('#message').val('');\n\t\t};\n\t\t\n\t\twebsocket.onerror = function(event){\n\t\t\tshowMessage(\"\u003cdiv\u003eProblem due to some Error\u003c/div\u003e\");\n\t\t};\n\t\twebsocket.onclose = function(event){\n\t\t\tshowMessage(\"\u003cdiv\u003eConnection Closed\u003c/div\u003e\");\n\t\t}; \n\t\t\n\t\t$('#btn-send').on(\"click\",function(event){\n\t\t\tevent.preventDefault();\n\t\t\tvar messageJSON = {\n\t\t\t\tname: $('#name').val(),\n\t\t\t\tmessage: $('#message').val()\n\t\t\t};\n\t\t\twebsocket.send(JSON.stringify(messageJSON));\n\t\t});\n\t});\n\u003c/script\u003e\n```\n\nOpen `index.html` with your browser. Use 2 tab/browser for simulation. Output will be like this :\n\n![Sample](https://i.ibb.co/PGgH8vy/screenshot-ibb-co-2020-05-14-19-17-38.png)\n\n## Reference\n\n- https://stackoverflow.com/questions/42955033/php-client-web-socket-to-send-messages/43121475\n- https://phppot.com/php/simple-php-chat-using-websocket\n- https://medium.com/@cn007b/super-simple-php-websocket-example-ea2cd5893575\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgemblue%2Fphpwebsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgemblue%2Fphpwebsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgemblue%2Fphpwebsocket/lists"}