redis_message_capsule 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +71 -109
- data/lib/redis_message_capsule/version.rb +1 -1
- data/lib/redis_message_capsule.rb +3 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -1,133 +1,95 @@
|
|
1
1
|
# RedisMessageCapsule
|
2
2
|
|
3
3
|
Send messages between node or rails apps asynchronously (via redis).
|
4
|
+
|
4
5
|
* [report an issue with the ruby version] (https://github.com/arbind/redis_message_capsule/issues)
|
5
6
|
* [report an issue with the node version] (https://github.com/arbind/redis_message_capsule-node/issues)
|
6
7
|
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
$ npm install redis-message-capsule
|
10
|
-
$ gem install redis_message_capsule
|
11
|
-
Note that the javascript version is named with dashes, ruby with underscore
|
8
|
+
## Installation
|
12
9
|
|
13
|
-
|
14
|
-
RedisMessageCapsule is used in the same way for both node and ruby (just the syntax is different, naturally).
|
10
|
+
**in node**:
|
15
11
|
|
16
|
-
|
12
|
+
$ npm install redis_message_capsule
|
17
13
|
|
18
|
-
|
19
|
-
> load RedisMessageCapsule
|
20
|
-
> capsule = materializeCapsule(redisURL, dbNumber)
|
14
|
+
**in ruby**:
|
21
15
|
|
22
|
-
|
23
|
-
> catChannel = capsule.materializeChannel('cat')
|
16
|
+
$ gem install redis_message_capsule
|
24
17
|
|
25
|
-
|
26
|
-
|
27
|
-
> catChannel.send('roooaaar')
|
18
|
+
## Usage
|
19
|
+
The purpose of RedisMessageCapsule is to enable separate apps to communicate with each other asynchronously.
|
28
20
|
|
29
|
-
|
30
|
-
|
31
|
-
> catChannel.register(messageHandler)
|
21
|
+
### Make sure redis is running:
|
22
|
+
The code below defaults to using redis running on localhost
|
32
23
|
|
33
|
-
|
24
|
+
### Open 2 terminal windows to simulate any mixture of ruby or node apps:
|
25
|
+
Both windows can be ruby, can be node, or one can be ruby and one can be node.
|
34
26
|
|
35
|
-
|
27
|
+
### To Send Messages From Window 1
|
28
|
+
**in node**:
|
36
29
|
|
37
|
-
|
30
|
+
$ node
|
31
|
+
require('redis_message_capsule')
|
32
|
+
redisurl = 'redis://127.0.0.1:6379/'
|
33
|
+
capsule = RedisMessageCapsule.capsule(redisurl)
|
34
|
+
cat = capsule.channel('cat')
|
35
|
+
cat.send('meow')
|
36
|
+
cat.send('meow')
|
38
37
|
|
39
|
-
|
40
|
-
However, nothing restricts the same app from doing the sending as well as receiving its own message.
|
38
|
+
**in ruby**:
|
41
39
|
|
42
|
-
|
43
|
-
|
40
|
+
$ irb
|
41
|
+
require('redis_message_capsule')
|
42
|
+
redisurl = 'redis://127.0.0.1:6379/'
|
43
|
+
capsule = RedisMessageCapsule.capsule(redisurl)
|
44
|
+
cat = capsule.channel('cat')
|
45
|
+
cat.send('meow')
|
46
|
+
cat.send('meow')
|
44
47
|
|
45
|
-
|
46
|
-
|
48
|
+
### To Receive messages In Window 2
|
49
|
+
**in node**:
|
47
50
|
|
48
|
-
|
51
|
+
$ node
|
52
|
+
require('redis_message_capsule')
|
53
|
+
redisurl = 'redis://127.0.0.1:6379/'
|
54
|
+
capsule = RedisMessageCapsule.capsule(redisurl)
|
55
|
+
cat = capsule.channel('cat')
|
56
|
+
cat.register(function(err, message){ console.log(message) })
|
49
57
|
|
50
|
-
|
58
|
+
**in ruby**:
|
51
59
|
|
52
|
-
|
53
|
-
|
60
|
+
$ irb
|
61
|
+
require('redis_message_capsule')
|
62
|
+
redisurl = 'redis://127.0.0.1:6379/'
|
63
|
+
capsule = RedisMessageCapsule.capsule(redisurl)
|
64
|
+
cat = capsule.channel('cat')
|
65
|
+
cat.register { |msg| puts msg }
|
54
66
|
|
55
|
-
|
56
|
-
|
57
|
-
* in node: using obj.toJSON() or JSON.stringify(obj)
|
58
|
-
* in rails: using obj.to_json
|
59
|
-
* Demo for node <-> node: Open 2 terminal windows to send messages between 2 node apps (outlined below)
|
60
|
-
* Demo for ruby <-> ruby: Open 2 terminal windows to send messages between 2 ruby apps (outlined below)
|
61
|
-
* Demo for node <-> ruby: Open 4 terminal windows and do both of the demos above (which are outlined below)
|
62
|
-
* Think of each window that you open as a stand-alone app
|
67
|
+
### To Send More Messages From Window 1
|
68
|
+
**in node**:
|
63
69
|
|
64
|
-
|
65
|
-
|
70
|
+
cat.send(9)
|
71
|
+
talk = { say: 'roar', time: new Date() } // <- to create a hash in node
|
72
|
+
cat.send( talk )
|
66
73
|
|
67
|
-
|
68
|
-
RedisMessageCapsule = require('redis-message-capsule')
|
69
|
-
redisURL = process.env.REDIS_URL || process.env.REDISTOGO_URL || 'redis://127.0.0.1:6379/'
|
70
|
-
capsule = RedisMessageCapsule.materializeCapsule(redisURL)
|
71
|
-
cat = capsule.materializeChannel('cat')
|
72
|
-
cat.send('meow') // will show up in node window 2 (listening to cat)
|
73
|
-
cat.send('meow') // will show up in node window 2 (listening to cat)
|
74
|
-
|
75
|
-
In node window 2 - listen for cat messages, send messages from dog:
|
74
|
+
**in ruby**:
|
76
75
|
|
77
|
-
$ node
|
78
|
-
RedisMessageCapsule = require('redis-message-capsule')
|
79
|
-
redisURL = process.env.REDIS_URL || process.env.REDISTOGO_URL || 'redis://127.0.0.1:6379/'
|
80
|
-
capsule = RedisMessageCapsule.materializeCapsule(redisURL)
|
81
|
-
cat = capsule.materializeChannel('cat')
|
82
|
-
cat.register( function(err, message){ console.log(message) } )
|
83
|
-
// => meow
|
84
|
-
// => meow
|
85
|
-
// create a dog channel to send messages to ruby, and start barking
|
86
|
-
dog = capsule.materializeChannel('dog')
|
87
|
-
dog.send('woof') // will show up in ruby window 2 (listening to dog)
|
88
|
-
|
89
|
-
In node window 1 - send more cat messages:
|
90
|
-
|
91
|
-
talk = { say: 'roar', time: new Date() }
|
92
76
|
cat.send(9)
|
77
|
+
talk = { say: 'grrrrrr', time: Time.now } # <- to create a hash in ruby
|
93
78
|
cat.send( talk )
|
94
|
-
// Watch for real-time messages show up in node window 2:
|
95
|
-
// => 9
|
96
|
-
// => {"say"=>"roar", "time"=>"2012-11-21T208:08:08.808Z"}
|
97
|
-
|
98
|
-
### Demo for ruby <-> ruby
|
99
|
-
In ruby window 1 - send dog messages:
|
100
79
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
dog.send 'bark'
|
80
|
+
* Use the module to create as many capsules as you want (only need one capsule for each redisurl:redisdbnum)
|
81
|
+
* Use a capsule to create as many channels as you want (use 'ns:channelname' to add a namespace to your channel)
|
82
|
+
* Use a channel to send as many messages as you want (be sure the data you send can be serialized to json )
|
83
|
+
* Use a channel to register as many handlers as you want (all registered handlers on a channel will get messages)
|
84
|
+
* Send messages between any mixture of apps: [ node <-> node ] or [ ruby <-> ruby ] or [ node <-> ruby ]
|
107
85
|
|
108
|
-
|
86
|
+
## Advanced Usage
|
87
|
+
This module sends messages in a queue rather than a typical pub/sub model: messages are not broadcast to all apps.
|
109
88
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
dog = capsule.materialize_channel 'dog'
|
115
|
-
dog.register { |msg| puts "#{msg}!" * 2 }
|
116
|
-
# => woof!woof!
|
117
|
-
# => bark!bark!
|
118
|
-
# create a cat channel to send messages to node
|
119
|
-
cat = capsule.materialize_channel 'cat'
|
120
|
-
cat.send 'purrr' # will show up in node window 2 (listening to cat)
|
121
|
-
|
122
|
-
Back in ruby window 1 - send more dog messages:
|
123
|
-
|
124
|
-
talk = { say: 'grrrrrr', time: Time.now }
|
125
|
-
dog.send 2 # will show up in window 4 (listening to dog)
|
126
|
-
dog.send talk # will show up in window 4 (listening to dog)
|
127
|
-
# Watch for real-time messages to show up in ruby window 2:
|
128
|
-
# => 2
|
129
|
-
# => {"say"=>"grrrrrr", "time"=>"2012-11-21 08:08:08 -0800"}
|
130
|
-
# => purr
|
89
|
+
### Multiple Listeners on a Channel Will Round Robin
|
90
|
+
* If multiple apps are listening on the same channel, only one will actually receive the message and pass it on to its registered handlers for processing.
|
91
|
+
* Listeners run in their own thread (in ruby) or fiber(in node) and may block waiting for a message.
|
92
|
+
* When a message comes in, only the app that was waiting the longest will receive it.
|
131
93
|
|
132
94
|
## Environment
|
133
95
|
In order for 2 apps to send messages to each other, they must bind a capsule to the same redis DB and select the same db number.
|
@@ -140,7 +102,7 @@ The selected db can also be overriden when materializing a capsule (examples ar
|
|
140
102
|
|
141
103
|
If 2 apps are not sending messages to each other:
|
142
104
|
* check that they are both in the same environment (test, development, production)
|
143
|
-
* or be sure to override using same
|
105
|
+
* or be sure to override using same redisdb when materializing a capsule.
|
144
106
|
|
145
107
|
## Environment for node
|
146
108
|
In node use one of the following to set your environment
|
@@ -151,10 +113,10 @@ In node use one of the following to set your environment
|
|
151
113
|
|
152
114
|
Alternatively, you can specify exactly what you want when materializing a capsule:
|
153
115
|
|
154
|
-
RedisMessageCapsule = require('
|
155
|
-
|
156
|
-
|
157
|
-
capsule = RedisMessageCapsule.
|
116
|
+
RedisMessageCapsule = require('redis_message_capsule')
|
117
|
+
redisurl = process.env.REDIS_URL || process.env.REDISTOGO_URL || 'redis://127.0.0.1:6379/'
|
118
|
+
redisdb = 5
|
119
|
+
capsule = RedisMessageCapsule.capsule(redisurl, redisdb)
|
158
120
|
|
159
121
|
## Environment for ruby
|
160
122
|
In ruby use one of the following to set your environment
|
@@ -166,15 +128,15 @@ In ruby use one of the following to set your environment
|
|
166
128
|
Alternatively, you can specify exactly what you want when materializing a capsule:
|
167
129
|
|
168
130
|
require 'redis_message_capsule'
|
169
|
-
|
170
|
-
|
171
|
-
capsule = RedisMessageCapsule.
|
131
|
+
redisurl = ENV["REDIS_URL"] || ENV["REDISTOGO_URL"] || "redis://127.0.0.1:6379/"
|
132
|
+
redisdb = 5
|
133
|
+
capsule = RedisMessageCapsule.capsule redisurl, redisdb
|
172
134
|
|
173
135
|
## To build the gem locally:
|
174
136
|
git clone git@github.com:arbind/redis_message_capsule-gem.git
|
175
137
|
cd redis_message_capsule-gem
|
176
138
|
bundle
|
177
|
-
|
139
|
+
|
178
140
|
gem uninstall redis_message_capsule
|
179
141
|
gem build redis_message_capsule.gemspec
|
180
142
|
rake install
|
@@ -82,6 +82,9 @@ class RedisMessageCapsule::Capsule
|
|
82
82
|
def materialize_channel(channel_name)
|
83
83
|
channels[channel_name] ||= (Channel.new channel_name, redis_client, redis_uri, db_number)
|
84
84
|
end
|
85
|
+
alias_method :channel, :materialize_channel
|
86
|
+
alias_method :make_channel, :materialize_channel
|
87
|
+
alias_method :create_channel, :materialize_channel
|
85
88
|
|
86
89
|
end
|
87
90
|
|
@@ -168,7 +171,6 @@ class RedisMessageCapsule::Capsule::Channel::Listener
|
|
168
171
|
element = channel_element.last
|
169
172
|
payload = ( JSON.parse(element) rescue {'data' => 'error parsing json!'} )
|
170
173
|
message = payload['data']
|
171
|
-
puts @handlers.count
|
172
174
|
@handlers.each { |handler| handler.call(message) }
|
173
175
|
end
|
174
176
|
rescue Exception => e
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_message_capsule
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|