A Crystal shard for Server-Sent Events

Releases:

  • v0.6.0 - August 29, 2019
  • v0.5.0 - July 18, 2019
  • v0.4.0 - June 27, 2019
  • v0.3.0 - June 12, 2019
  • v0.2.0 - November 12, 2018

Owner:

server-sent-events

Build Status Releases

Server-Sent Events server/client for Crystal.

Installation

  1. Add the dependency to your shard.yml:
dependencies:
  sse:
    github: y2k2mt/sse.cr
    version: 0.6.0
  1. Run shards install

Usage

Client

sse = HTTP::ServerSentEvents::EventSource.new("http://127.0.0.1:8080")

sse.on_message do |message|
  # Receiving messages from server
  p message.data
end

sse.run

Server

server = HTTP::Server.new [
  HTTP::ServerSentEventsHandler.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: bar

data: foo
data: bar

...

Contributing

  1. Fork it (https://github.com/y2k2mt/sse.cr/fork)
  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 - creator and maintainer
Github statistic:
  • 3
  • 0
  • 1
  • 1
  • 0
  • 22 days ago

License:

MIT License

Links: