{"id":22303126,"url":"https://github.com/calderonsamuel/sseparser","last_synced_at":"2025-07-28T05:35:00.100Z","repository":{"id":211156247,"uuid":"728358175","full_name":"calderonsamuel/SSEparser","owner":"calderonsamuel","description":"Parse Server-Sent Events in R","archived":false,"fork":false,"pushed_at":"2024-01-08T20:32:39.000Z","size":4010,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T00:35:53.260Z","etag":null,"topics":["http-requests","parser","r","stream"],"latest_commit_sha":null,"homepage":"https://calderonsamuel.github.io/SSEparser/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/calderonsamuel.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":".github/SUPPORT.md","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-06T19:13:52.000Z","updated_at":"2024-01-06T16:45:00.000Z","dependencies_parsed_at":"2024-01-08T21:45:45.501Z","dependency_job_id":"325f565a-59f0-4940-bcc4-19e2fc84365f","html_url":"https://github.com/calderonsamuel/SSEparser","commit_stats":null,"previous_names":["calderonsamuel/sseparser"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/calderonsamuel/SSEparser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calderonsamuel%2FSSEparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calderonsamuel%2FSSEparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calderonsamuel%2FSSEparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calderonsamuel%2FSSEparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/calderonsamuel","download_url":"https://codeload.github.com/calderonsamuel/SSEparser/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calderonsamuel%2FSSEparser/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267468561,"owners_count":24092337,"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-28T02:00:09.689Z","response_time":68,"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":["http-requests","parser","r","stream"],"created_at":"2024-12-03T18:42:31.509Z","updated_at":"2025-07-28T05:35:00.072Z","avatar_url":"https://github.com/calderonsamuel.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# SSEparser\n\n\u003c!-- badges: start --\u003e\n[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)\n[![CRAN status](https://www.r-pkg.org/badges/version/SSEparser)](https://CRAN.R-project.org/package=SSEparser)\n[![R-CMD-check](https://github.com/calderonsamuel/SSEparser/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/calderonsamuel/SSEparser/actions/workflows/R-CMD-check.yaml)\n[![Codecov test coverage](https://codecov.io/gh/calderonsamuel/SSEparser/branch/main/graph/badge.svg)](https://app.codecov.io/gh/calderonsamuel/SSEparser?branch=main)\n[![CRAN RStudio mirror downloads](https://cranlogs.r-pkg.org/badges/last-month/SSEparser?color=blue)](https://r-pkg.org/pkg/SSEparser)\n\u003c!-- badges: end --\u003e\n\nThe goal of SSEparser is to provide robust functionality to parse Server-Sent Events and to build on top of it.\n\n## Installation\n\nYou can install `SEEparser` from CRAN like so:\n\n```r\ninstall.packages(\"SSEparser\")\n```\n\nAlternatively, you can install the development version like so:\n\n``` r\npak::pak(\"calderonsamuel/SSEparser\")\n```\n\n## Example\n\nThe `parse_sse()` function takes a string containing a server-sent event and converts it to a R list.\n\n```{r example}\nlibrary(SSEparser)\n\nevent \u003c- \"data: test\\nevent: message\\nid: 123\\n\\n\"\n\nparse_sse(event)\n\n```\n\nComments are usually received in a line starting with a colon. They are not parsed.\n\n```{r}\nwith_comment \u003c- \"data: test\\n: comment\\nevent: example\\n\\n\"\n\nparse_sse(with_comment)\n```\n\n## Use in HTTP requests\n\n`parse_sse()` wraps the `SSEparser` R6 class, which is also exported to be used with real-time streaming data. The following code handles a request with MIME type \"text/event-stream\".\n\n```{r}\nparser \u003c- SSEparser$new()\nresponse \u003c- httr2::request(\"https://postman-echo.com/server-events/3\") %\u003e%\n\thttr2::req_body_json(data = list(\n\t\tevent = \"message\",\n\t\trequest = \"POST\"\n\t)) %\u003e%\n\thttr2::req_perform_stream(callback = \\(x) {\n\t\tevent \u003c- rawToChar(x)\n\t\tparser$parse_sse(event)\n\t\tTRUE\n\t})\n\nstr(parser$events)\n```\n\n## Extending SSEparser\n\nFollowing the previous example, it should be useful to parse the content of every `data` field to be also an R list instead of a JSON string. For that, we can create a new R6 class which inherits from `SSEparser`. We just need to overwrite the `append_parsed_sse()` method.\n\n```{r}\nCustomParser \u003c- R6::R6Class(\n\tclassname = \"CustomParser\",\n\tinherit = SSEparser,\n\tpublic = list(\n\t\tinitialize = function() {\n\t\t\tsuper$initialize()\n\t\t},\n\t\tappend_parsed_sse = function(parsed_event) {\n\t\t\tparsed_event$data \u003c- jsonlite::fromJSON(parsed_event$data)\n\t\t\tself$events = c(self$events, list(parsed_event))\n\t\t\tinvisible(self)\n\t\t}\n\t)\n)\n```\n\nNotice that the only thing we are modifying is the parsing of the data field, not the parsing of the event itself. This is the original method from `SSEparser`:\n\n```{r}\nSSEparser$public_methods$append_parsed_sse\n```\n\n\n`CustomParser` uses `jsonlite::fromJSON()` to parse the data field of every chunk in the event stream. We can now use our custom class with the previous request[^1].\n\n[^1]: This endpoint returns random event field names for each chunk in every request, so the response will not be exactly the same.\n\n```{r}\nparser \u003c- CustomParser$new()\nresponse \u003c- httr2::request(\"https://postman-echo.com/server-events/3\") %\u003e%\n\thttr2::req_body_json(data = list(\n\t\tevent = \"message\",\n\t\trequest = \"POST\"\n\t)) %\u003e%\n\thttr2::req_perform_stream(callback = \\(x) {\n\t\tevent \u003c- rawToChar(x)\n\t\tparser$parse_sse(event)\n\t\tTRUE\n\t})\n\nstr(parser$events)\n```\n\nNow instead of a JSON string we can have an R list in the data field **while the stream is still in process**.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalderonsamuel%2Fsseparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcalderonsamuel%2Fsseparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalderonsamuel%2Fsseparser/lists"}