Skip to content

Commit

Permalink
Send Lesson Notification via Sockets
Browse files Browse the repository at this point in the history
When Email notification is sent out about an upcoming lesson, send
the lesson to a Node server (which would be responsible for forwarding
the lesson to mobile devices).
  • Loading branch information
donaldali committed May 5, 2015
1 parent 9c61b1c commit 09c7d4e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Expand Up @@ -62,6 +62,9 @@ gem 'modular-scale', '1.0.6'
gem 'jquery-rails'
gem 'chronic'

# Allow use of sockets for mobile support
gem 'socket.io-client-simple'

group :test do
gem "factory_girl_rails"
gem "timecop"
Expand Down
11 changes: 11 additions & 0 deletions Gemfile.lock
Expand Up @@ -87,6 +87,7 @@ GEM
mail (~> 2.2)
equalizer (0.0.9)
erubis (2.7.0)
event_emitter (0.2.5)
execjs (2.3.0)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -265,6 +266,11 @@ GEM
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
slop (3.6.0)
socket.io-client-simple (1.1.3)
event_emitter
httparty
json
websocket-client-simple (>= 0.2.1)
sprockets (2.12.3)
hike (~> 1.2)
multi_json (~> 1.0)
Expand Down Expand Up @@ -305,6 +311,10 @@ GEM
webmock (1.20.4)
addressable (>= 2.3.6)
crack (>= 0.3.2)
websocket (1.2.2)
websocket-client-simple (0.2.2)
event_emitter
websocket
websocket-driver (0.5.1)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.1)
Expand Down Expand Up @@ -356,6 +366,7 @@ DEPENDENCIES
sidekiq
simplecov
sinatra
socket.io-client-simple
sqlite3
timecop
twitter
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/users_controller.rb
Expand Up @@ -39,6 +39,8 @@ def notify_subscribers
flash[:notice] = "Subscribers notified but error sending tweet, \
perhaps it was too long?"
end

emit_lesson_notification_on_socket lesson
lesson.update_attribute(:notification_sent_at, Time.now)
else
flash[:error] =
Expand Down Expand Up @@ -91,4 +93,18 @@ def authenticate_sendgrid_webhook
return head status: 401
end
end

# Send the lesson (for which a notification is being sent) to a Node
# server with socket.io; the Node server will then send the lesson to
# all mobile devices subscribed to a channel
def emit_lesson_notification_on_socket lesson
server_addr = 'https://rs-socketio.herokuapp.com'
socket = SocketIO::Client::Simple.connect server_addr
message = lesson.as_json

socket.emit 'lessonNotification', message
socket.on :connect do
socket.emit 'lessonNotification', message
end
end
end

0 comments on commit 09c7d4e

Please sign in to comment.