{"id":19435988,"url":"https://github.com/ralscha/sse-eventbus","last_synced_at":"2025-04-05T20:09:31.501Z","repository":{"id":54276134,"uuid":"73617051","full_name":"ralscha/sse-eventbus","owner":"ralscha","description":"EventBus library for sending events from a Spring appliction to the web browser with SSE","archived":false,"fork":false,"pushed_at":"2025-03-29T08:40:33.000Z","size":484,"stargazers_count":83,"open_issues_count":2,"forks_count":28,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-29T19:08:16.396Z","etag":null,"topics":["java","server-sent-events","spring"],"latest_commit_sha":null,"homepage":null,"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/ralscha.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":"2016-11-13T13:18:59.000Z","updated_at":"2025-03-29T08:40:36.000Z","dependencies_parsed_at":"2023-01-30T19:15:57.938Z","dependency_job_id":"93902573-9e2f-44ee-932d-d404a53f7875","html_url":"https://github.com/ralscha/sse-eventbus","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralscha%2Fsse-eventbus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralscha%2Fsse-eventbus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralscha%2Fsse-eventbus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralscha%2Fsse-eventbus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ralscha","download_url":"https://codeload.github.com/ralscha/sse-eventbus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393573,"owners_count":20931813,"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","server-sent-events","spring"],"created_at":"2024-11-10T15:08:45.882Z","updated_at":"2025-04-05T20:09:31.472Z","avatar_url":"https://github.com/ralscha.png","language":"Java","funding_links":[],"categories":["进程间通信"],"sub_categories":["Spring Cloud框架"],"readme":"[![Test Status](https://github.com/ralscha/sse-eventbus/actions/workflows/maven.yml/badge.svg)](https://github.com/ralscha/sse-eventbus/actions/workflows/maven.yml)\n\n\nsse-eventbus is a Java library that sits on top of [Spring's Sever-Sent Event support](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-async-sse).   \nIt keeps track of connected clients and broadcasts events to them.\n\n## Usage\n\n\n### Setup server\n\nEnable support by adding ```@EnableSseEventBus``` to a Spring application.\n```\n@SpringBootApplication\n@EnableSseEventBus\npublic class Application {\n  ...\n}\n```\n\nCreate a controller that handles the SSE requests and returns a [SseEmitter](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.html).\nEach client has to provide an id that identifies this client. \nThe controller then registers the client in the eventBus with the method ```registerClient``` and\nsubscribes it to events with the ```subscribe``` method.\nThe SseEventBus class contains a convenient method ```createSseEmitter``` that does all of this. \n\n```\n@Controller\npublic class SseController {\n  private final SseEventBus eventBus;\n  public SseController(SseEventBus eventBus) {\n    this.eventBus = eventBus;\n  }\n\n  @GetMapping(\"/register/{id}\")\n  public SseEmitter register(@PathVariable(\"id\") String id) {\n    SseEmitter emitter = new SseEmitter(180_000L);\n    emitter.onTimeout(emitter::complete);\n    this.eventBus.registerClient(id, emitter);\n    this.eventBus.subscribe(id, SseEvent.DEFAULT_EVENT);\n    return emitter;\n\n    //OR\n    //return this.eventBus.createSseEmitter(id, SseEvent.DEFAULT_EVENT)\n  }\n}\n```\n\n### Setup client\n\nOn the client side an application interacts with the [EventSource](https://developer.mozilla.org/en/docs/Web/API/EventSource) object.\nThis object is responsible for sending the SSE request to the server and calling listeners\nthe application registered on this object. \nAs mentioned before the client has to send an id that should be unique among all the clients. \nA simple way is to use libraries like [node-uuid](https://github.com/kelektiv/node-uuid) that generates UUIDs.\n\n```\nconst uuid = uuid();\nconst eventSource = new EventSource(`/register/${uuid}`);\neventSource.addEventListener('message', response =\u003e {\n  //handle the response from the server\n  //response.data contains the data line \n}, false);\n```\n\n\n### Broadcasting events\n\nTo broadcast an event to all connected clients a Spring application can either inject the SseEventBus \nsingleton and call the ```handleEvent``` method \n\n```\n@Service\npublic class DataEmitterService {\n  private final SseEventBus eventBus;\n  public DataEmitterService(SseEventBus eventBus) {\n    this.eventBus = eventBus;\n  }\n\n  public void broadcastEvent() {\n    this.eventBus.handleEvent(SseEvent.ofData(\"some useful data\"));\n  }\n\n}\n\n```\n\nor use Spring's event infrastructure and publish a SseEvent\n\n```\n@Service\npublic class DataEmitterService {\n  private final ApplicationEventPublisher eventPublisher;\n  // OR: private final ApplicationContext ctx;\n  // this class implements the ApplicationEventPublisher interface\n  public DataEmitterService(ApplicationEventPublisher eventPublisher) {\n    this.eventPublisher = eventPublisher;\n  }\n\n  public void broadcastEvent() {\n    this.eventPublisher.publishEvent(SseEvent.ofData(\"some useful data\"));\n  }\n}\n```\n\n\n## Maven\nThe library is hosted on the Central Maven Repository\n```\n  \u003cdependency\u003e\n    \u003cgroupId\u003ech.rasc\u003c/groupId\u003e\n    \u003cartifactId\u003esse-eventbus\u003c/artifactId\u003e\n    \u003cversion\u003e2.0.0\u003c/version\u003e\n  \u003c/dependency\u003e  \n```\n\n## Demo\nSimple demo application:    \nhttps://github.com/ralscha/sse-eventbus-demo\n\nIonic Demo Chat application:    \nhttps://github.com/ralscha/sse-eventbus-demo-chat\n\n\n## More information\nArticles about Server-Sent Events    \n* https://hpbn.co/server-sent-events-sse/   \n* https://www.html5rocks.com/en/tutorials/eventsource/basics/\n\n\n## Browser Support\nSSE is supported in most browsers. The notable exceptions are the browsers from Microsoft IE and Edge.   \nhttp://caniuse.com/#feat=eventsource\n\nFortunately it is possible to polyfill the SSE support where it's missing. \n\n* **[EventSource](https://github.com/remy/polyfills/blob/master/EventSource.js)** by Remy Sharp\n* **[jQuery.EventSource](http://github.com/rwldrn/jquery.eventsource)** by Rick Waldron\n* **[EventSource](https://github.com/Yaffle/EventSource)** by Yaffle\n* **[EventSource](https://github.com/amvtek/EventSource)** by AmvTek\n\n\n## Changelog\n\n### 2.0.0 - December 6, 2022\n  * Upgrade to Spring 6 (javax. -\u003e jakarta.)\n\n\n### 1.1.9 - February 18, 2020\n  * Catch and log exceptions in event loop. Prevents the loop to terminate.\n\n\n### 1.1.8 - December 21, 2019\n  * Resolves [Issue #13](https://github.com/ralscha/sse-eventbus/issues/13): Add lifecycle hooks\n  \n  * Resolves [Issue #12](https://github.com/ralscha/sse-eventbus/issues/12): Hide ImmutableSseEvent completely from public API\n\n\n### 1.1.7 - May 24, 2018\n  * Resolves [Issue #8](https://github.com/ralscha/sse-eventbus/issues/8): Fix handling messages containing a new line character \\n\n  \n  * Resolves [Issue #6](https://github.com/ralscha/sse-eventbus/issues/6): Make members of DefaultSseEventBusConfiguration protected for easier sub classing \n  \n\n### 1.1.6 - March 21, 2018\n  * Change client expiration job to fixed delay and add separate configuration for this delay. By default it is 1 day, you change this value by implementing\n    `SseEventBusConfigurer.clientExpirationJobDelay` \n\n\n### 1.1.5 - January 7, 2018\n  * Extract subscription registry code out of the SseEventBus class into the interface SubscriptionRegistry and the class DefaultSubscriptionRegistry. \n    This allows a project to customize the existing implementation or write their own implementation. To\n  override the default implementation add a Spring managed bean of type SubscriptionRegistry to your project.   \n\n  Example:\n  ```\n  @Component\n  public class CustomSubscriptionRegistry extends DefaultSubscriptionRegistry {\n\n    @Override\n    public boolean isClientSubscribedToEvent(String clientId, String eventName) {\n      return super.isClientSubscribedToEvent(clientId, eventName)\n          || super.isClientSubscribedToEvent(clientId, \"*\");\n    }\n  }  \n  ```\n  \n\n### 1.1.4 - December 15, 2017\n  * Resolves [Issue #2](https://github.com/ralscha/sse-eventbus/issues/2). Make sure that your project depends on Spring 4.3.13 or newer.\n\n\n### 1.1.3 - September 12, 2017\n  * Add the following public methods to the SseEventBus class to query events and subscribers.\n     * Set\u003cString\u003e getAllClientIds()\n     * Set\u003cString\u003e getAllEvents()\n     * Map\u003cString, Set\u003cString\u003e\u003e getAllSubscriptions()\n     * Set\u003cString\u003e getSubscribers(String event)\n     * int countSubscribers(String event)\n     * boolean hasSubscribers(String event)\n\n\n### 1.1.2 - July 16, 2017\n  * Add a workaround for the Microsoft Edge browser where the polyfill no longer work correctly.\n  The createSseEmitter method supports an additional parameter that tells the library to complete (close) the connection after sending a message.\n  This way the system behaves like long polling instead of http streaming.\n  ```\n  boolean completeAfterMessage = true;\n  eventBus.createSseEmitter(\"client1\", 180_000L, true, completeAfterMessage, \"event1\", \"event2\");\n  ```\n\n\n### 1.1.1 - July 8, 2017\n  * Add support for automatic unregister clients from events during registering.\n  ```SseEventBus.createSseEmitter``` supports an additional boolean parameter. If true the method\n  subscribes the client to the provided events and unsubscribes it from all other currently subscribed events.\n  \n    ```eventBus.createSseEmitter(\"client1\", 180_000L, true, \"event1\", \"event2\");```    \n    After this call the client is only subscribed to ```event1``` and ```event2```.\n  \n    *...later in the application...*   \n\n    ```eventBus.createSseEmitter(\"client1\", 180_000L, true, \"event1\");```    \n    After this call the client is only subscribed to ```event1```. The method automatically unregistered the client from ```event2```.\n\n\n### 1.1.0 - April 28, 2017\n  * Add support for Jackson JSON View.\n    ```SseEvent.builder().event(\"eventName\").data(dataObj).jsonView(JsonViews.PUBLIC.class).build()```   \n  To support that the interface ```ch.rasc.sse.eventbus.DataObjectConverter``` changed. \n  Instead of the ```data``` object the two methods receive the ```SseEvent``` object.    \n  ```1.0.x:  boolean supports(Object object);  String convert(Object object);```    \n  ```1.1.x:  boolean supports(SseEvent event); String convert(SseEvent event);```    \n  To get the data object your code can call ```event.data()```.\n \n\n### 1.0.1 - March 31, 2017\n  * Add support for excluding clients with the ```addExcludeClientId``` method.\n    ``` \n    SseEvent.builder().addExcludeClientId(\"2\")\n          .event(\"eventName\")\n          .data(\"payload\")\n          .build();\n    ```\n    \n\n### 1.0.0 - November 19, 2016\n  * Initial release\n\n\n## License\nCode released under [the Apache license](http://www.apache.org/licenses/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fralscha%2Fsse-eventbus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fralscha%2Fsse-eventbus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fralscha%2Fsse-eventbus/lists"}