Created
October 2, 2014 12:14
-
-
Save sha1sum/4fd12cd38be18b7fb9c2 to your computer and use it in GitHub Desktop.
Rails Initializer for GroupMe Faye Sockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Originally set up as a Rails initializer, but can be adapted to use elsewhere. | |
# Requires the faye gem | |
require 'eventmachine' | |
Thread.new do | |
EM.run { | |
::GroupMeClient = Faye::Client.new 'https://push.groupme.com/faye' | |
class GroupMeClientAuth | |
def outgoing(message, callback) | |
unless message['channel'] == '/meta/subscribe' | |
return callback.call(message) | |
end | |
message['ext'] ||= {} | |
message['ext']['access_token'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
message['ext']['timestamp'] = Time.now.to_i | |
callback.call(message) | |
end | |
end | |
GroupMeClient.add_extension GroupMeClientAuth.new | |
# | |
class GroupMeGroupSubscription | |
def initialize(group_id) | |
GroupMeClient.unsubscribe "/group/#{group_id}" | |
GroupMeClient.subscribe "/group/#{group_id}" do |message| | |
# --------------- ON MESSAGE EVENT CODE HERE ------------------ | |
end | |
end | |
end | |
class GroupMeUserSubscription | |
def initialize(user_id) | |
GroupMeClient.unsubscribe "/user/#{user_id}" | |
GroupMeClient.subscribe "/user/#{user_id}" do |message| | |
# --------------- ON MESSAGE EVENT CODE HERE ------------------ | |
end | |
end | |
end | |
# Subscribe to a group's channel | |
GroupMeGroupSubscription.new '123456789' | |
# Subscribe to a user's channel | |
GroupMeUserSubscription.new '123456789' | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment