{"id":21566354,"url":"https://github.com/onevroad/net-websocket-spring-boot","last_synced_at":"2025-07-09T19:37:09.596Z","repository":{"id":37164333,"uuid":"187737453","full_name":"onevroad/net-websocket-spring-boot","owner":"onevroad","description":"Easy to use websocket","archived":false,"fork":false,"pushed_at":"2023-05-24T08:35:16.000Z","size":162,"stargazers_count":10,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-05T01:41:46.497Z","etag":null,"topics":["java","netty","spring-boot","websocket"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/onevroad.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":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-21T01:15:08.000Z","updated_at":"2023-05-23T13:24:13.000Z","dependencies_parsed_at":"2024-11-24T10:36:25.643Z","dependency_job_id":null,"html_url":"https://github.com/onevroad/net-websocket-spring-boot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/onevroad/net-websocket-spring-boot","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onevroad%2Fnet-websocket-spring-boot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onevroad%2Fnet-websocket-spring-boot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onevroad%2Fnet-websocket-spring-boot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onevroad%2Fnet-websocket-spring-boot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/onevroad","download_url":"https://codeload.github.com/onevroad/net-websocket-spring-boot/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onevroad%2Fnet-websocket-spring-boot/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263675036,"owners_count":23494602,"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":["java","netty","spring-boot","websocket"],"created_at":"2024-11-24T10:24:45.022Z","updated_at":"2025-07-09T19:37:09.523Z","avatar_url":"https://github.com/onevroad.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# net-websocket-spring-boot-starter [![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)\n\nThe WebSocket is based on netty. It's easier to use for somebody who want to build a WebSocket in a short time.\n\nSupport jdk version 1.8 or 1.8+\n\n[中文文档](https://github.com/onevroad/net-websocket-spring-boot/blob/master/README_cn.md \"中文文档\")\n\n## Request Data Format\n```json\n{\n  \"e\": \"event type\",\n  \"t\": [\"topic name\"],\n  \"d\": \"data\"\n}\n```\n- e:event, t:topic, d:data\n- support event type：subscribe, message, cancel, heartbeat\n- Except for the heartbeat event, the topic is a required param.You can send multiple topics at the same time.\n- You can customize the response data format.\n\n## Heartbeat Event\n- The server will send a heartbeat event when the client and server have no data interaction within 1 minute. The data format that the server send is like this:\n```json\n{\n  \"e\": \"heartbeat\",\n  \"d\": \"ping\"\n}\n```\n- If the client receive a heartbeat event, please send a data to the server. The data format is like this:\n```json\n{\n  \"e\": \"heartbeat\",\n  \"d\": \"pong\"\n}\n```\n- The connection will be disconnected when the server who sent the heartbeat event twice din't receive any response.\n\n## Quick Start\n- add maven dependency\n```\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.onevroad\u003c/groupId\u003e\n    \u003cartifactId\u003enet-websocket-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e0.4.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n- You have two choices to handle event, add annotation or implement WebSocketEventHandler\u003cRequest, Response\u003e or WebSocketCustomizeEventHandler\u003cRequest, Response\u003e for every topic that you have.\n\n### Add annotation\nAdd the annotation on the method to handle event.  \nYou can define the topic for every annotation.\nThe priority of topic is: method annotation topic \u003e class annotation topic \nThe supporting params is: topic and data. You need add RequestTopic annotation for the topic and add RequestData annotation for the data.\n```java\n@WebSocketListener(\"test-annotation\")\npublic class SampleMessageAnnotationEventHandler {\n\n    @OnSubscribe(\"test-annotation-subscribe\")\n    public String onSubscribe(@RequestTopic String topic, @RequestData String data) {\n        return \"subscribe success!\";\n    }\n\n    @OnMessage\n    public String onMessage(@RequestTopic String topic, @RequestData String data) {\n        return \"message received!\";\n    }\n\n    @OnCancel(\"test-annotation-cancel\")\n    public String onCancel(@RequestTopic String topic, @RequestData String data) {\n        return \"cancel success!\";\n    }\n}\n```\n\n### Implement interface\nFor the definite topics, you can implement WebSocketEventHandler\u003cRequest, Response\u003e with the WebsocketListener annotation.\n```java\n@WebsocketListener(\"test\")\npublic class SampleMessageEventHandler implements WebSocketEventHandler\u003cString, String\u003e {\n    @Override\n    public String onSubscribe(String topic, String data) {\n        return \"subscribe success!\";\n    }\n\n    @Override\n    public String onMessage(String topic, String data) {\n        return \"message received!\";\n    }\n\n    @Override\n    public String onCancel(String topic, String data) {\n        return \"cancel success!\";\n    }\n}\n```\nFor the dynamic topics, you can implement WebSocketCustomizeEventHandler\u003cRequest, Response\u003e and override the equalsTopic method.\n```java\n@WebsocketListener\npublic class SampleMessageCustomizeEventHandler implements WebSocketCustomizeEventHandler\u003cString, String\u003e {\n    @Override\n    public boolean equalsTopic(String topic) {\n        return \"test2\".equals(topic);\n    }\n\n    @Override\n    public String onSubscribe(String topic, String data) {\n        return \"subscribe success!\";\n    }\n\n    @Override\n    public String onMessage(String topic, String data) {\n        return \"message received!\";\n    }\n\n    @Override\n    public String onCancel(String topic, String data) {\n        return \"cancel success!\";\n    }\n}\n```\n\n- configure the scan package where your implementers are.\n```java\n@EnableAutoConfiguration\n@WebSocketScan(basePackages = {\"org.net.websocket.samples.handler\"})\npublic class WebSocketApplication {\n\n    public static void main(String[] args) {\n        new SpringApplicationBuilder(WebSocketApplication.class).run(args);\n    }\n}\n```\n\n- configure parameter\n```yaml\nnet:\n  websocket:\n    # Listening port\n    port: 80\n    # The number of listening threads, default 1 thread\n    boss-group-threads: 1\n    # The number of working threads, default 0 is the number of CPU cores\n    worker-group-threads: 0\n    # Request path\n    end-point: /ws\n    # Retry config\n    retry:\n      # Whether to enable the retry\n      enable: true\n      # The maximum number of retry times, the default is 3 times\n      max-retry-time: 3\n      # Retry interval, unit: milliseconds, default 1000ms\n      retry-interval: 1000\n```\n\n- send message\n\nYou can use the method of the WebSocketMessagePublisher class to send your message.\n```java\npublic class SendMessageHandler {\n    public static void send(String topic, String message) {\n        WebSocketMessagePublisher.publish(topic, message);\n    }\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonevroad%2Fnet-websocket-spring-boot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fonevroad%2Fnet-websocket-spring-boot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonevroad%2Fnet-websocket-spring-boot/lists"}