Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/y2k2mt/sse.cr
A Crystal shard for Server-Sent Events
https://github.com/y2k2mt/sse.cr
crystal-language server-sent-events
Last synced: 15 days ago
JSON representation
A Crystal shard for Server-Sent Events
- Host: GitHub
- URL: https://github.com/y2k2mt/sse.cr
- Owner: y2k2mt
- License: mit
- Created: 2018-11-02T08:22:21.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2023-01-29T06:45:05.000Z (almost 2 years ago)
- Last Synced: 2024-10-04T21:33:18.218Z (about 1 month ago)
- Topics: crystal-language, server-sent-events
- Language: Crystal
- Homepage:
- Size: 57.6 KB
- Stars: 22
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - sse.cr - [Server-Sent Events](https://www.w3.org/TR/2009/WD-eventsource-20090421) client (HTTP)
- awesome-crystal - sse.cr - [Server-Sent Events](https://html.spec.whatwg.org/multipage/server-sent-events.html) client (HTTP)
README
# Server-Sent Events
[![Build Status](https://travis-ci.org/y2k2mt/sse.cr.svg?branch=master)](https://travis-ci.org/y2k2mt/sse.cr)
[![Releases](https://img.shields.io/github/release/y2k2mt/sse.cr.svg?maxAge=360)](https://github.com/y2k2mt/sse.cr/releases)[Server-Sent Events](https://www.w3.org/TR/eventsource/) server/client for Crystal.
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
sse:
github: y2k2mt/sse.cr
```2. Run `shards install`
## Usage
### Client
```crystal
require "sse"sse = HTTP::ServerSentEvents::EventSource.new("http://127.0.0.1:8080")
sse.on_message do |message|
# Receiving messages from server
p message.data
endsse.run
```### Server
```crystal
require "sse"server = HTTP::Server.new [
HTTP::ServerSentEvents::Handler.new { |es, _|
es.source {
# Delivering event data every 1 second.
sleep 1
HTTP::ServerSentEvents::EventMessage.new(
data: ["foo", "bar"],
)
}
},
]server.bind_tcp "127.0.0.1", 8080
server.listen
```Running server and you can get then:
```
$ curl 127.0.0.1:8080 -H "Accept: text/event-stream"data: foo
data: bardata: foo
data: bar...
```
## Contributing
1. Fork it ()
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request## Contributors
- [y2k2mt](https://github.com/y2k2mt) - creator and maintainer