realtime 0.0.12 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ if (window.realtime.socketIo) {
2
+
3
+ window.realtime.enabled = true;
4
+
5
+ window.realtime.socketIo.on('connect', function() {
6
+ // Give a nice round-trip ACK to our realtime server that we connected.
7
+ window.realtime.socketIo.emit('realtime_user_id_connected', { userId: window.realtime.userId });
8
+ });
9
+
10
+ // Queue up all incoming realtime messages.
11
+ window.realtime.socketIo.on('realtime_msg', function(message) {
12
+ window.realtime.messageQueue.push(message);
13
+ });
14
+ }
@@ -0,0 +1,20 @@
1
+ if (window.realtime.enabled && window.realtime.eventBus){
2
+ // handle events in the queue with eventing
3
+ var realtimeMessageEventConsoleLogger = function(message) {
4
+ console.log(message);
5
+ };
6
+ window.realtime.eventBus.on('realtimeMessage', realtimeMessageEventConsoleLogger);
7
+
8
+ } else if (window.realtime.enabled) {
9
+ // handle events in the queue without eventing
10
+ messageQueueConsoleLogger = function() {
11
+ message = window.realtime.messageQueue.shift();
12
+ if (message) {
13
+ console.log(message);
14
+ }
15
+ };
16
+ setInterval(messageQueueConsoleLogger, 100);
17
+
18
+ } else {
19
+ console.log('Error: Realtime was not enabled.')
20
+ }
@@ -0,0 +1,13 @@
1
+ if (window.realtime.enabled){
2
+
3
+ window.realtime.eventBus = _.extend({}, Backbone.Events);
4
+
5
+ messageQueueRunner = function() {
6
+ message = window.realtime.messageQueue.shift();
7
+ if (message) {
8
+ window.realtime.eventBus.trigger('realtimeMessage', message);
9
+ }
10
+ };
11
+
12
+ setInterval(messageQueueRunner, 100);
13
+ }
@@ -0,0 +1,71 @@
1
+ <script type='text/javascript'>
2
+
3
+ window.realtime = {};
4
+ window.realtime.messageQueue = [];
5
+ window.realtime.enabled = false;
6
+
7
+ (function () {
8
+ // Load external scripts asynchronously
9
+ var loadScript = function (src, callback) {
10
+ var s, r, t;
11
+ r = false;
12
+ s = document.createElement('script');
13
+ s.type = 'text/javascript';
14
+ s.src = src;
15
+ s.onload = s.onreadystatechange = function () {
16
+ if (!r && (!this.readyState || this.readyState == 'complete'))
17
+ {
18
+ r = true;
19
+ callback();
20
+ }
21
+ };
22
+ t = document.getElementsByTagName('script')[0];
23
+ t.parentNode.insertBefore(s, t);
24
+ };
25
+
26
+ // Trigger a custom event that the user JS can listen for
27
+ // when everything finishes loading
28
+ var triggerEvent = function (name) {
29
+ var event;
30
+
31
+ if (document.createEvent) {
32
+ event = document.createEvent('HTMLEvents');
33
+ event.initEvent(name, true, true);
34
+ event.eventName = name;
35
+ document.dispatchEvent(event);
36
+ } else {
37
+ // For IE compatibility
38
+ event = document.createEventObject();
39
+ event.eventType = name;
40
+ event.eventName = name;
41
+ document.fireEvent('on' + event.eventType, event);
42
+ }
43
+ };
44
+
45
+ loadScript('<%= realtime_server_url %>/socket.io/socket.io.js', function () {
46
+ if (typeof io != 'undefined' && io != null) {
47
+ window.realtime.token = '<%= realtime_token %>';
48
+ window.realtime.userId = '<%= realtime_user_id %>';
49
+ window.realtime.socketIo = io.connect('<%= realtime_server_url %>/?_rtUserId=<%= @realtime_user_id %>&_rtToken=<%= @realtime_token %>');
50
+ }
51
+
52
+ <%= render "realtime/realtime_loader" %>
53
+
54
+ <% if message_handler %>
55
+ loadScript('//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js', function () {
56
+ loadScript('//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js', function () {
57
+ <%= render "realtime/realtime_message_handler" %>
58
+
59
+ triggerEvent('realtime:loaded')
60
+ })
61
+ })
62
+ <% else %>
63
+ <% if message_console_logger %>
64
+ <%= render "realtime/realtime_message_console_logger" %>
65
+ <% end %>
66
+
67
+ triggerEvent('realtime:loaded')
68
+ <% end %>
69
+ })
70
+ })();
71
+ </script>
@@ -1,25 +1,3 @@
1
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>
2
+ <%= render "realtime/realtime_message_console_logger" %>
3
+ </script>
@@ -1,22 +1,7 @@
1
-
2
1
  <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
3
2
 
4
3
  <script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
5
4
 
6
5
  <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>
6
+ <%= render "realtime/realtime_message_handler" %>
7
+ </script>
@@ -16,19 +16,22 @@
16
16
  window.realtime.socketIo = io.connect('<%= realtime_server_url %>/?_rtUserId=<%= @realtime_user_id %>&_rtToken=<%= @realtime_token %>');
17
17
  }
18
18
 
19
- if (window.realtime.socketIo) {
20
-
21
- window.realtime.enabled = true;
22
-
23
- window.realtime.socketIo.on('connect', function() {
24
- // Give a nice round-trip ACK to our realtime server that we connected.
25
- window.realtime.socketIo.emit('realtime_user_id_connected', { userId: window.realtime.userId });
26
- });
27
-
28
- // Queue up all incoming realtime messages.
29
- window.realtime.socketIo.on('realtime_msg', function(message) {
30
- window.realtime.messageQueue.push(message);
31
- });
32
- }
19
+ <%= render "realtime/realtime_loader" %>
20
+
21
+ </script>
22
+
23
+ <% if message_handler %>
24
+ <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
25
+
26
+ <script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
27
+
28
+ <script type='text/javascript'>
29
+ <%= render "realtime/realtime_message_handler" %>
30
+ </script>
31
+ <% end %>
33
32
 
34
- </script>
33
+ <% if message_console_logger %>
34
+ <script type='text/javascript'>
35
+ <%= render "realtime/realtime_message_console_logger" %>
36
+ </script>
37
+ <% end %>
@@ -1,6 +1,6 @@
1
-
2
- require "realtime/realtime_controller.rb"
3
- require "realtime/redis_wrapper.rb"
4
- require "realtime/zmq_wrapper.rb"
5
- require 'realtime/view_helpers.rb'
6
- require 'realtime/railtie.rb'
1
+
2
+ require "realtime/realtime_controller.rb"
3
+ require "realtime/redis_wrapper.rb"
4
+ require "realtime/zmq_wrapper.rb"
5
+ require 'realtime/view_helpers.rb'
6
+ require 'realtime/railtie.rb'
@@ -1,75 +1,75 @@
1
- module Realtime
2
- module Controller
3
- extend ActiveSupport::Concern
4
-
5
- module ClassMethods
6
- def realtime_controller(options = nil)
7
- before_filter :do_realtime_token
8
- before_filter :do_realtime_user_id
9
- before_filter :do_realtime_server_url
10
- if options.nil?
11
- after_filter :store_realtime_session_redis
12
- elsif options[:queue] == :redis
13
- after_filter :store_realtime_session_redis
14
- elsif options[:queue] == :zmq
15
- after_filter :store_realtime_session_zmq
16
- end
17
- end
18
- end
19
-
20
- def do_realtime_token
21
- @realtime_token = Digest::MD5.hexdigest("#{session[:session_id]}:#{realtime_user_id}")
22
- return @realtime_token
23
- end
24
-
25
- def do_realtime_user_id
26
- @realtime_user_id = realtime_user_id
27
- return @realtime_user_id
28
- end
29
-
30
- def do_realtime_server_url
31
- @realtime_server_url = realtime_server_url
32
- return @realtime_server_url
33
- end
34
-
35
- # create shared session tokens for redis/socketio realtime server running on node.js
36
- def store_realtime_session_zmq
37
- # store session data or any authentication data you want here, generate to JSON data
38
- session_data = {
39
- "user_id" => realtime_user_id,
40
- }
41
-
42
- # todo: merge additional session data passed in
43
- stored_session_data = JSON.generate(session_data)
44
-
45
- ZmqWrapper.store_session(
46
- realtime_user_id,
47
- @realtime_token,
48
- stored_session_data,
49
- 86400
50
- )
51
- end
52
-
53
- # create shared session tokens for redis/socketio realtime server running on node.js
54
- def store_realtime_session_redis
55
- # store session data or any authentication data you want here, generate to JSON data
56
- session_data = {
57
- "user_id" => realtime_user_id,
58
- }
59
-
60
- # todo: merge additional session data passed in
61
-
62
- stored_session_data = JSON.generate(session_data)
63
-
64
- RedisWrapper.redis.hset(
65
- "rtSession-" + realtime_user_id.to_s,
66
- @realtime_token,
67
- stored_session_data,
68
- )
69
-
70
- # expire this realtime session after one day.
71
- RedisWrapper.redis.expire("rtSession-" + realtime_user_id.to_s, 86400)
72
- end
73
-
74
- end
1
+ module Realtime
2
+ module Controller
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def realtime_controller(options = nil)
7
+ before_filter :do_realtime_token
8
+ before_filter :do_realtime_user_id
9
+ before_filter :do_realtime_server_url
10
+ if options.nil?
11
+ after_filter :store_realtime_session_redis
12
+ elsif options[:queue] == :redis
13
+ after_filter :store_realtime_session_redis
14
+ elsif options[:queue] == :zmq
15
+ after_filter :store_realtime_session_zmq
16
+ end
17
+ end
18
+ end
19
+
20
+ def do_realtime_token
21
+ @realtime_token = Digest::MD5.hexdigest("#{session[:session_id]}:#{realtime_user_id}")
22
+ return @realtime_token
23
+ end
24
+
25
+ def do_realtime_user_id
26
+ @realtime_user_id = realtime_user_id
27
+ return @realtime_user_id
28
+ end
29
+
30
+ def do_realtime_server_url
31
+ @realtime_server_url = realtime_server_url
32
+ return @realtime_server_url
33
+ end
34
+
35
+ # create shared session tokens for redis/socketio realtime server running on node.js
36
+ def store_realtime_session_zmq
37
+ # store session data or any authentication data you want here, generate to JSON data
38
+ session_data = {
39
+ "user_id" => realtime_user_id,
40
+ }
41
+
42
+ # todo: merge additional session data passed in
43
+ stored_session_data = JSON.generate(session_data)
44
+
45
+ ZmqWrapper.store_session(
46
+ realtime_user_id,
47
+ @realtime_token,
48
+ stored_session_data,
49
+ 86400
50
+ )
51
+ end
52
+
53
+ # create shared session tokens for redis/socketio realtime server running on node.js
54
+ def store_realtime_session_redis
55
+ # store session data or any authentication data you want here, generate to JSON data
56
+ session_data = {
57
+ "user_id" => realtime_user_id,
58
+ }
59
+
60
+ # todo: merge additional session data passed in
61
+
62
+ stored_session_data = JSON.generate(session_data)
63
+
64
+ RedisWrapper.redis.hset(
65
+ "rtSession-" + realtime_user_id.to_s,
66
+ @realtime_token,
67
+ stored_session_data,
68
+ )
69
+
70
+ # expire this realtime session after one day.
71
+ RedisWrapper.redis.expire("rtSession-" + realtime_user_id.to_s, 86400)
72
+ end
73
+
74
+ end
75
75
  end
@@ -1,3 +1,3 @@
1
- module Realtime
2
- VERSION = "0.0.12"
3
- end
1
+ module Realtime
2
+ VERSION = "0.1.0"
3
+ end
@@ -1,29 +1,33 @@
1
1
  module Realtime
2
- module ViewHelpers
2
+ module ViewHelpers
3
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
4
+ def realtime_support(args = {})
5
+ async = args[:async] ? 'async_' : ''
6
+ return render(template: "realtime/#{async}realtime_support",
7
+ layout: nil,
8
+ locals:
9
+ { realtime_token: @realtime_token,
10
+ realtime_domain: @realtime_domain,
11
+ realtime_server_url: @realtime_server_url,
12
+ realtime_user_id: @realtime_user_id,
13
+ message_handler: args[:message_handler],
14
+ message_console_logger: args[:message_console_logger] }).to_s
15
+ end
14
16
 
15
- def realtime_message_handler
16
- return render(:template =>"realtime/realtime_message_handler",
17
- :layout => nil,
18
- :locals => {}).to_s
19
-
20
- end
17
+ def realtime_message_handler
18
+ ActiveSupport::Deprecation.warn("'realtime_message_handler' is deprecated, "\
19
+ "please refer to the documentation for details.")
20
+ return render(template: "realtime/realtime_message_handler",
21
+ layout: nil,
22
+ locals: {}).to_s
23
+ end
21
24
 
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
25
+ def realtime_message_console_logger
26
+ ActiveSupport::Deprecation.warn("'realtime_message_console_logger' is deprecated, "\
27
+ "please refer to the documentation for details.")
28
+ return render(template: "realtime/realtime_message_console_logger",
29
+ layout: nil,
30
+ locals: {}).to_s
31
+ end
32
+ end
33
+ end
@@ -1,34 +1,34 @@
1
- class ZmqWrapper
2
-
3
- if defined?(ZMQ) && defined?(EmZeromq)
4
- require 'em-zeromq'
5
-
6
- def self.publish(the_message)
7
- EM.run {
8
- zmq = EM::ZeroMQ::Context.new(1)
9
- pusher = zmq.socket(ZMQ::PUSH)
10
- pusher.connect($zmq_server)
11
- message = "realtime_msg:" + the_message.to_json
12
- puts "Pushing realtime message: " + message
13
- pusher.send_msg(message)
14
- EM.stop
15
- }
16
- end
17
-
18
- def self.store_session(user_id, session_id, session_data, expiration)
19
- EM.run {
20
- zmq = EM::ZeroMQ::Context.new(1)
21
- pusher = zmq.socket(ZMQ::PUSH)
22
- pusher.connect($zmq_server)
23
- the_message = {user_id: user_id,
24
- session_id: session_id,
25
- session_data: session_data,
26
- expiration: expiration}
27
- message = "rtSession:" + the_message.to_json
28
- puts "Pushing session message: " + message
29
- pusher.send_msg(message)
30
- EM.stop
31
- }
32
- end
33
- end
34
- end
1
+ class ZmqWrapper
2
+
3
+ if defined?(ZMQ) && defined?(EmZeromq)
4
+ require 'em-zeromq'
5
+
6
+ def self.publish(the_message)
7
+ EM.run {
8
+ zmq = EM::ZeroMQ::Context.new(1)
9
+ pusher = zmq.socket(ZMQ::PUSH)
10
+ pusher.connect($zmq_server)
11
+ message = "realtime_msg:" + the_message.to_json
12
+ puts "Pushing realtime message: " + message
13
+ pusher.send_msg(message)
14
+ EM.stop
15
+ }
16
+ end
17
+
18
+ def self.store_session(user_id, session_id, session_data, expiration)
19
+ EM.run {
20
+ zmq = EM::ZeroMQ::Context.new(1)
21
+ pusher = zmq.socket(ZMQ::PUSH)
22
+ pusher.connect($zmq_server)
23
+ the_message = {user_id: user_id,
24
+ session_id: session_id,
25
+ session_data: session_data,
26
+ expiration: expiration}
27
+ message = "rtSession:" + the_message.to_json
28
+ puts "Pushing session message: " + message
29
+ pusher.send_msg(message)
30
+ EM.stop
31
+ }
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,27 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: realtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.1.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Mike Atlas
9
+ - Ahmad Abdel-Yaman (@ayaman)
10
+ - Nick Prokesch (@prokizzle)
8
11
  autorequire:
9
12
  bindir: bin
10
13
  cert_chain: []
11
- date: 2014-10-11 00:00:00.000000000 Z
14
+ date: 2016-01-24 00:00:00.000000000 Z
12
15
  dependencies:
13
16
  - !ruby/object:Gem::Dependency
14
17
  name: rails
15
18
  requirement: !ruby/object:Gem::Requirement
19
+ none: false
16
20
  requirements:
17
- - - '>='
21
+ - - ! '>='
18
22
  - !ruby/object:Gem::Version
19
23
  version: '4.0'
20
24
  type: :runtime
21
25
  prerelease: false
22
26
  version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
23
28
  requirements:
24
- - - '>='
29
+ - - ! '>='
25
30
  - !ruby/object:Gem::Version
26
31
  version: '4.0'
27
32
  description: Provides a simple Realtime framework for Rails applications.
@@ -32,38 +37,43 @@ extensions: []
32
37
  extra_rdoc_files: []
33
38
  files:
34
39
  - app/views/realtime/realtime_message_console_logger.html.erb
40
+ - app/views/realtime/_realtime_message_handler.html.erb
35
41
  - app/views/realtime/realtime_message_handler.html.erb
42
+ - app/views/realtime/async_realtime_support.html.erb
36
43
  - app/views/realtime/realtime_support.html.erb
44
+ - app/views/realtime/_realtime_message_console_logger.html.erb
45
+ - app/views/realtime/_realtime_loader.html.erb
37
46
  - lib/realtime/railtie.rb
38
47
  - lib/realtime/realtime_controller.rb
48
+ - lib/realtime/zmq_wrapper.rb
39
49
  - lib/realtime/redis_wrapper.rb
40
50
  - lib/realtime/version.rb
41
51
  - lib/realtime/view_helpers.rb
42
- - lib/realtime/zmq_wrapper.rb
43
52
  - lib/realtime.rb
44
53
  - MIT-LICENSE
45
54
  homepage: http://mikeatlas.github.io/realtime-rails/
46
55
  licenses:
47
56
  - MIT
48
- metadata: {}
49
57
  post_install_message:
50
58
  rdoc_options: []
51
59
  require_paths:
52
60
  - lib
53
61
  required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
54
63
  requirements:
55
- - - '>='
64
+ - - ! '>='
56
65
  - !ruby/object:Gem::Version
57
66
  version: '0'
58
67
  required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
59
69
  requirements:
60
- - - '>='
70
+ - - ! '>='
61
71
  - !ruby/object:Gem::Version
62
72
  version: '0'
63
73
  requirements: []
64
74
  rubyforge_project:
65
- rubygems_version: 2.0.3
75
+ rubygems_version: 1.8.23
66
76
  signing_key:
67
- specification_version: 4
77
+ specification_version: 3
68
78
  summary: Realtime support for Rails applications.
69
79
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: c3ec19eba97229354e064d27bfc0b92b0911a3d8
4
- data.tar.gz: 4ea1fd69755b1b971838b592ccb5eac0caa06e3d
5
- SHA512:
6
- metadata.gz: df907e7537971450e92f4ca3960a1cb53b5bae9deb9d4db38732709487021c10772f6bf7f560b444812a8f3c402768b4aa29dabdc11dcdbdf30212328fa656c7
7
- data.tar.gz: ea678d1709d3eff430de67e890528fbe5ef43f3b994af83ac0f67366099ca83ad6bf5f99f64d5800a4ae448da0722d65c87cc2700ce99981519f9b7693f0190e