Skip to content

Commit

Permalink
Merge pull request #1 from rubyengineer/implement-server-side-subscri…
Browse files Browse the repository at this point in the history
…ption

Implement server side event stream subscription
  • Loading branch information
kpheasey committed May 19, 2016
2 parents 629320a + 9be7bd2 commit 9f97671
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ And then execute:

$ bundle

After bundling you need to run the initial installation generator:
After bundling you need to run the initial installation generator for javascript client usage:

$ rails g shower:install

## Configuration

Create `config/initializers/redis.rb` and configure Redis connection:

$redis = Redis.new(host: '127.0.0.1', port: 6379)

## Usage
### Publishing

Expand All @@ -35,6 +41,14 @@ JSON using ```to_json```.

Shower::Stream.publish('message.new', { username: 'anon', message: 'hello!' })

### Subscription

Subscribing to an event stream on server side.

Shower::Stream.subscribe(['message.new']) do |event, data|
# do something with event
# do something with data
end

### Listening

Expand Down
13 changes: 12 additions & 1 deletion lib/shower/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,16 @@ def self.publish(event, message)
$redis.publish(event, message.to_json)
end

# Yield event and data back to subscribed entity
#
# @param [Array] events
def self.subscribe(events)
$redis.subscribe(events << 'heartbeat') do |on|
on.message do |event, data|
yield(event, data)
end
end
end

end
end
end

0 comments on commit 9f97671

Please sign in to comment.