{"id":17127972,"url":"https://github.com/rohanrhu/cebsocket","last_synced_at":"2025-04-13T06:31:57.952Z","repository":{"id":90318569,"uuid":"321087382","full_name":"rohanrhu/cebsocket","owner":"rohanrhu","description":"Lightweight WebSocket library for C.","archived":false,"fork":false,"pushed_at":"2021-12-17T17:21:16.000Z","size":16,"stargazers_count":85,"open_issues_count":0,"forks_count":7,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-05T14:54:27.779Z","etag":null,"topics":["c","websocket","websocket-library","websocket-server"],"latest_commit_sha":null,"homepage":"","language":"C","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/rohanrhu.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}},"created_at":"2020-12-13T14:44:35.000Z","updated_at":"2024-11-21T11:44:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"a1cdc6d1-f24c-44e4-89b5-9b2d3566a27e","html_url":"https://github.com/rohanrhu/cebsocket","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohanrhu%2Fcebsocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohanrhu%2Fcebsocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohanrhu%2Fcebsocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohanrhu%2Fcebsocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rohanrhu","download_url":"https://codeload.github.com/rohanrhu/cebsocket/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248674658,"owners_count":21143760,"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":["c","websocket","websocket-library","websocket-server"],"created_at":"2024-10-14T19:05:53.813Z","updated_at":"2025-04-13T06:31:57.479Z","avatar_url":"https://github.com/rohanrhu.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `cebsocket`: a lightweight websocket library for C\nCebsocket is a lightweight websocket library for C.\n\n## Usage\nUsage is easy and simple. You can look to `examples/` directory. You can build examples with `make` command.\n\n### Simple WebSocket Server\nHere is an example for creating simple WebSocket server.\n\n```C\nvoid on_data(cebsocket_clients_t* client, char* data) {\n    printf(\"WebSocket Message: %s\\n\", data);\n    \n    char answer[500];\n    sprintf(answer, \"Answer to client: %s\", data);\n\n    cebsocket_send(client, answer);\n\n    sprintf(answer, \"Answer to broadcast: %s\", data);\n\n    cebsocket_send_broadcast(client, answer);\n}\n\nvoid on_connected(cebsocket_clients_t* client) {\n    printf(\"Client connected #%d\\n\", client-\u003eid);\n}\n\nvoid on_disconnected(cebsocket_clients_t* client) {\n    printf(\"Client disconnected #%d\\n\", client-\u003eid);\n}\n\nint main() {\n    printf(\"Starting WebSocket server..\\n\");\n\n    cebsocket_t* ws = cebsocket_init(8080);\n\n    ws-\u003eon_data = on_data;\n    ws-\u003eon_connected = on_connected;\n    ws-\u003eon_disconnected = on_disconnected;\n    \n    cebsocket_listen(ws);\n    \n    return 0;\n}\n```\n\n## What about HTTP?\nCebsocket is designed to only handle WebSocket requests as a HTTP server. You can use it with Apache's [mod_proxy_ws_tunnel](https://httpd.apache.org/docs/current/mod/mod_proxy_wstunnel.html).\n\n## What about SSL?\nAlso you can use Apache's [mod_proxy_ws_tunnel](https://httpd.apache.org/docs/current/mod/mod_proxy_wstunnel.html) for SSL.\n\n## Build\nBuilding is simple just do `make`.\n\n```bash\nmake clean; make\n```\n\nYou will see `websocket.o`. You can use it like:\n\n```bash\ngcc -o hello hello.c websocket.o\n./hello\n```\n\n## Events\n### Thread Safety\nEvent handler functions get called from client thread so you must be sure for they are **thread-safe**.\n\n### `void on_connected(cebsocket_clients_t* client)`\nCalled when a `client` connected.\n\n### `void on_disconnected(cebsocket_clients_t* client)`\nCalled when a `client` disconnected.\n\n### `void on_message(cebsocket_clients_t* client, char* data)`\nCalled when a message is arrived from `client`.\n\n## Functions\n### `extern cebsocket_t* cebsocket_init(int port)`\nCreates WebSocket server instance.\n\n### `extern void cebsocket_listen(cebsocket_t* ws)`\nStarts listening new connections.\n\n### `extern void cebsocket_send(cebsocket_clients_t* client, char* message)`\nSends `message` to `client`.\n\n### `extern void cebsocket_send_broadcast(cebsocket_clients_t* client, char* message)`\nSends `message` to boradcast of `client`.\n\n### `extern void cebsocket_send_all(cebsocket_t* ws, char* message)`\nSends `message` to all clients.\n\n## Iterating Clients\nSince `ws-\u003eclients` is a linked-list, you can iterate it like the following example.\n\n```c\ncebsocket_clients_t* _client = ws-\u003eclients;\n\nwhile (_client) {\n    cebsocket_send(_client, message);\n    _client = _client-\u003enext;\n}\n```\n\n## Types\n### `cebsocket_t`\nThe WebSocket server instance.\n\n```C\ntypedef struct cebsocket {\n    int port;\n    char* host_address;\n    char* bind_address;\n    cebsocket_clients_t* clients;\n    cebsocket_clients_t* current_client;\n    void (*on_data)(cebsocket_clients_t*, char*);\n    void (*on_connected)(cebsocket_clients_t*);\n    void (*on_disconnected)(cebsocket_clients_t*);\n} cebsocket_t;\n```\n\n### `cebsocket_clients_t`\nThe WebSocket client instance. It is also a linked-list.\n\n```C\ntypedef struct cebsocket_clients {\n    cebsocket_t* ws;\n    int id;\n    int socket;\n    int server_socket;\n    int address;\n    char* ws_key;\n    void* data;\n    cebsocket_clients_t* prev;\n    cebsocket_clients_t* next;\n} cebsoket_clients_t;\n```\n\n## License\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frohanrhu%2Fcebsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frohanrhu%2Fcebsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frohanrhu%2Fcebsocket/lists"}