{"id":23672899,"url":"https://github.com/php-ion/websocket-parser","last_synced_at":"2025-09-02T00:32:42.031Z","repository":{"id":99858110,"uuid":"50023036","full_name":"php-ion/websocket-parser","owner":"php-ion","description":"Streaming  websocket frame parser and frame builder for c","archived":false,"fork":false,"pushed_at":"2019-05-08T14:07:02.000Z","size":39,"stargazers_count":67,"open_issues_count":2,"forks_count":22,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-03T05:12:20.817Z","etag":null,"topics":["builder","c","parser","rfc6455","websocket","websocket-parser"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/php-ion.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,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-01-20T10:33:23.000Z","updated_at":"2025-01-25T14:11:48.000Z","dependencies_parsed_at":"2023-05-10T18:45:13.384Z","dependency_job_id":null,"html_url":"https://github.com/php-ion/websocket-parser","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/php-ion/websocket-parser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-ion%2Fwebsocket-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-ion%2Fwebsocket-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-ion%2Fwebsocket-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-ion%2Fwebsocket-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/php-ion","download_url":"https://codeload.github.com/php-ion/websocket-parser/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-ion%2Fwebsocket-parser/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273213769,"owners_count":25065058,"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","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["builder","c","parser","rfc6455","websocket","websocket-parser"],"created_at":"2024-12-29T11:28:45.992Z","updated_at":"2025-09-02T00:32:42.021Z","avatar_url":"https://github.com/php-ion.png","language":"C","readme":"WebSocket frame parser and builder\n----------------------------------\n\nThis is a parser and builder for WebSocket messages (see [RFC6455](https://tools.ietf.org/html/rfc6455)) written in C.\n\nTable of Contents\n-----------------\n\n* [Features](#features)\n* [Status](#status)\n* [Usage](#usage)\n* [Frame builder](#frame-builder)\n* [UUID](#uuid)\n* [Frame example](#frame-example)\n\nFeatures\n--------\n\n* Fast parsing and building of websocket messages\n* No dependencies\n* No internal buffering\n* No need to buffer the whole frame — works with chunks of a data\n* No syscalls\n* No allocations\n* It can be interrupted at anytime\n\nTested as part of [PHP-ION](https://github.com/php-ion/php-ion) extension.\n\nInspired by [http-parser](https://github.com/joyent/http-parser) by [Ryan Dahl](https://github.com/ry)\nand [multipart-parser](https://github.com/iafonov/multipart-parser-c) by [Igor Afonov](https://github.com/iafonov).\n\nStatus\n------\n\nProduction ready.\n\nUsage\n-----\n\nUse [http-parser](https://github.com/joyent/http-parser) for parsing headers. This library parse only websocket frames.\n\nThis parser library works with several callbacks, which the user may set up at application initialization time.\n\n```c\nwebsocket_parser_settings settings;\n\nwebsocket_parser_settings_init(\u0026settings);\n\nsettings.on_frame_header = websocket_frame_header;\nsettings.on_frame_body = websocket_frame_body;\nsettings.on_frame_end = websocket_frame_end;\n```\n\nThese functions must match the signatures defined in the websocket-parser header file.\n\nReturning a value other than 0 from the callbacks will abort message processing.\n\nOne websocket_parser object is used per TCP connection. Initialize `websocket_parser` struct using `websocket_parser_init()` and set callbacks:\n\n```c\nwebsocket_parser_settings settings;\n\nwebsocket_parser_settings_init(\u0026settings);\n\nsettings.on_frame_header = websocket_frame_header;\nsettings.on_frame_body   = websocket_frame_body;\nsettings.on_frame_end    = websocket_frame_end;\n\nparser = malloc(sizeof(websocket_parser));\nwebsocket_parser_init(parser);\n// Attention! Sets your \u003cdata\u003e after websocket_parser_init\nparser-\u003edata = my_frame_struct;\n```\n\nBasically, callback looks like that:\n\n```c\nint websocket_frame_header(websocket_parser * parser) {\n    parser-\u003edata-\u003eopcode = parser-\u003eflags \u0026 WS_OP_MASK; // gets opcode\n    parser-\u003edata-\u003eis_final = parser-\u003eflags \u0026 WS_FIN;   // checks is final frame\n    if(parser-\u003elength) {\n        parser-\u003edata-\u003ebody = malloc(parser-\u003elength);   // allocate memory for frame body, if body exists\n    }\n    return 0;\n}\n\nint ion_websocket_frame_body(websocket_parser * parser, const char *at, size_t size) {\n    if(parser-\u003eflags \u0026 WS_HAS_MASK) {\n        // if frame has mask, we have to copy and decode data via websocket_parser_copy_masked function\n        websocket_parser_decode(\u0026parser-\u003edata-\u003ebody[parser-\u003eoffset], at, length, parser);\n    } else {\n        memcpy(\u0026parser-\u003edata-\u003ebody[parser-\u003eoffset], at, length);\n    }\n    return 0;\n}\n\nint websocket_frame_end(websocket_parser * parser) {\n    my_app_push_frame(parser-\u003edata); // use parsed frame\n}\n```\n\nWhen data is received execute the parser and check for errors.\n\n```c\nsize_t nread;\n// .. init settitngs and parser ... \n\nnread = websocket_parser_execute(parser, \u0026settings, data, data_len);\nif(nread != data_len) {\n    // some callback return a value other than 0\n}\n\n// ...\nfree(parser);\n```\n\nFrame builder\n-------------\n\nTo calculate how many bytes to allocate for a frame, use the `websocket_calc_frame_size` function:\n\n```c\nsize_t frame_len = websocket_calc_frame_size(WS_OP_TEXT | WS_FINAL_FRAME | WS_HAS_MASK, data_len);\nchar * frame = malloc(sizeof(char) * frame_len);\n```\n\nAfter that you can build a frame\n\n```c\nwebsocket_build_frame(frame, WS_OP_TEXT | WS_FINAL_FRAME | WS_HAS_MASK, mask, data, data_len);\n```\n\nand send binary string to the socket\n\n```c\nwrite(sock, frame, frame_len);\n```\n\nUUID\n----\n\nMacros WEBSOCKET_UUID contains unique ID for handshake\n\n```c\n#define WEBSOCKET_UUID   \"258EAFA5-E914-47DA-95CA-C5AB0DC85B11\"\n```\n\nFrame example\n-------------\n\nThere is binary websocket frame example:\n\n* Raw frame: `\\x81\\x8Amask\\x0B\\x13\\x12\\x06\\x08\\x41\\x17\\x0A\\x19\\x00`\n* Has mask: yes\n* Mask: `mask`\n* Payload: `frame data`\n* Fin: yes\n* Opcode: `WS_OP_TEXT`\n","funding_links":[],"categories":["C"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-ion%2Fwebsocket-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-ion%2Fwebsocket-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-ion%2Fwebsocket-parser/lists"}