pusher 1.3.1 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/CHANGELOG.md +96 -66
- data/README.md +107 -121
- data/lib/pusher/channel.rb +14 -2
- data/lib/pusher/client.rb +107 -66
- data/lib/pusher/request.rb +2 -0
- data/lib/pusher/version.rb +1 -1
- data/lib/pusher.rb +5 -6
- metadata +61 -65
- data/.document +0 -5
- data/.gemtest +0 -0
- data/.gitignore +0 -24
- data/.travis.yml +0 -15
- data/Gemfile +0 -2
- data/Rakefile +0 -11
- data/examples/async_message.rb +0 -28
- data/lib/pusher/native_notification/client.rb +0 -69
- data/pusher.gemspec +0 -33
- data/spec/channel_spec.rb +0 -168
- data/spec/client_spec.rb +0 -615
- data/spec/spec_helper.rb +0 -26
- data/spec/web_hook_spec.rb +0 -117
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
Mjk3MmUwODFhNDcxY2U1ZjVhZTlkMGUzYmZlMGE3NWY0ZGNkZGE3Mg==
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 71dc5eb4f9d389dce37f6ec8c1f78390402b31bd94edcb6f22094c70edeb2d72
|
4
|
+
data.tar.gz: 3b2d0db0ffe052cbd8c71b252ced1d5910ece64a08100a92c0f17b5ac9b6fa8a
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
ZTBiNzUyODYzYWE4NjQxNDE5OWQ2ZjIxMTMwYzUwYTdhNjkyNTY0OWZmMDQ2
|
11
|
-
YTA4NWVmMTJmMmZhMmFiNGY0NTM4Mjg5YjhlMDRhM2FkNDcxYWE=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
ZGZmNzQ0YmExMGMzZjlkY2U2Yzk5MzgyMWUwOTJhNzFmYjkxNTFkMGZmODNl
|
14
|
-
MmI4OGJlMmI0OGJiZjZhN2MwYzE3MDJiZWUzNzlhNzE1NDQ0OTQzODQxNTdj
|
15
|
-
ZGMwNGExNDljYTU4MjY1OTBjMWYzZTIzM2Q2YTdkMTY4OWI2Mzc=
|
6
|
+
metadata.gz: 2f644cc58f1b8accd8785eeffc3fc74bad4c20677032455d96e655944ee0818f99802ed339a3f5f9049acd57ea1166817f6c01feba926c9b61256260239c6354
|
7
|
+
data.tar.gz: c0d4edd095918225bea29707df4e8e0364b16218c367b40f72c5541fe98232e4698e83e58ea8260b7caeeea54d314ec343ea25050f1f7847747d01a003c68221
|
data/CHANGELOG.md
CHANGED
@@ -1,103 +1,133 @@
|
|
1
|
-
|
2
|
-
==================
|
1
|
+
# Changelog
|
3
2
|
|
4
|
-
|
5
|
-
* Document raised exception in the `authenticate` method
|
6
|
-
* Fixes em-http-request from using v2.5.0 of `addressable` breaking builds.
|
3
|
+
## 2.0.3
|
7
4
|
|
8
|
-
|
9
|
-
==================
|
5
|
+
* [FIXED] Corrected the channels limit when publishing events. Upped from 10 to 100.
|
10
6
|
|
11
|
-
|
7
|
+
## 2.0.2
|
12
8
|
|
13
|
-
|
14
|
-
==================
|
9
|
+
* [CHANGED] made encryption_master_key_base64 globally configurable
|
15
10
|
|
16
|
-
|
11
|
+
## 2.0.1
|
17
12
|
|
18
|
-
|
19
|
-
==================
|
13
|
+
* [CHANGED] Only include lib and essential docs in gem.
|
20
14
|
|
21
|
-
|
15
|
+
## 2.0.0
|
22
16
|
|
23
|
-
|
24
|
-
|
17
|
+
* [CHANGED] Use TLS by default.
|
18
|
+
* [REMOVED] Support for Ruby 2.4 and 2.5.
|
19
|
+
* [FIXED] Handle empty or nil configuration.
|
20
|
+
* [REMOVED] Legacy Push Notification integration.
|
21
|
+
* [ADDED] Stalebot and Github actions.
|
22
|
+
|
23
|
+
## 1.4.3
|
25
24
|
|
26
|
-
*
|
25
|
+
* [FIXED] Remove newline from end of base64 encoded strings, some decoders don't like
|
26
|
+
them.
|
27
27
|
|
28
|
-
1.
|
28
|
+
## 1.4.2
|
29
29
|
==================
|
30
30
|
|
31
|
-
*
|
31
|
+
* [FIXED] Return `shared_secret` to support authenticating encrypted channels. Thanks
|
32
|
+
@Benjaminpjacobs
|
32
33
|
|
33
|
-
1.
|
34
|
-
==================
|
34
|
+
## 1.4.1
|
35
35
|
|
36
|
-
|
37
|
-
|
36
|
+
* [CHANGED] Remove rbnacl from dependencies so we don't get errors when it isn't
|
37
|
+
required. Thanks @y-yagi!
|
38
38
|
|
39
|
-
|
40
|
-
==================
|
39
|
+
## 1.4.0
|
41
40
|
|
42
|
-
*
|
43
|
-
* Improve error handling on missing config
|
41
|
+
* [ADDED] Support for end-to-end encryption.
|
44
42
|
|
45
|
-
|
46
|
-
==================
|
43
|
+
## 1.3.3
|
47
44
|
|
48
|
-
*
|
45
|
+
* [CHANGED] Rewording to clarify "Pusher Channels" or simply "Channels" product name.
|
49
46
|
|
50
|
-
|
51
|
-
==================
|
47
|
+
## 1.3.2
|
52
48
|
|
53
|
-
*
|
54
|
-
*
|
49
|
+
* [FIXED] Return a specific error for "Request Entity Too Large" (body over 10KB).
|
50
|
+
* [ADDED] Add a `use_tls` option for SSL (defaults to false).
|
51
|
+
* [ADDED] Add a `from_url` client method (in addition to existing `from_env` option).
|
52
|
+
* [CHANGED] Improved documentation and fixed typos.
|
53
|
+
* [ADDED] Add Ruby 2.4 to test matrix.
|
55
54
|
|
56
|
-
|
57
|
-
==================
|
55
|
+
## 1.3.1
|
58
56
|
|
59
|
-
*
|
60
|
-
*
|
57
|
+
* [FIXED] Added missing client batch methods to default client delegations
|
58
|
+
* [CHANGED] Document raised exception in the `authenticate` method
|
59
|
+
* [FIXED] Fixes em-http-request from using v2.5.0 of `addressable` breaking builds.
|
61
60
|
|
62
|
-
|
63
|
-
==================
|
61
|
+
## 1.3.0
|
64
62
|
|
65
|
-
*
|
63
|
+
* [ADDED] Add support for sending push notifications on up to 10 interests.
|
66
64
|
|
67
|
-
|
68
|
-
==================
|
65
|
+
## 1.2.1
|
69
66
|
|
70
|
-
*
|
71
|
-
This is prefered over the older `Pusher['a_channel'].authenticate(...)` style.
|
67
|
+
* [FIXED] Fixes Rails 5 compatibility. Use duck-typing to detect request object
|
72
68
|
|
73
|
-
|
74
|
-
==================
|
75
|
-
* Updated to use the `pusher-signature` gem instead of `signature`.
|
76
|
-
This resolves namespace related issues.
|
69
|
+
## 1.2.0
|
77
70
|
|
78
|
-
|
79
|
-
==================
|
71
|
+
* [CHANGED] Minor release for Native notifications
|
80
72
|
|
81
|
-
|
73
|
+
## 1.2.0.rc1
|
82
74
|
|
83
|
-
|
84
|
-
==================
|
75
|
+
* [ADDED] Add support for Native notifications
|
85
76
|
|
86
|
-
|
87
|
-
* SECURITY: Prevent POODLE. Disable SSLv3, update httpclient to v2.5
|
88
|
-
* Fix channel name character limit.
|
89
|
-
* Adds support for listing users on a presence channel
|
77
|
+
## 1.1.0
|
90
78
|
|
91
|
-
|
92
|
-
==================
|
79
|
+
* [ADDED] Add support for batch events
|
93
80
|
|
94
|
-
|
81
|
+
## 1.0.0
|
95
82
|
|
96
|
-
|
97
|
-
|
83
|
+
* [CHANGED] No breaking changes, this release is just to follow semver and show that we
|
84
|
+
are stable.
|
85
|
+
|
86
|
+
## 0.18.0
|
87
|
+
|
88
|
+
* [ADDED] Introduce `Pusher::Client.from_env`
|
89
|
+
* [FIXED] Improve error handling on missing config
|
90
|
+
|
91
|
+
## 0.17.0
|
92
|
+
|
93
|
+
* [ADDED] Introduce the `cluster` option.
|
94
|
+
|
95
|
+
## 0.16.0
|
96
|
+
|
97
|
+
* [CHANGED] Bump httpclient version to 2.7
|
98
|
+
* [REMOVED] Ruby 1.8.7 is not supported anymore.
|
99
|
+
|
100
|
+
## 0.15.2
|
101
|
+
|
102
|
+
* [CHANGED] Documented `Pusher.channel_info`, `Pusher.channels`
|
103
|
+
* [ADDED] Added `Pusher.channel_users`
|
104
|
+
|
105
|
+
## 0.15.1
|
106
|
+
|
107
|
+
* [FIXED] Fixed a bug where the `authenticate` method added in 0.15.0 wasn't exposed on the Pusher class.
|
108
|
+
|
109
|
+
## 0.15.0
|
110
|
+
|
111
|
+
* [ADDED] Added `Pusher.authenticate` method for authenticating private and presence channels.
|
112
|
+
This is prefered over the older `Pusher['a_channel'].authenticate(...)` style.
|
113
|
+
|
114
|
+
## 0.14.6
|
115
|
+
|
116
|
+
* [CHANGED] Updated to use the `pusher-signature` gem instead of `signature`.
|
117
|
+
This resolves namespace related issues.
|
118
|
+
|
119
|
+
## 0.14.5
|
120
|
+
|
121
|
+
* [SECURITY] Prevent auth delegation trough crafted socket IDs
|
122
|
+
|
123
|
+
## 0.14.4
|
98
124
|
|
99
|
-
|
125
|
+
* [SECURITY] Prevent timing attack, update signature to v0.1.8
|
126
|
+
* [SECURITY] Prevent POODLE. Disable SSLv3, update httpclient to v2.5
|
127
|
+
* [FIXED] Fix channel name character limit.
|
128
|
+
* [ADDED] Adds support for listing users on a presence channel
|
100
129
|
|
101
|
-
|
102
|
-
* Fix limited channel count at README.md. Thanks @tricknotes
|
130
|
+
## 0.14.2
|
103
131
|
|
132
|
+
* [CHANGED] Bump httpclient to v2.4. See #62 (POODLE SSL)
|
133
|
+
* [CHANGED] Fix limited channel count at README.md. Thanks @tricknotes
|
data/README.md
CHANGED
@@ -1,11 +1,16 @@
|
|
1
|
-
Pusher
|
2
|
-
==========
|
1
|
+
# Gem for Pusher Channels
|
3
2
|
|
4
|
-
[
|
3
|
+
This Gem provides a Ruby interface to [the Pusher HTTP API for Pusher Channels](https://pusher.com/docs/channels/library_auth_reference/rest-api).
|
5
4
|
|
6
|
-
|
5
|
+
[![Build Status](https://github.com/pusher/pusher-http-ruby/workflows/Tests/badge.svg)](https://github.com/pusher/pusher-http-ruby/actions?query=workflow%3ATests+branch%3Amaster) [![Gem](https://img.shields.io/gem/v/pusher)](https://rubygems.org/gems/pusher) [![Gem](https://img.shields.io/gem/dt/pusher)](https://rubygems.org/gems/pusher)
|
7
6
|
|
8
|
-
|
7
|
+
## Supported Platforms
|
8
|
+
|
9
|
+
* Ruby - supports **Ruby 2.6 or greater**.
|
10
|
+
|
11
|
+
## Installation and Configuration
|
12
|
+
|
13
|
+
Add `pusher` to your Gemfile, and then run `bundle install`
|
9
14
|
|
10
15
|
``` ruby
|
11
16
|
gem 'pusher'
|
@@ -17,68 +22,60 @@ or install via gem
|
|
17
22
|
gem install pusher
|
18
23
|
```
|
19
24
|
|
20
|
-
After registering at
|
25
|
+
After registering at [Pusher](https://dashboard.pusher.com/accounts/sign_up), configure your Channels app with the security credentials.
|
21
26
|
|
22
|
-
### Instantiating a Pusher client
|
27
|
+
### Instantiating a Pusher Channels client
|
23
28
|
|
24
|
-
Creating a new Pusher `client` can be done as follows.
|
29
|
+
Creating a new Pusher Channels `client` can be done as follows.
|
25
30
|
|
26
31
|
``` ruby
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
32
|
+
require 'pusher'
|
33
|
+
|
34
|
+
pusher = Pusher::Client.new(
|
35
|
+
app_id: 'your-app-id',
|
36
|
+
key: 'your-app-key',
|
37
|
+
secret: 'your-app-secret',
|
38
|
+
cluster: 'your-app-cluster',
|
39
|
+
use_tls: true
|
31
40
|
)
|
32
41
|
```
|
33
42
|
|
34
|
-
|
43
|
+
The `cluster` value will set the `host` to `api-<cluster>.pusher.com`. The `use_tls` value is optional and defaults to `true`. It will set the `scheme` and `port`. A custom `port` value takes precendence over `use_tls`.
|
35
44
|
|
36
|
-
|
37
|
-
pusher_client = Pusher::Client.new(
|
38
|
-
app_id: 'your-pusher-app-id',
|
39
|
-
key: 'your-pusher-key',
|
40
|
-
secret: 'your-pusher-secret',
|
41
|
-
host: 'your-pusher-host'
|
42
|
-
)
|
43
|
-
```
|
44
|
-
|
45
|
-
If you created your app in a different cluster to the default cluster, you must pass the `cluster` option as follows:
|
45
|
+
If you want to set a custom `host` value for your client then you can do so when instantiating a Pusher Channels client like so:
|
46
46
|
|
47
47
|
``` ruby
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
48
|
+
require 'pusher'
|
49
|
+
|
50
|
+
pusher = Pusher::Client.new(
|
51
|
+
app_id: 'your-app-id',
|
52
|
+
key: 'your-app-key',
|
53
|
+
secret: 'your-app-secret',
|
54
|
+
host: 'your-app-host'
|
53
55
|
)
|
54
56
|
```
|
55
57
|
|
56
|
-
|
58
|
+
If you pass both `host` and `cluster` options, the `host` will take precendence and `cluster` will be ignored.
|
57
59
|
|
58
60
|
Finally, if you have the configuration set in an `PUSHER_URL` environment
|
59
61
|
variable, you can use:
|
60
62
|
|
61
63
|
``` ruby
|
62
|
-
|
64
|
+
pusher = Pusher::Client.from_env
|
63
65
|
```
|
64
66
|
|
65
|
-
### Global
|
67
|
+
### Global configuration
|
66
68
|
|
67
|
-
|
68
|
-
|
69
|
-
``` ruby
|
70
|
-
Pusher.app_id = 'your-pusher-app-id'
|
71
|
-
Pusher.key = 'your-pusher-key'
|
72
|
-
Pusher.secret = 'your-pusher-secret'
|
73
|
-
```
|
74
|
-
|
75
|
-
If you created your app in a different cluster to the default cluster, you must set it as follows:
|
69
|
+
The library can also be configured globally on the `Pusher` class.
|
76
70
|
|
77
71
|
``` ruby
|
72
|
+
Pusher.app_id = 'your-app-id'
|
73
|
+
Pusher.key = 'your-app-key'
|
74
|
+
Pusher.secret = 'your-app-secret'
|
78
75
|
Pusher.cluster = 'your-app-cluster'
|
79
76
|
```
|
80
77
|
|
81
|
-
Global configuration will automatically be set from the `PUSHER_URL` environment variable if it exists. This should be in the form `http://KEY:SECRET@
|
78
|
+
Global configuration will automatically be set from the `PUSHER_URL` environment variable if it exists. This should be in the form `http://KEY:SECRET@HOST/apps/APP_ID`. On Heroku this environment variable will already be set.
|
82
79
|
|
83
80
|
If you need to make requests via a HTTP proxy then it can be configured
|
84
81
|
|
@@ -86,10 +83,11 @@ If you need to make requests via a HTTP proxy then it can be configured
|
|
86
83
|
Pusher.http_proxy = 'http://(user):(password)@(host):(port)'
|
87
84
|
```
|
88
85
|
|
89
|
-
By default API requests are made over
|
86
|
+
By default API requests are made over HTTPS. HTTP can be used by setting `use_tls` to `false`.
|
87
|
+
Issuing this command is going to reset `port` value if it was previously specified.
|
90
88
|
|
91
89
|
``` ruby
|
92
|
-
Pusher.
|
90
|
+
Pusher.use_tls = false
|
93
91
|
```
|
94
92
|
|
95
93
|
As of version 0.12, SSL certificates are verified when using the synchronous http client. If you need to disable this behaviour for any reason use:
|
@@ -98,9 +96,9 @@ As of version 0.12, SSL certificates are verified when using the synchronous htt
|
|
98
96
|
Pusher.default_client.sync_http_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
99
97
|
```
|
100
98
|
|
101
|
-
## Interacting with the
|
99
|
+
## Interacting with the Channels HTTP API
|
102
100
|
|
103
|
-
The
|
101
|
+
The `pusher` gem contains a number of helpers for interacting with the API. As a general rule, the library adheres to a set of conventions that we have aimed to make universal.
|
104
102
|
|
105
103
|
### Handling errors
|
106
104
|
|
@@ -108,7 +106,7 @@ Handle errors by rescuing `Pusher::Error` (all errors are descendants of this er
|
|
108
106
|
|
109
107
|
``` ruby
|
110
108
|
begin
|
111
|
-
|
109
|
+
pusher.trigger('a_channel', 'an_event', :some => 'data')
|
112
110
|
rescue Pusher::Error => e
|
113
111
|
# (Pusher::AuthenticationError, Pusher::HTTPError, or Pusher::Error)
|
114
112
|
end
|
@@ -127,14 +125,14 @@ Pusher.logger = Rails.logger
|
|
127
125
|
An event can be published to one or more channels (limited to 10) in one API call:
|
128
126
|
|
129
127
|
``` ruby
|
130
|
-
|
131
|
-
|
128
|
+
pusher.trigger('channel', 'event', foo: 'bar')
|
129
|
+
pusher.trigger(['channel_1', 'channel_2'], 'event_name', foo: 'bar')
|
132
130
|
```
|
133
131
|
|
134
|
-
An optional fourth argument may be used to send additional parameters to the API, for example to [exclude a single connection from receiving the event](
|
132
|
+
An optional fourth argument may be used to send additional parameters to the API, for example to [exclude a single connection from receiving the event](https://pusher.com/docs/channels/server_api/excluding-event-recipients).
|
135
133
|
|
136
134
|
``` ruby
|
137
|
-
|
135
|
+
pusher.trigger('channel', 'event', {foo: 'bar'}, {socket_id: '123.456'})
|
138
136
|
```
|
139
137
|
|
140
138
|
#### Batches
|
@@ -143,8 +141,8 @@ It's also possible to send multiple events with a single API call (max 10
|
|
143
141
|
events per call on multi-tenant clusters):
|
144
142
|
|
145
143
|
``` ruby
|
146
|
-
|
147
|
-
{channel: 'channel_1', name: 'event_name', data: { foo: 'bar' }}
|
144
|
+
pusher.trigger_batch([
|
145
|
+
{channel: 'channel_1', name: 'event_name', data: { foo: 'bar' }},
|
148
146
|
{channel: 'channel_1', name: 'event_name', data: { hello: 'world' }}
|
149
147
|
])
|
150
148
|
```
|
@@ -157,69 +155,69 @@ Most examples and documentation will refer to the following syntax for triggerin
|
|
157
155
|
Pusher['a_channel'].trigger('an_event', :some => 'data')
|
158
156
|
```
|
159
157
|
|
160
|
-
This will continue to work, but has been replaced by `
|
158
|
+
This will continue to work, but has been replaced by `pusher.trigger` which supports one or multiple channels.
|
161
159
|
|
162
|
-
###
|
160
|
+
### Getting information about the channels in your Pusher Channels app
|
163
161
|
|
164
|
-
This gem provides methods for accessing information from the [
|
162
|
+
This gem provides methods for accessing information from the [Channels HTTP API](https://pusher.com/docs/channels/library_auth_reference/rest-api). The documentation also shows an example of the responses from each of the API endpoints.
|
165
163
|
|
166
164
|
The following methods are provided by the gem.
|
167
165
|
|
168
|
-
- `
|
166
|
+
- `pusher.channel_info('channel_name', {info:"user_count,subscription_count"})` returns a hash describing the state of the channel([docs](https://pusher.com/docs/channels/library_auth_reference/rest-api#get-channels-fetch-info-for-multiple-channels-)).
|
169
167
|
|
170
|
-
- `
|
168
|
+
- `pusher.channel_users('presence-channel_name')` returns a list of all the users subscribed to the channel (only for Presence Channels) ([docs](https://pusher.com/docs/channels/library_auth_reference/rest-api#get-channels-fetch-info-for-multiple-channels-)).
|
171
169
|
|
172
|
-
- `
|
170
|
+
- `pusher.channels({filter_by_prefix: 'presence-', info: 'user_count'})` returns a hash of occupied channels (optionally filtered by prefix, f.i. `presence-`), and optionally attributes for these channels ([docs](https://pusher.com/docs/channels/library_auth_reference/rest-api#get-channels-fetch-info-for-multiple-channels-)).
|
173
171
|
|
174
172
|
### Asynchronous requests
|
175
173
|
|
176
174
|
There are two main reasons for using the `_async` methods:
|
177
175
|
|
178
|
-
* In a web application where the response from
|
176
|
+
* In a web application where the response from the Channels HTTP API is not used, but you'd like to avoid a blocking call in the request-response cycle
|
179
177
|
* Your application is running in an event loop and you need to avoid blocking the reactor
|
180
178
|
|
181
179
|
Asynchronous calls are supported either by using an event loop (eventmachine, preferred), or via a thread.
|
182
180
|
|
183
181
|
The following methods are available (in each case the calling interface matches the non-async version):
|
184
182
|
|
185
|
-
* `
|
186
|
-
* `
|
187
|
-
* `
|
183
|
+
* `pusher.get_async`
|
184
|
+
* `pusher.post_async`
|
185
|
+
* `pusher.trigger_async`
|
188
186
|
|
189
|
-
It is of course also possible to make calls to
|
187
|
+
It is of course also possible to make calls to the Channels HTTP API via a job queue. This approach is recommended if you're sending a large number of events.
|
190
188
|
|
191
|
-
#### With
|
189
|
+
#### With EventMachine
|
192
190
|
|
193
191
|
* Add the `em-http-request` gem to your Gemfile (it's not a gem dependency).
|
194
|
-
* Run the
|
192
|
+
* Run the EventMachine reactor (either using `EM.run` or by running inside an evented server such as Thin).
|
195
193
|
|
196
194
|
The `_async` methods return an `EM::Deferrable` which you can bind callbacks to:
|
197
195
|
|
198
196
|
``` ruby
|
199
|
-
|
197
|
+
pusher.get_async("/channels").callback { |response|
|
200
198
|
# use reponse[:channels]
|
201
199
|
}.errback { |error|
|
202
200
|
# error is an instance of Pusher::Error
|
203
201
|
}
|
204
202
|
```
|
205
203
|
|
206
|
-
A HTTP error or an error response from
|
204
|
+
A HTTP error or an error response from Channels will cause the errback to be called with an appropriate error object.
|
207
205
|
|
208
|
-
#### Without
|
206
|
+
#### Without EventMachine
|
209
207
|
|
210
|
-
If the
|
208
|
+
If the EventMachine reactor is not running, async requests will be made using threads (managed by the httpclient gem).
|
211
209
|
|
212
210
|
An `HTTPClient::Connection` object is returned immediately which can be [interrogated](http://rubydoc.info/gems/httpclient/HTTPClient/Connection) to discover the status of the request. The usual response checking and processing is not done when the request completes, and frankly this method is most useful when you're not interested in waiting for the response.
|
213
211
|
|
214
212
|
|
215
213
|
## Authenticating subscription requests
|
216
214
|
|
217
|
-
It's possible to use the gem to authenticate subscription requests to private or presence channels. The `authenticate` method is available on a channel object for this purpose and returns a JSON object that can be returned to the client that made the request. More information on this authentication scheme can be found in the docs on <
|
215
|
+
It's possible to use the gem to authenticate subscription requests to private or presence channels. The `authenticate` method is available on a channel object for this purpose and returns a JSON object that can be returned to the client that made the request. More information on this authentication scheme can be found in the docs on <https://pusher.com/docs/channels/server_api/authenticating-users>
|
218
216
|
|
219
217
|
### Private channels
|
220
218
|
|
221
219
|
``` ruby
|
222
|
-
|
220
|
+
pusher.authenticate('private-my_channel', params[:socket_id])
|
223
221
|
```
|
224
222
|
|
225
223
|
### Presence channels
|
@@ -227,7 +225,7 @@ Pusher.authenticate('private-my_channel', params[:socket_id])
|
|
227
225
|
These work in a very similar way, but require a unique identifier for the user being authenticated, and optionally some attributes that are provided to clients via presence events:
|
228
226
|
|
229
227
|
``` ruby
|
230
|
-
|
228
|
+
pusher.authenticate('presence-my_channel', params[:socket_id],
|
231
229
|
user_id: 'user_id',
|
232
230
|
user_info: {} # optional
|
233
231
|
)
|
@@ -238,7 +236,7 @@ Pusher.authenticate('presence-my_channel', params[:socket_id],
|
|
238
236
|
A WebHook object may be created to validate received WebHooks against your app credentials, and to extract events. It should be created with the `Rack::Request` object (available as `request` in Rails controllers or Sinatra handlers for example).
|
239
237
|
|
240
238
|
``` ruby
|
241
|
-
webhook =
|
239
|
+
webhook = pusher.webhook(request)
|
242
240
|
if webhook.valid?
|
243
241
|
webhook.events.each do |event|
|
244
242
|
case event["name"]
|
@@ -254,62 +252,50 @@ else
|
|
254
252
|
end
|
255
253
|
```
|
256
254
|
|
257
|
-
|
255
|
+
### End-to-end encryption
|
258
256
|
|
259
|
-
|
257
|
+
This library supports [end-to-end encrypted channels](https://pusher.com/docs/channels/using_channels/encrypted-channels). This means that only you and your connected clients will be able to read your messages. Pusher cannot decrypt them. You can enable this feature by following these steps:
|
260
258
|
|
261
|
-
|
259
|
+
1. Add the `rbnacl` gem to your Gemfile (it's not a gem dependency).
|
262
260
|
|
263
|
-
|
261
|
+
2. Install [Libsodium](https://github.com/jedisct1/libsodium), which we rely on to do the heavy lifting. [Follow the installation instructions for your platform.](https://github.com/RubyCrypto/rbnacl/wiki/Installing-libsodium)
|
264
262
|
|
265
|
-
|
263
|
+
3. Encrypted channel subscriptions must be authenticated in the exact same way as private channels. You should therefore [create an authentication endpoint on your server](https://pusher.com/docs/authenticating_users).
|
266
264
|
|
267
|
-
|
268
|
-
- `data`: The content of the notification represented by a Hash. You must supply either the `gcm` or `apns` key. For a detailed list of the acceptable keys, take a look at the [iOS](https://pusher.com/docs/push_notifications/ios/server) and [Android](https://pusher.com/docs/push_notifications/android/server) docs.
|
265
|
+
4. Next, generate your 32 byte master encryption key, encode it as base64 and pass it to the Pusher constructor.
|
269
266
|
|
270
|
-
|
267
|
+
This is secret and you should never share this with anyone.
|
268
|
+
Not even Pusher.
|
271
269
|
|
272
|
-
```
|
273
|
-
|
274
|
-
|
275
|
-
aps: {
|
276
|
-
alert: {
|
277
|
-
body: 'tada'
|
278
|
-
}
|
279
|
-
}
|
280
|
-
}
|
281
|
-
}
|
270
|
+
```bash
|
271
|
+
openssl rand -base64 32
|
272
|
+
```
|
282
273
|
|
283
|
-
|
284
|
-
|
274
|
+
```rb
|
275
|
+
pusher = new Pusher::Client.new({
|
276
|
+
app_id: 'your-app-id',
|
277
|
+
key: 'your-app-key',
|
278
|
+
secret: 'your-app-secret',
|
279
|
+
cluster: 'your-app-cluster',
|
280
|
+
use_tls: true
|
281
|
+
encryption_master_key_base64: '<KEY GENERATED BY PREVIOUS COMMAND>',
|
282
|
+
});
|
283
|
+
```
|
285
284
|
|
286
|
-
|
287
|
-
|
288
|
-
Push notification requests, once submitted to the service are executed asynchronously. To make reporting errors easier, you can supply a `webhook_url` field in the body of the request. This will be used by the service to send a webhook to the supplied URL if there are errors.
|
289
|
-
|
290
|
-
You may also supply a `webhook_level` field in the body, which can either be INFO or DEBUG. It defaults to INFO - where INFO only reports customer facing errors, while DEBUG reports all errors.
|
291
|
-
|
292
|
-
For example:
|
293
|
-
|
294
|
-
```ruby
|
295
|
-
data = {
|
296
|
-
apns: {
|
297
|
-
aps: {
|
298
|
-
alert: {
|
299
|
-
body: "hello"
|
300
|
-
}
|
301
|
-
}
|
302
|
-
},
|
303
|
-
gcm: {
|
304
|
-
notification: {
|
305
|
-
title: "hello",
|
306
|
-
icon: "icon"
|
307
|
-
}
|
308
|
-
},
|
309
|
-
webhook_url: "http://yolo.com",
|
310
|
-
webhook_level: "INFO"
|
311
|
-
}
|
312
|
-
```
|
285
|
+
5. Channels where you wish to use end-to-end encryption should be prefixed with `private-encrypted-`.
|
313
286
|
|
314
|
-
|
287
|
+
6. Subscribe to these channels in your client, and you're done! You can verify it is working by checking out the debug console on the [https://dashboard.pusher.com/](dashboard) and seeing the scrambled ciphertext.
|
288
|
+
|
289
|
+
**Important note: This will __not__ encrypt messages on channels that are not prefixed by `private-encrypted-`.**
|
290
|
+
|
291
|
+
**Limitation**: you cannot trigger a single event on multiple channels in a call to `trigger`, e.g.
|
292
|
+
|
293
|
+
```rb
|
294
|
+
pusher.trigger(
|
295
|
+
['channel-1', 'private-encrypted-channel-2'],
|
296
|
+
'test_event',
|
297
|
+
{ message: 'hello world' },
|
298
|
+
)
|
299
|
+
```
|
315
300
|
|
301
|
+
Rationale: the methods in this library map directly to individual Channels HTTP API requests. If we allowed triggering a single event on multiple channels (some encrypted, some unencrypted), then it would require two API requests: one where the event is encrypted to the encrypted channels, and one where the event is unencrypted for unencrypted channels.
|
data/lib/pusher/channel.rb
CHANGED
@@ -86,6 +86,9 @@ module Pusher
|
|
86
86
|
|
87
87
|
# Request info for a channel
|
88
88
|
#
|
89
|
+
# @example Response
|
90
|
+
# [{:occupied=>true, :subscription_count => 12}]
|
91
|
+
#
|
89
92
|
# @param info [Array] Array of attributes required (as lowercase strings)
|
90
93
|
# @return [Hash] Hash of requested attributes for this channel
|
91
94
|
# @raise [Pusher::Error] on invalid Pusher response - see the error message for more details
|
@@ -99,7 +102,7 @@ module Pusher
|
|
99
102
|
# Only works on presence channels (see: http://pusher.com/docs/client_api_guide/client_presence_channels and https://pusher.com/docs/rest_api)
|
100
103
|
#
|
101
104
|
# @example Response
|
102
|
-
# [{
|
105
|
+
# [{:id=>"4"}]
|
103
106
|
#
|
104
107
|
# @param params [Hash] Hash of parameters for the API - see REST API docs
|
105
108
|
# @return [Hash] Array of user hashes for this channel
|
@@ -146,7 +149,7 @@ module Pusher
|
|
146
149
|
# render :json => Pusher['private-my_channel'].authenticate(params[:socket_id])
|
147
150
|
#
|
148
151
|
# @example Presence channels
|
149
|
-
# render :json => Pusher['
|
152
|
+
# render :json => Pusher['presence-my_channel'].authenticate(params[:socket_id], {
|
150
153
|
# :user_id => current_user.id, # => required
|
151
154
|
# :user_info => { # => optional - for example
|
152
155
|
# :name => current_user.name,
|
@@ -171,6 +174,15 @@ module Pusher
|
|
171
174
|
r
|
172
175
|
end
|
173
176
|
|
177
|
+
def shared_secret(encryption_master_key)
|
178
|
+
return unless encryption_master_key
|
179
|
+
|
180
|
+
secret_string = @name + encryption_master_key
|
181
|
+
digest = OpenSSL::Digest::SHA256.new
|
182
|
+
digest << secret_string
|
183
|
+
digest.digest
|
184
|
+
end
|
185
|
+
|
174
186
|
private
|
175
187
|
|
176
188
|
def validate_socket_id(socket_id)
|