Skip to content

Commit

Permalink
add meteorite gem
Browse files Browse the repository at this point in the history
  • Loading branch information
llawlor committed Jul 1, 2015
1 parent 65c799f commit adb20f1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ gem 'uglifier', '>= 1.3.0'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'

gem 'meteorite'

group :development, :test do
gem 'spring'
end
Expand Down
16 changes: 16 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ GEM
tzinfo (~> 1.1)
arel (6.0.0)
builder (3.2.2)
em-hiredis (0.3.0)
eventmachine (~> 1.0)
hiredis (~> 0.5.0)
em-websocket (0.5.1)
eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0)
erubis (2.7.0)
eventmachine (1.0.7)
execjs (2.5.2)
globalid (0.3.5)
activesupport (>= 4.1.0)
hiredis (0.5.2)
http_parser.rb (0.6.0)
i18n (0.7.0)
jquery-rails (4.0.3)
rails-dom-testing (~> 1.0)
Expand All @@ -53,6 +62,11 @@ GEM
nokogiri (>= 1.5.9)
mail (2.6.3)
mime-types (>= 1.16, < 3)
meteorite (0.1.0)
em-hiredis
em-websocket
railties
redis
mime-types (2.6.1)
mini_portile (0.6.2)
minitest (5.7.0)
Expand Down Expand Up @@ -86,6 +100,7 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.4.2)
redis (3.2.1)
ref (1.0.5)
spring (1.3.6)
sprockets (3.2.0)
Expand All @@ -111,6 +126,7 @@ PLATFORMS

DEPENDENCIES
jquery-rails
meteorite
rails (= 4.2.1)
spring
sqlite3
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/meteorite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "redis"

# configure your Redis server
$redis = Redis.new(url: "redis://127.0.0.1:6379")
56 changes: 56 additions & 0 deletions daemons/websocket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'em-websocket'
require 'em-hiredis'
require 'json'

$clients = []

EM.run do
puts "starting websocket server. . ."

redis = EM::Hiredis.connect #("redis://127.0.0.1:6379")
pubsub = redis.pubsub

# when a pubsub message is received
pubsub.on(:message) do |channel, message|
puts [:message, channel, message]
# for each client
$clients.each do |client|
# send a message
client.send({bind_key: channel, bind_data: message}.to_json)
end
end

EM::WebSocket.run(:host => "0.0.0.0", :port => 8080) do |ws|
ws.onopen do |handshake|
puts "WebSocket connection open"

# add the client
$clients.push(ws)
# Access properties on the EM::WebSocket::Handshake object, e.g.
# path, query_string, origin, headers

# Publish message to the client
#ws.send "Hello Client, you connected to #{handshake.path}"
end

ws.onclose do
puts "Connection closed"
$clients.delete(ws)
end

ws.onmessage do |msg|
puts "Received message: #{msg}"
#ws.send "Pong: #{msg}"

json = JSON.parse(msg)

if json['action'] == 'subscribe'
# subscribe
pubsub.subscribe(json['key'])
puts "subscribe to: #{json['key']}"
end
end

end

end

0 comments on commit adb20f1

Please sign in to comment.