magicbus 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +16 -0
- data/README.md +0 -0
- data/lib/magicbus.rb +115 -0
- data/lib/magicbus/version.rb +5 -0
- metadata +50 -0
data/LICENSE
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
MagicBus - A Sinatra-ready RabbitMQ wrapper for message routing with AMQP.
|
2
|
+
|
3
|
+
This program is free software: you can redistribute it and/or modify
|
4
|
+
it under the terms of the GNU General Public License as published by
|
5
|
+
the Free Software Foundation, either version 3 of the License, or
|
6
|
+
(at your option) any later version.
|
7
|
+
|
8
|
+
This program is distributed in the hope that it will be useful,
|
9
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
GNU General Public License for more details.
|
12
|
+
|
13
|
+
You should have received a copy of the GNU General Public License
|
14
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
15
|
+
|
16
|
+
Author: Louis-Antoine Mullie (louis.mullie@gmail.com). Copyright 2013.
|
data/README.md
ADDED
File without changes
|
data/lib/magicbus.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
module MagicBus
|
2
|
+
|
3
|
+
def self.connect(params = { host: '127.0.0.1' })
|
4
|
+
EM.next_tick do
|
5
|
+
Publisher.publisher = AMQP.connect(*params)
|
6
|
+
Subscriber.connection = AMQP.connect(*params)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Publisher
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :publisher
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.scatter(group, action, model, &block)
|
17
|
+
|
18
|
+
MagicBus::Subscriber.connected_users(group).each do |user_id|
|
19
|
+
user = User.find(user_id)
|
20
|
+
self.send_to(user_id, action, model, block.call(user))
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.broadcast(group, action, model, data = nil)
|
26
|
+
|
27
|
+
MagicBus::Subscriber.connected_users(group).each do |user_id|
|
28
|
+
self.send_to(user_id, action, model, data)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.send_to(user_id, action, model, data)
|
34
|
+
|
35
|
+
warn 'Sending message to client'
|
36
|
+
|
37
|
+
data = { action: action,
|
38
|
+
model: model, data: data }
|
39
|
+
|
40
|
+
self.publish(user_id, data)
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def self.publish(user_id, data)
|
47
|
+
channel = Subscriber.channels[user_id]
|
48
|
+
exchange = channel.default_exchange
|
49
|
+
exchange.publish(data.to_json, routing_key: user_id)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
class Subscriber
|
55
|
+
|
56
|
+
class << self
|
57
|
+
attr_accessor :connection
|
58
|
+
attr_accessor :channels
|
59
|
+
attr_accessor :clients
|
60
|
+
end
|
61
|
+
|
62
|
+
self.channels, self.clients = {}, {}
|
63
|
+
|
64
|
+
def self.connected_users(group)
|
65
|
+
|
66
|
+
connected_users = []
|
67
|
+
|
68
|
+
group.users.each do |user|
|
69
|
+
if self.clients[user.id.to_s]
|
70
|
+
connected_users << user.id.to_s
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
connected_users
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.subscribe(user_id, out)
|
79
|
+
|
80
|
+
unless channels[user_id]
|
81
|
+
|
82
|
+
channels[user_id] = AMQP::Channel.new(connection)
|
83
|
+
channel = channels[user_id]
|
84
|
+
|
85
|
+
queue = channel.queue(user_id, auto_delete: true)
|
86
|
+
|
87
|
+
queue.subscribe do |payload|
|
88
|
+
self.clients[user_id].each do |client|
|
89
|
+
client << "data: #{payload}\n\n"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
self.clients[user_id] ||= []
|
96
|
+
self.clients[user_id] << out
|
97
|
+
self.clients[user_id].length
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.unsubscribe(user_id, client_id)
|
102
|
+
|
103
|
+
if self.clients[user_id][client_id]
|
104
|
+
self.clients[user_id].delete_at(client_id)
|
105
|
+
end
|
106
|
+
|
107
|
+
if self.clients[user_id].length == 0
|
108
|
+
self.clients.unsubscribe(user_id)
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: magicbus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Louis Mullie
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-22 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! ' A thin wrapper around RabbitMQ designed for delivery of push notifications
|
15
|
+
to web clients. '
|
16
|
+
email:
|
17
|
+
- louis.mullie@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/magicbus/version.rb
|
23
|
+
- lib/magicbus.rb
|
24
|
+
- README.md
|
25
|
+
- LICENSE
|
26
|
+
homepage: https://github.com/asocial/magicbus
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.25
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: Sinatra-ready RabbitMQ wrapper for message routing with AMQP.
|
50
|
+
test_files: []
|