{"id":13806790,"url":"https://github.com/aesteve/vertx-sse","last_synced_at":"2025-07-31T09:39:48.080Z","repository":{"id":31538663,"uuid":"35103238","full_name":"aesteve/vertx-sse","owner":"aesteve","description":"Add support for Server-Sent-Events in Vert.x Web","archived":false,"fork":false,"pushed_at":"2019-12-04T10:06:19.000Z","size":275,"stargazers_count":47,"open_issues_count":3,"forks_count":13,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-06T17:51:40.356Z","etag":null,"topics":["java","server-sent-events","sse","vertx","vertx-web"],"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/aesteve.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-05T14:01:14.000Z","updated_at":"2025-01-30T03:31:09.000Z","dependencies_parsed_at":"2022-09-09T11:40:15.181Z","dependency_job_id":null,"html_url":"https://github.com/aesteve/vertx-sse","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/aesteve/vertx-sse","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aesteve%2Fvertx-sse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aesteve%2Fvertx-sse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aesteve%2Fvertx-sse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aesteve%2Fvertx-sse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aesteve","download_url":"https://codeload.github.com/aesteve/vertx-sse/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aesteve%2Fvertx-sse/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268017357,"owners_count":24181669,"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-07-31T02:00:08.723Z","response_time":66,"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":["java","server-sent-events","sse","vertx","vertx-web"],"created_at":"2024-08-04T01:01:16.199Z","updated_at":"2025-07-31T09:39:48.056Z","avatar_url":"https://github.com/aesteve.png","language":"Java","funding_links":[],"categories":["Integration"],"sub_categories":[],"readme":"[![Travis](https://img.shields.io/travis/aesteve/vertx-sse.svg)](https://travis-ci.org/aesteve/vertx-sse)\n[![Codecov](https://img.shields.io/codecov/c/github/aesteve/vertx-sse.svg)](https://codecov.io/gh/aesteve/vertx-sse)\n\n# vertx-sse\n\nAdd Server-Sent-Events support to Vert.x by providing a vertx-web handler that deals with all the stuff required for server-sent-events.\n\nCurrently trying to integrate into vertx-web. If you have some time to help, just contact me, thanks.\n\n## Server-side\n\nSimply\n\n```java\nyourRouter.get(\"/sse\").handler(SSEHandler.create());\n```\nBut it won't do anything if you don't attach any handler to it.\n\n```java\nSSEHandler sse = SSEHandler.create();\nsse.connectHandler(connection -\u003e {\n  // connection is an SSEConnection object you can push messages into\n  // it wraps the HttpServerRequest used to establish the connection\n});\nsse.closeHandler(connection -\u003e {\n  // the same connection object as in the connect handler\n});\n```\n\n### Example 1 : simply sends some data once the client is connected\n\n```java\nSSEHandler sse = SSEHandler.create();\nsse.connectHandler(sseConnection -\u003e {\n  sseConnection.data(\"Welcome!\");\n});\nyourRouter.get(\"/sse\").handler(sse);\n```\n\n### Example 2 : send events periodically and keep track of connected users\n\n```java\nSSEHandler pingSSE = SSEHandler.create();\nMap\u003cSSEConnection, Long\u003e timersPerConnection = new HashMap\u003cSSEConnection, Long\u003e();\npingSSE.connectHandler(sseConnection -\u003e {\n  Long timerId = vertx.setPeriodic(1000, tId -\u003e {\n      sseConnection.data(\"ping ! \"+ new Date().getTime());\n  });\n  timersPerConnection.put(sseConnection, timerId);\n});\npingSSE.closeHandler(sseConnection -\u003e {\n    Long timerId = timersPerConnection.get(sseConnection);\n    if (timerId != null) {\n        vertx.cancelTimer(timerId);\n    }\n});\nyourRouter.get(\"/sse\").handler(pingSSE);\n```\n\nYou can also reject a connection and specify an http status code / message.\n\n```java\nString magicToken = \"theOneThatRulesThemAll\";\nSSEHandler sse = SSEHandler.create();\nsse.connectHandler(sseConnection -\u003e {\n   HttpServerRequest request = sseConnection.request();\n   String token = request.getParam(\"authenticationToken\");\n   if (token == null) {\n     sseConnection.reject(401);\n   } else if (!magicToken.equals(token)) {\n     sseConnection.reject(403);\n   }\n});\nyourRouter.get(\"/sse\").handler(sse);\n```\n\n### Forwarding the event-bus\n\nThis project also provides the ability to forward messages from Vert.x's event-bus to a `SSEConnection`\n\n\n```java\nSSEHandler sse = SSEHandler.create();\nsse.connectHandler(sseConnection -\u003e {\n    sseConnection.forward(\"some-eventbus-address\");\n});\nyourRouter.get(\"/sse\").handler(sse);\n```\n\nThen every message published to `some-eventbus-address` will be forwarded to every event source connected to `/sse`\n \n### Event-Bus Bridge\n\nOn top of the `forward` capability, this project comes with a very simple way to \"bridge\" event-bus messages onto SSE connections.\n\n```java\nyourRouter.get(\"/sse/*\").handler(EventBusSSEBridge.create());\n```\n\nIf an event-source connects to `/sse/foo/bar` then every message published on `/sse/foo/bar` will be propagated to the the event source.\n\nThis is handy, but in practice, you'll probably need a mapping between the event-source URL and the event-bus address.\n\nIn this case, you can just use : \n\n```java\nEventBusSSEBridge bridge = EventBusSSEBridge.create();\nbridge.mapping(request -\u003e request.getParam(\"ticker\"))\nyourRouter.get(\"/stocks/nasdaq/:ticker\").handler(bridge);\n```\n\nNow, if an event-source connects on `/stocks/nasdaq/aapl` it will receive every message published on the address `aapl`.\nYou're free to create any mapping you need based on the incomoing event-source URL.\n\n\n\n\nFor more advanced use cases, you can have a look at the tests for inspiration.\n\n## Client side\n\nThis project also provides a simple `EventSource` object that mimics the html5 EventSource API. So that you can create your own SSE clients programmatically.\n\n### Example 1 : Establish connection\n\n```java\nHttpClientOptions options = new HttpClientOptions();\noptions.setDefaultHost(\"localhost\");\noptions.setDefaultPort(9000);\nEventSource eventSource = EventSource.create(vertx, options);\neventSource.connect(connectHandler -\u003e {\n   if (connectHandler.succeeded()) {\n       // Yay ! you'll be able to receive events from the server\n       eventSource.onMessage(msg -\u003e {\n         System.out.println(\"Message received : \"+msg);\n       });\n   } else {\n     ConnectionRefusedException cre = (ConnectionRefuseException)connectHandler.cause();\n     System.out.println(\"Server dropped me because : \" + cre.statusCode() + \" and he told me : \"+cre.statusMessage());\n   }\n});\n```\n\nYou can register handlers on the `EventSource` object by calling : \n* onMessage\n* onEvent\n\nor ask for the last id sent by the server : `eventSource.lastId()` if you want to keep track of message ids (especially if you want to connect later).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faesteve%2Fvertx-sse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faesteve%2Fvertx-sse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faesteve%2Fvertx-sse/lists"}