centrifuge 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c72f7e53d98f1c39fa274dad8db771151df6275
4
- data.tar.gz: c26967230df365720bcab949250650cd1f368df5
3
+ metadata.gz: 3d24a6b2607907f90eee9fc465ecf5fc59e231c3
4
+ data.tar.gz: 49908552ac245cbbb3b3d5c773e5b5df35b89e3f
5
5
  SHA512:
6
- metadata.gz: 753fe93461f18f82a268cb0e648e30bb0cce09f88aace6d5b15a044eef1e291d1b1105f57adaccc462d48e4647a6a932aa436717a450e2e8c14fb3e23c2dc8c6
7
- data.tar.gz: 9af78670d1570d93fabc973ff7e46c3e254560e77864d253dc9bfb9a2128c0cf30d1cca2261aa0b46b4bcc95f2bba4da658bae438ea4825adad333e9e9c3e27f
6
+ metadata.gz: f5943232807ff2c1dd6329dacf65ae812a110096005c1118713e3138fcd6856253750a73cab5677262fe94cda537ae517d7bc5d8cca12c4e171b737b0206ef94
7
+ data.tar.gz: 3d4787d2ddff2d82c787dd539648aad42922f27338646d935175ca5e6c59715e27f38a75b3845480dd3befeaa124378f51962db5508c61ba63cb1dff49468290
data/CHANGELOG.md ADDED
File without changes
data/README.md CHANGED
@@ -8,8 +8,8 @@ Ruby gem for [Centrifuge](https://github.com/centrifugal/centrifuge) real-time m
8
8
 
9
9
  Add this line to your application's Gemfile:
10
10
 
11
- ```
12
- gem 'centrifuge'
11
+ ```ruby
12
+ gem 'centrifuge'
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -20,39 +20,51 @@ Or install it yourself as:
20
20
 
21
21
  $ gem install centrifuge
22
22
 
23
- Compatible version for Centrifuge version 0.8.0 and above are 0.1.0+. Please use 0.0.x versions for older Centrifuge
23
+ ## Compatibility
24
+
25
+ Centrifuge-ruby 1.0.0+ supports only Centrifuge version 1.0.0+ with single project only.
26
+ If you want to use multiproject Centrifuge, please use centrifuge-ruby 0.1.0
24
27
 
25
28
  ## Usage
26
29
 
27
30
  `Centrifuge::Client` - is main usable class. Start with:
28
31
 
29
- client = Centrifute::Client.new(scheme: :http, host: :localhost, port: 80, project_key: 'abc', secret: 'cde')
32
+ ```ruby
33
+ client = Centrifuge::Client.new(scheme: :http, host: :localhost, port: 80, secret: 'cde')
34
+ ```
30
35
 
31
- If you are planning to use only one project, its convenient to set all data and use class methods:
36
+ Or just:
32
37
 
38
+ ```ruby
33
39
  Centrifuge.scheme = :http
34
40
  Centrifuge.host = 'localhost'
35
41
  Centrifuge.port = 8000
36
- Centrifuge.project_key = 'abc'
37
42
  Centrifuge.secret = 'def'
43
+ ```
38
44
 
39
- There are five methds available:
45
+ There are six methods available:
40
46
 
41
47
  ### Publish
42
48
 
43
49
  Sends message to all connected users:
44
50
 
51
+ ```ruby
45
52
  client.publish('teshchannel', { data: :foo })
53
+ ```
46
54
 
47
55
  You can also use class methods if you set all necessary config data:
48
56
 
57
+ ```ruby
49
58
  Centrifuge.publish('testchannel', { data: :foo })
59
+ ```
50
60
 
51
61
  ### Unsubscribe
52
62
 
53
63
  Unsubscribes user from channel:
54
64
 
65
+ ```ruby
55
66
  client.unsubscribe('testchannel', 'user#23')
67
+ ```
56
68
 
57
69
  `user#23` - is string identificator of user.
58
70
 
@@ -60,29 +72,149 @@ Unsubscribes user from channel:
60
72
 
61
73
  Disconnects user from Centrifuge:
62
74
 
75
+ ```ruby
63
76
  client.disconnect('user#23')
64
-
77
+ ```
65
78
  ### Presence
66
79
 
67
80
  Gets presence info of the channel:
68
81
 
82
+ ```ruby
69
83
  client.presence('testchannel')
70
-
84
+ ```
71
85
  ### History
72
86
 
73
87
  Gets message history of the channel:
74
88
 
89
+ ```ruby
75
90
  client.history('test_channel')
91
+ ```
92
+
93
+ ### Channels
94
+
95
+ Get active channels (with one or more subscribers):
96
+
97
+ ```ruby
98
+ client.channels()
99
+ ```
76
100
 
77
101
  ### JS Client token generation
78
102
 
79
103
  Generates token for JS client:
80
104
 
81
- client.token_for('testuser', '123123')
105
+ ```ruby
106
+ client.token_for('testuser', '1443422448')
107
+ ```
108
+ Where `1443422448` is UNIX timestamp seconds. You can also add user info as valid json string as third parameter:
109
+
110
+ ```ruby
111
+ client.token_for('testuser', '1443422448', "{}")
112
+ ```
113
+ ### Channel Sign token generation
114
+
115
+ Generates Sign token for a Channel:
116
+
117
+ ```ruby
118
+ client.generate_channel_sign(params[:client], params[:channels], "{}")
119
+ ```
120
+
121
+ Where ```params[:client]``` is client passed during authentication and ```params[:channels]``` can be an array of channels or a single channel. You can read more here about batching channels here [JS Documentation](https://fzambia.gitbooks.io/centrifugal/content/client/api.html).
122
+
123
+ You can also add user info as valid json string as third parameter:
124
+
125
+ ```ruby
126
+ client.generate_channel_sign(params[:client], params[:channels], "{"name": "John"}")
127
+ ```
128
+
129
+ On server side using rails, you can authenticate private channels like so:
130
+
131
+ ```ruby
132
+
133
+ #routes.rb or any router lib
134
+ post 'sockets/auth'
135
+
136
+ # Auth method to authenticate private channels
137
+ #sockets_controller.rb or anywhere in your app
138
+
139
+ # client = Instance of Centrifuge initialized // check Usage section
140
+ # params[:channels] = single/array of channels
141
+ def auth
142
+ if user_signed_in? # Or use a before_filter
143
+ data = {}
144
+ sign = client.generate_channel_sign(
145
+ params[:client], params[:channels], "{}"
146
+ )
147
+ data[channel] = {
148
+ "sign": sign,
149
+ "info": "{}"
150
+ }
151
+ render :json => data
152
+ else
153
+ render :text => "Not authorized", :status => '403'
154
+ end
155
+ end
156
+ ```
157
+ On client side initialize Centrifuge like so:
158
+
159
+ ``` javascript
160
+
161
+ // client_info = valid JSON string of user_info
162
+ // client_token = client.token_for (described above)
163
+
164
+ var centrifuge = new Centrifuge({
165
+ url: "http://localhost:8000/connection",
166
+ user: window.currentUser.id,
167
+ timestamp: window.currentUser.current_timestamp,
168
+ debug: true,
169
+ info: JSON.stringify(window.client_info),
170
+ token: window.client_token,
171
+ refreshEndpoint: "/sockets/refresh",
172
+ authEndpoint: "/sockets/auth",
173
+ authHeaders: {
174
+ 'X-Transaction': 'WebSocket Auth',
175
+ 'X-CSRF-Token': window.currentUser.form_authenticity_token
176
+ },
177
+ refreshHeaders: {
178
+ 'X-Transaction': 'WebSocket Auth',
179
+ 'X-CSRF-Token': window.currentUser.form_authenticity_token
180
+ }
181
+ });
182
+
183
+ centrifuge.connect();
184
+
185
+ // If you using jbuiler use raw render(template_name) and use a global window object to load the user information.
186
+ ```
187
+
188
+ If you want to batch sign channels request to avoid multiple HTTP requests, you can use centrifuge JS client to batch channels ```centrifuge.startBatching(); // your code // centrifuge.stopBatching();``` in one request and on the server iterate through channels and subscribe.
189
+
190
+ Read more: [JS client Documentation](https://fzambia.gitbooks.io/centrifugal/content/client/api.html)
191
+
192
+ ### API request sign
193
+
194
+ When you use Centrifugo server API you should sign each request to successfully authorize with the server.
195
+
196
+ ```ruby
197
+
198
+ # For example this is how you would encode a Publish command
199
+ commands = {
200
+ "method": "publish",
201
+ "params": {
202
+ "channel": "Test",
203
+ "data": { "name": "John Doe" }
204
+ }
205
+ }
206
+
207
+ encoded_data = MultiJson.dump(commands) # Or use to_json
208
+ sign = client.sign(encoded_data) #Sign the data
209
+
210
+ # Using HTTP party GEM interact with Server API
211
+ r = HTTParty.post(client.url.to_s, query: {"sign": sign, "data": encoded_data})
212
+ r.parsed_response
213
+
214
+ ```
82
215
 
83
- Where `123123` is UNIX timestamp. You can also add user info as valid json string as third parameter:
216
+ ```encoded_data``` is a JSON string with your API command/commands. See all available commands in [Server API](https://fzambia.gitbooks.io/centrifugal/content/server/api.html) chapter.
84
217
 
85
- client.token_for('testuser', '123123', "{}")
86
218
 
87
219
  ## Rails assets
88
220
 
@@ -9,7 +9,7 @@ module Centrifuge
9
9
  host: 'localhost',
10
10
  port: 8000,
11
11
  }
12
- attr_accessor :scheme, :host, :port, :project_key, :secret
12
+ attr_accessor :scheme, :host, :port, :secret
13
13
  attr_writer :connect_timeout, :send_timeout, :receive_timeout,
14
14
  :keep_alive_timeout
15
15
 
@@ -17,8 +17,8 @@ module Centrifuge
17
17
 
18
18
  def initialize(options = {})
19
19
  options = DEFAULT_OPTIONS.merge(options)
20
- @scheme, @host, @port, @project_key, @secret = options.values_at(
21
- :scheme, :host, :port, :project_key, :secret
20
+ @scheme, @host, @port, @secret = options.values_at(
21
+ :scheme, :host, :port, :secret
22
22
  )
23
23
  set_default_timeouts
24
24
  end
@@ -35,7 +35,7 @@ module Centrifuge
35
35
  scheme: scheme.to_s,
36
36
  host: host,
37
37
  port: port,
38
- path: "/api/#{project_key}#{path}"
38
+ path: "/api/#{path}"
39
39
  })
40
40
  end
41
41
 
@@ -59,13 +59,25 @@ module Centrifuge
59
59
  Centrifuge::Builder.new('history', { channel: channel }, self).process
60
60
  end
61
61
 
62
+ def channels()
63
+ Centrifuge::Builder.new('channels', {}, self).process
64
+ end
65
+
66
+ def stats()
67
+ Centrifuge::Builder.new('stats', {}, self).process
68
+ end
69
+
62
70
  def token_for(user, timestamp, user_info = "")
63
71
  sign("#{user}#{timestamp}#{user_info}")
64
72
  end
65
73
 
74
+ def generate_channel_sign(client, channel, user_info="")
75
+ data = "#{client}#{channel}#{user_info}"
76
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, data)
77
+ end
78
+
66
79
  def sign(body)
67
- dig = OpenSSL::Digest.new('sha256')
68
- OpenSSL::HMAC.hexdigest(dig, secret, "#{project_key}#{body}")
80
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, body)
69
81
  end
70
82
 
71
83
  def client
@@ -78,5 +90,6 @@ module Centrifuge
78
90
  end
79
91
  end
80
92
  end
93
+
81
94
  end
82
95
  end
@@ -1,3 +1,3 @@
1
1
  module Centrifuge
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/centrifuge.rb CHANGED
@@ -16,8 +16,8 @@ module Centrifuge
16
16
  class << self
17
17
  extend Forwardable
18
18
 
19
- def_delegators :default_client, :scheme, :host, :port, :project_key, :secret
20
- def_delegators :default_client, :scheme=, :host=, :port=, :project_key=, :secret=
19
+ def_delegators :default_client, :scheme, :host, :port, :secret
20
+ def_delegators :default_client, :scheme=, :host=, :port=, :secret=
21
21
 
22
22
  # def_delegators :default_client, :authentication_token, :url
23
23
  # def_delegators :default_client, :encrypted=, :url=
data/spec/client_spec.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Centrifuge::Client do
4
- let(:options) { { scheme: :https, host: 'centrifugo.herokuapp.com', port: 443, project_key: 'development', secret: 'secret' } }
4
+ let(:options) { { scheme: :https, host: 'centrifugo-dev.herokuapp.com', port: 443, secret: 'secret' } }
5
5
  let(:client) { Centrifuge::Client.new(options) }
6
6
  let(:data) { { action: :test } }
7
7
 
8
8
  it 'generates url' do
9
- expect(client.url.to_s).to eq "https://centrifugo.herokuapp.com:443/api/#{client.project_key}"
9
+ expect(client.url.to_s).to eq "https://centrifugo-dev.herokuapp.com:443/api/"
10
10
  end
11
11
 
12
12
  it 'publishes data' do