Skip to content
This repository has been archived by the owner on Feb 1, 2018. It is now read-only.

Add hooks #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ application, and serving them to your clients.
keep in mind that a client will receive events of all types from their
channels. To handle access rights, use channels instead.

* **Hooks** in order to do custom actions during the (dis)connection of the
clients to the server.

## Rationale

Redisse’s design comes from these requirements:
Expand Down
44 changes: 44 additions & 0 deletions lib/redisse/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,50 @@ def self.channels(*, &block)
end
end

# Public: Define a custom piece of code to run during a new connection.
#
# Calls the given block with a Rack environment.
#
# block - The block we want to run at client connection
#
# Examples
#
# Redisse.on_connect do |env|
# puts "A new client just connected to Redisse"
# end
#
# Redisse.on_connect({})
# # => "A new client just connected to Redisse"
def self.on_connect(env, &block)
if block
@on_connect = block
elsif @on_connect
@on_connect.call(env)
end
end

# Public: Define a custom piece of code to run during a disconnection.
#
# Calls the given block with a Rack environment.
#
# block - The block we want to run at client disconnection
#
# Examples
#
# Redisse.on_disconnect do |env|
# puts "A new client just disconnected from Redisse"
# end
#
# Redisse.on_disconnect({})
# # => "A new client just disconnected from Redisse"
def self.on_disconnect(env, &block)
if block
@on_disconnect = block
elsif @on_disconnect
@on_disconnect.call(env)
end
end

self.redis_server = ENV['REDISSE_REDIS'] ||
'redis://localhost:6379/'
self.default_port = ENV['REDISSE_PORT'] ||
Expand Down
2 changes: 2 additions & 0 deletions lib/redisse/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def response(env)
channels = Array(redisse.channels(env))
return not_found if channels.empty?
subscribe(env, channels) or return service_unavailable
redisse.on_connect(env)
history_events(env, channels)
heartbeat(env)
streaming_response(200, {
Expand All @@ -80,6 +81,7 @@ def response(env)
end

def on_close(env)
redisse.on_disconnect(env)
unsubscribe(env)
stop_heartbeat(env)
end
Expand Down