Project

sse-rails

0.0
No commit activity in last 3 years
No release in over 3 years
SSE for rails, made easy.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

sse-rails Gem Version Build Status Code Climate

sse-rails is a simple wrapper around ActionController::Live to hide all the complexity of streaming.

Installation

Add this line to your application's Gemfile:

gem 'sse-rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sse-rails

Usage

Add these lines to a controller:

require 'rails/sse'

include ActionController::Live
include Rails::SSE

And then use it in one of your actions:

def listen
  stream do |channel|
    channel.post({ username: 'jim' }, event: 'refresh')
  end
end

You can also use Rails::SSE::Channel#ping! to see if connection is still open. This is useful when you are in a loop like this:

def listen
  stream do |channel|
    loop do
      channel.post('anything') if condition

      channel.ping!
      sleep 1
    end
  end
end

Without pinging you would know that the connection was lost only when the condition becomes true.

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 new Pull Request