realtime 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +1 -0
- data/app/views/realtime/realtime_message_console_logger.html.erb +25 -0
- data/app/views/realtime/realtime_message_handler.html.erb +22 -0
- data/app/views/realtime/realtime_support.html.erb +36 -0
- data/lib/realtime.rb +5 -0
- data/lib/realtime/railtie.rb +22 -0
- data/lib/realtime/realtime_controller.rb +56 -0
- data/lib/realtime/redis_wrapper.rb +15 -0
- data/lib/realtime/version.rb +3 -0
- data/lib/realtime/view_helpers.rb +29 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9761455dd3d83567ee9fb1522ac53dbd6d6434cd
|
4
|
+
data.tar.gz: efe0161d15e2ea8662bedddb1121e55f4da43fbe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f3ec2ec5304734b1810efa90075b5debcaaa9c9c1a5c2c73281287303a9fdc672e743715709d2b7959a9bebe95d14ced65db54260d53742eaea52546f8df47a0
|
7
|
+
data.tar.gz: 05f0bf622d25c0289ad1d82c2d9672205e5388f4b8639291992739ad0818881e740cedc8be90926ff4458aef3cfb694ef6f38cb53cb6b3662c37759981cbb660
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Mike Atlas
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
= Realtime Support for Rails
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<script type='text/javascript'>
|
2
|
+
|
3
|
+
|
4
|
+
if (window.realtime.enabled && window.realtime.eventBus){
|
5
|
+
// handle events in the queue with eventing
|
6
|
+
var realtimeMessageEventConsoleLogger = function(message) {
|
7
|
+
console.log(message);
|
8
|
+
};
|
9
|
+
window.realtime.eventBus.on('realtimeMessage', realtimeMessageEventConsoleLogger);
|
10
|
+
|
11
|
+
} else if (window.realtime.enabled) {
|
12
|
+
// handle events in the queue without eventing
|
13
|
+
messageQueueConsoleLogger = function() {
|
14
|
+
message = window.realtime.messageQueue.shift();
|
15
|
+
if (message) {
|
16
|
+
console.log(message);
|
17
|
+
}
|
18
|
+
};
|
19
|
+
setInterval(messageQueueConsoleLogger, 100);
|
20
|
+
|
21
|
+
} else {
|
22
|
+
console.log('Error: Realtime was not enabled.')
|
23
|
+
}
|
24
|
+
|
25
|
+
</script>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
|
3
|
+
|
4
|
+
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
|
5
|
+
|
6
|
+
<script type='text/javascript'>
|
7
|
+
|
8
|
+
if (window.realtime.enabled){
|
9
|
+
|
10
|
+
window.realtime.eventBus = _.extend({}, Backbone.Events);
|
11
|
+
|
12
|
+
messageQueueRunner = function() {
|
13
|
+
message = window.realtime.messageQueue.shift();
|
14
|
+
if (message) {
|
15
|
+
window.realtime.eventBus.trigger('realtimeMessage', message);
|
16
|
+
}
|
17
|
+
};
|
18
|
+
|
19
|
+
setInterval(messageQueueRunner, 100);
|
20
|
+
}
|
21
|
+
|
22
|
+
</script>
|
@@ -0,0 +1,36 @@
|
|
1
|
+
<script type='text/javascript'>
|
2
|
+
|
3
|
+
window.realtime = {};
|
4
|
+
window.realtime.messageQueue = [];
|
5
|
+
window.realtime.enabled = false;
|
6
|
+
|
7
|
+
</script>
|
8
|
+
|
9
|
+
<script src="<%= realtime_server_url %>/socket.io/socket.io.js"></script>
|
10
|
+
|
11
|
+
<script type='text/javascript'>
|
12
|
+
|
13
|
+
if (typeof io != 'undefined' && io != null) {
|
14
|
+
window.realtime.token = '<%= realtime_token %>';
|
15
|
+
window.realtime.userId = '<%= realtime_user_id %>';
|
16
|
+
document.cookie = "_rtUserId=" + '<%= realtime_user_id %>' + ";domain=." + '<%= realtime_domain %>' + ";path=/"
|
17
|
+
document.cookie = "_rtToken=" + '<%= realtime_token %>' + ";domain=." + '<%= realtime_domain %>' + ";path=/"
|
18
|
+
window.realtime.socketIo = io.connect('<%= realtime_server_url %>');
|
19
|
+
}
|
20
|
+
|
21
|
+
if (window.realtime.socketIo) {
|
22
|
+
|
23
|
+
window.realtime.enabled = true;
|
24
|
+
|
25
|
+
window.realtime.socketIo.on('connect', function() {
|
26
|
+
// Give a nice round-trip ACK to our realtime server that we connected.
|
27
|
+
window.realtime.socketIo.emit('realtime_user_id_connected', { userId: window.realtime.userId });
|
28
|
+
});
|
29
|
+
|
30
|
+
// Queue up all incoming realtime messages.
|
31
|
+
window.realtime.socketIo.on('realtime_msg', function(message) {
|
32
|
+
window.realtime.messageQueue.push(message);
|
33
|
+
});
|
34
|
+
}
|
35
|
+
|
36
|
+
</script>
|
data/lib/realtime.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Realtime
|
2
|
+
|
3
|
+
class Railtie < Rails::Railtie
|
4
|
+
|
5
|
+
initializer "realtime.action_controller" do
|
6
|
+
ActiveSupport.on_load(:action_controller) do
|
7
|
+
ActionController::Base.send :include, Realtime::Controller
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
initializer "realtime.view_helpers" do
|
12
|
+
ActionView::Base.send :include, ViewHelpers
|
13
|
+
|
14
|
+
# kinda blah way to force rails to load our helper views path
|
15
|
+
curr_path = File.expand_path(File.dirname(__FILE__))
|
16
|
+
view_path = "#{curr_path}/../../app/views"
|
17
|
+
ActionController::Base.append_view_path(view_path)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Realtime
|
2
|
+
module Controller
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def realtime_controller
|
7
|
+
before_filter :do_realtime_token
|
8
|
+
before_filter :do_realtime_domain
|
9
|
+
before_filter :do_realtime_user_id
|
10
|
+
before_filter :do_realtime_server_url
|
11
|
+
after_filter :store_realtime_session
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def do_realtime_token
|
16
|
+
cookies["_rtToken"] = Digest::MD5.hexdigest("#{session[:session_id]}:#{realtime_user_id}")
|
17
|
+
@realtime_token = cookies["_rtToken"]
|
18
|
+
return @realtime_token
|
19
|
+
end
|
20
|
+
|
21
|
+
def do_realtime_domain
|
22
|
+
@realtime_domain = realtime_domain
|
23
|
+
return @realtime_domain
|
24
|
+
end
|
25
|
+
|
26
|
+
def do_realtime_user_id
|
27
|
+
@realtime_user_id = realtime_user_id
|
28
|
+
return @realtime_user_id
|
29
|
+
end
|
30
|
+
|
31
|
+
def do_realtime_server_url
|
32
|
+
@realtime_server_url = realtime_server_url
|
33
|
+
return @realtime_server_url
|
34
|
+
end
|
35
|
+
|
36
|
+
# create shared session tokens for redis/socketio realtime server running on node.js
|
37
|
+
def store_realtime_session
|
38
|
+
# store session data or any authentication data you want here, generate to JSON data
|
39
|
+
session_data = {
|
40
|
+
"user_id" => realtime_user_id,
|
41
|
+
}
|
42
|
+
|
43
|
+
stored_session_data = JSON.generate(session_data)
|
44
|
+
|
45
|
+
RedisWrapper.redis.hset(
|
46
|
+
"rtSession-" + realtime_user_id.to_s,
|
47
|
+
@realtime_token,
|
48
|
+
stored_session_data,
|
49
|
+
)
|
50
|
+
|
51
|
+
# expire this realtime session after one day.
|
52
|
+
RedisWrapper.redis.expire("rtSession-" + realtime_user_id.to_s, 86400)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class RedisWrapper
|
2
|
+
|
3
|
+
def self.publish(message)
|
4
|
+
redis.publish 'realtime_msg', message.to_json
|
5
|
+
end
|
6
|
+
|
7
|
+
def redis
|
8
|
+
return $redis # global set in redis.rb initializer
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.redis
|
12
|
+
return $redis # global set in redis.rb initializer
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Realtime
|
2
|
+
module ViewHelpers
|
3
|
+
|
4
|
+
def realtime_support
|
5
|
+
return render(:template =>"realtime/realtime_support",
|
6
|
+
:layout => nil,
|
7
|
+
:locals =>
|
8
|
+
{ :realtime_token => @realtime_token,
|
9
|
+
:realtime_domain => @realtime_domain,
|
10
|
+
:realtime_server_url => @realtime_server_url,
|
11
|
+
:realtime_user_id => @realtime_user_id }).to_s
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
def realtime_message_handler
|
16
|
+
return render(:template =>"realtime/realtime_message_handler",
|
17
|
+
:layout => nil,
|
18
|
+
:locals => {}).to_s
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def realtime_message_console_logger
|
23
|
+
return render(:template =>"realtime/realtime_message_console_logger",
|
24
|
+
:layout => nil,
|
25
|
+
:locals => {}).to_s
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: realtime
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Atlas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: redis
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Provides a simple Realtime framework for Rails applications.
|
42
|
+
email:
|
43
|
+
- mike.atlas@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- app/views/realtime/realtime_message_console_logger.html.erb
|
49
|
+
- app/views/realtime/realtime_message_handler.html.erb
|
50
|
+
- app/views/realtime/realtime_support.html.erb
|
51
|
+
- lib/realtime/railtie.rb
|
52
|
+
- lib/realtime/realtime_controller.rb
|
53
|
+
- lib/realtime/redis_wrapper.rb
|
54
|
+
- lib/realtime/version.rb
|
55
|
+
- lib/realtime/view_helpers.rb
|
56
|
+
- lib/realtime.rb
|
57
|
+
- MIT-LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
homepage: https://github.com/mikeatlas/realtime-rails
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.3
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Realtime support for Rails applications.
|
83
|
+
test_files: []
|