https://github.com/calderonsamuel/sseparser
Parse Server-Sent Events in R
Science Score: 13.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (17.4%) to scientific vocabulary
Keywords
http-requests
parser
r
stream
Last synced: 5 months ago
·
JSON representation
Repository
Parse Server-Sent Events in R
Basic Info
- Host: GitHub
- Owner: calderonsamuel
- License: other
- Language: R
- Default Branch: main
- Homepage: https://calderonsamuel.github.io/SSEparser/
- Size: 3.82 MB
Statistics
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
- Releases: 1
Topics
http-requests
parser
r
stream
Created about 2 years ago
· Last pushed about 2 years ago
Metadata Files
Readme
Changelog
Contributing
License
Code of conduct
Support
README.Rmd
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# SSEparser
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=SSEparser)
[](https://github.com/calderonsamuel/SSEparser/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/calderonsamuel/SSEparser?branch=main)
[](https://r-pkg.org/pkg/SSEparser)
The goal of SSEparser is to provide robust functionality to parse Server-Sent Events and to build on top of it.
## Installation
You can install `SEEparser` from CRAN like so:
```r
install.packages("SSEparser")
```
Alternatively, you can install the development version like so:
``` r
pak::pak("calderonsamuel/SSEparser")
```
## Example
The `parse_sse()` function takes a string containing a server-sent event and converts it to a R list.
```{r example}
library(SSEparser)
event <- "data: test\nevent: message\nid: 123\n\n"
parse_sse(event)
```
Comments are usually received in a line starting with a colon. They are not parsed.
```{r}
with_comment <- "data: test\n: comment\nevent: example\n\n"
parse_sse(with_comment)
```
## Use in HTTP requests
`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".
```{r}
parser <- SSEparser$new()
response <- httr2::request("https://postman-echo.com/server-events/3") %>%
httr2::req_body_json(data = list(
event = "message",
request = "POST"
)) %>%
httr2::req_perform_stream(callback = \(x) {
event <- rawToChar(x)
parser$parse_sse(event)
TRUE
})
str(parser$events)
```
## Extending SSEparser
Following 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.
```{r}
CustomParser <- R6::R6Class(
classname = "CustomParser",
inherit = SSEparser,
public = list(
initialize = function() {
super$initialize()
},
append_parsed_sse = function(parsed_event) {
parsed_event$data <- jsonlite::fromJSON(parsed_event$data)
self$events = c(self$events, list(parsed_event))
invisible(self)
}
)
)
```
Notice 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`:
```{r}
SSEparser$public_methods$append_parsed_sse
```
`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].
[^1]: This endpoint returns random event field names for each chunk in every request, so the response will not be exactly the same.
```{r}
parser <- CustomParser$new()
response <- httr2::request("https://postman-echo.com/server-events/3") %>%
httr2::req_body_json(data = list(
event = "message",
request = "POST"
)) %>%
httr2::req_perform_stream(callback = \(x) {
event <- rawToChar(x)
parser$parse_sse(event)
TRUE
})
str(parser$events)
```
Now instead of a JSON string we can have an R list in the data field **while the stream is still in process**.
Owner
- Name: Samuel Calderon
- Login: calderonsamuel
- Kind: user
- Location: Lima
- Website: www.samuelenrique.com
- Twitter: samucalse
- Repositories: 9
- Profile: https://github.com/calderonsamuel
Peruvian political scientist
GitHub Events
Total
Last Year
Committers
Last synced: 7 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| calderonsamuel | s****n@u****e | 36 |
Committer Domains (Top 20 + Academic)
uarm.pe: 1
Issues and Pull Requests
Last synced: 7 months ago
All Time
- Total issues: 1
- Total pull requests: 0
- Average time to close issues: 25 days
- Average time to close pull requests: N/A
- Total issue authors: 1
- Total pull request authors: 0
- Average comments per issue: 0.0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- calderonsamuel (1)
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- cran 633 last-month
- Total dependent packages: 1
- Total dependent repositories: 0
- Total versions: 1
- Total maintainers: 1
cran.r-project.org: SSEparser
Parse Server-Sent Events
- Homepage: https://github.com/calderonsamuel/SSEparser
- Documentation: http://cran.r-project.org/web/packages/SSEparser/SSEparser.pdf
- License: MIT + file LICENSE
-
Latest release: 0.1.0
published about 2 years ago
Rankings
Dependent packages count: 28.6%
Dependent repos count: 36.7%
Average: 50.4%
Downloads: 85.9%
Maintainers (1)
Last synced:
7 months ago
Dependencies
.github/workflows/R-CMD-check.yaml
actions
- actions/checkout v3 composite
- r-lib/actions/check-r-package v2 composite
- r-lib/actions/setup-pandoc v2 composite
- r-lib/actions/setup-r v2 composite
- r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pkgdown.yaml
actions
- JamesIves/github-pages-deploy-action v4.4.1 composite
- actions/checkout v3 composite
- r-lib/actions/setup-pandoc v2 composite
- r-lib/actions/setup-r v2 composite
- r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pr-commands.yaml
actions
- actions/checkout v3 composite
- r-lib/actions/pr-fetch v2 composite
- r-lib/actions/pr-push v2 composite
- r-lib/actions/setup-r v2 composite
- r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml
actions
- actions/checkout v3 composite
- actions/upload-artifact v3 composite
- r-lib/actions/setup-r v2 composite
- r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION
cran
- magrittr * imports
- testthat >= 3.0.0 suggests