dorsum-events

Version, currently main branch1 version
  • main branchlatestJul 8, 2024

github.com/funfairin/dorsum-events

Listens to Redis Streams and forwards them to an HTTP client using server-sent events

0 stars
0 dependents
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  dorsum-events:
    github: funfairin/dorsum-events
    branch: main

main is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
no constraint declared
License
MIT

Dependencies

Runtime Dependencies

  • redis*github: stefanwille/crystal-redis

README

# Dorsum-events

Listens to Redis Streams and forwards them to an HTTP client using server-sent events.

The SSE can be requested by setting the `Accept` header to `text/event-stream` and then requesting the channel in the request path.

For example, when the channel is `moths` you can test with the following:

    curl -H "Accept: text/event-stream" http://localhost:9110/moths

When you want to start the stream from a specific event id, you can either set it throught the `last-event-id` query parameter or the `Last-Event-ID` request header.

    curl -H "Accept: text/event-stream" http://localhost:9110/moths?last-event-id=1675014118178-0

## Redis Stream assumptions

We make a few assumptions to the Redis Stream to simplify the implementation. The name of the field in the stream is the event type and the value should be a single String. Usually that means a JSON document.

    XADD moths * individuals '{"name":"yellow"}'

Will translate to an SSE that looks something like this:

    id: 1675018066245-0
    event: individuals
    data: {"name":"yellow"}

## Future

* Automated trimming and using XRANGE with COUNT on requests without a last-event-id to keep performance acceptable.

## Setup

Setting up is probably easiest in Caddy:

```
example.com {
	@events header Accept text/event-stream
	handle @events {
			reverse_proxy 127.0.0.1:9110
	}
}
```

Caddy will automatically turn off response buffering when it detects the `text/event-stream` content-type. Turning on gzip or other output filters will enable response buffering and break SSE streaming, so be careful.