socketclusterclient 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.simplecov +1 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +54 -0
- data/LICENSE +21 -0
- data/README.md +307 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +6 -0
- data/examples/authentication.rb +22 -0
- data/examples/channel.rb +49 -0
- data/examples/emitter.rb +31 -0
- data/examples/receiver.rb +36 -0
- data/examples/reconnection.rb +25 -0
- data/examples/ssl_connect.rb +12 -0
- data/lib/sc_client.rb +365 -0
- data/lib/socketclusterclient.rb +11 -0
- data/lib/socketclusterclient/data_models.rb +120 -0
- data/lib/socketclusterclient/emitter.rb +91 -0
- data/lib/socketclusterclient/log.rb +47 -0
- data/lib/socketclusterclient/parser.rb +39 -0
- data/lib/socketclusterclient/reconnect.rb +47 -0
- data/lib/socketclusterclient/version.rb +3 -0
- data/socketclusterclient.gemspec +32 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3d7a4cf9273a946fcc59caded2ae7c4771fa0fa4
|
4
|
+
data.tar.gz: e7dd5a0dcc605561d0189290d30b4a7d34e82898
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b2ad8016f8cd5c6361516a1a862e63349acd7b0ff0ac7ca4df0ef54a02fb01f5c364c516ebf9771530427be44636e3b2c15ced6f6f45c0e3b9ddd0366800d946
|
7
|
+
data.tar.gz: 1d0a22609ea8e734108ef52dd9f1bbf8adbc8350ec5ad784f9e5ebf5be5f43348e325ca0750e25a9d2a540fa8e199d888113c94afc6a6c143ce7aca041017e12
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.simplecov
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
SimpleCov.start
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
git_source(:github) { |_repo_name| 'https://github.com/opensocket' }
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in socketclusterclient.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'websocket-eventmachine-client'
|
9
|
+
|
10
|
+
gem 'rspec', require: false, group: :test
|
11
|
+
gem 'simplecov', require: false, group: :test
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
socketclusterclient (0.1.0)
|
5
|
+
websocket-eventmachine-client (~> 1.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.3)
|
11
|
+
docile (1.3.1)
|
12
|
+
eventmachine (1.2.7)
|
13
|
+
json (2.1.0)
|
14
|
+
rake (10.5.0)
|
15
|
+
rspec (3.7.0)
|
16
|
+
rspec-core (~> 3.7.0)
|
17
|
+
rspec-expectations (~> 3.7.0)
|
18
|
+
rspec-mocks (~> 3.7.0)
|
19
|
+
rspec-core (3.7.1)
|
20
|
+
rspec-support (~> 3.7.0)
|
21
|
+
rspec-expectations (3.7.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.7.0)
|
24
|
+
rspec-mocks (3.7.0)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.7.0)
|
27
|
+
rspec-support (3.7.1)
|
28
|
+
simplecov (0.16.1)
|
29
|
+
docile (~> 1.1)
|
30
|
+
json (>= 1.8, < 3)
|
31
|
+
simplecov-html (~> 0.10.0)
|
32
|
+
simplecov-html (0.10.2)
|
33
|
+
websocket (1.2.8)
|
34
|
+
websocket-eventmachine-base (1.2.0)
|
35
|
+
eventmachine (~> 1.0)
|
36
|
+
websocket (~> 1.0)
|
37
|
+
websocket-native (~> 1.0)
|
38
|
+
websocket-eventmachine-client (1.2.0)
|
39
|
+
websocket-eventmachine-base (~> 1.0)
|
40
|
+
websocket-native (1.0.0)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
bundler (~> 1.16)
|
47
|
+
rake (~> 10.0)
|
48
|
+
rspec
|
49
|
+
simplecov
|
50
|
+
socketclusterclient!
|
51
|
+
websocket-eventmachine-client
|
52
|
+
|
53
|
+
BUNDLED WITH
|
54
|
+
1.16.1
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
OpenSocket License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Maanav Shah
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,307 @@
|
|
1
|
+
# socketcluster-client-ruby
|
2
|
+
A Ruby client for socketcluster.io
|
3
|
+
|
4
|
+
In `lib/socketclusterclient`, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file . To experiment with that code, run `bin/console` for an interactive prompt.
|
5
|
+
|
6
|
+
Refer below examples for more details.
|
7
|
+
|
8
|
+
Overview
|
9
|
+
--------
|
10
|
+
This client provides following functionality
|
11
|
+
|
12
|
+
- Easy to setup and use
|
13
|
+
- Support for emitting and listening to remote events
|
14
|
+
- Automatic reconnection
|
15
|
+
- Pub/sub
|
16
|
+
- Authentication (JWT)
|
17
|
+
- Can be used for extensive unit-testing of all server side functions
|
18
|
+
- Support for ruby >= 2.2.0
|
19
|
+
|
20
|
+
Installation
|
21
|
+
------------
|
22
|
+
|
23
|
+
Add this line to your application's Gemfile:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'socketclusterclient'
|
27
|
+
```
|
28
|
+
|
29
|
+
And then execute:
|
30
|
+
|
31
|
+
$ bundle
|
32
|
+
|
33
|
+
Or install it yourself as:
|
34
|
+
|
35
|
+
$ gem install socketclusterclient
|
36
|
+
|
37
|
+
Usage
|
38
|
+
-----------
|
39
|
+
Create instance of `Socket` class by passing url of socketcluster-server end-point
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# Create a socket instance
|
43
|
+
socket = ScClient.new('ws://localhost:8000/socketcluster/')
|
44
|
+
```
|
45
|
+
**Important Note** : Default url to socketcluster end-point is always *ws://somedomainname.com/socketcluster/*.
|
46
|
+
|
47
|
+
#### Registering basic listeners
|
48
|
+
|
49
|
+
- Different functions are given as an argument to register listeners
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
require 'socketclusterclient'
|
53
|
+
require 'logger'
|
54
|
+
|
55
|
+
logger = Logger.new(STDOUT)
|
56
|
+
logger.level = Logger::WARN
|
57
|
+
|
58
|
+
on_connect = -> { logger.info('on connect got called') }
|
59
|
+
|
60
|
+
on_disconnect = -> { logger.info('on disconnect got called') }
|
61
|
+
|
62
|
+
on_connect_error = -> { logger.info('on connect error got called') }
|
63
|
+
|
64
|
+
on_set_authentication = lambda do |socket, token|
|
65
|
+
logger.info("Token received #{token}")
|
66
|
+
socket.set_auth_token(token)
|
67
|
+
end
|
68
|
+
|
69
|
+
on_authentication = lambda do |socket, is_authenticated|
|
70
|
+
logger.info("Authenticated is #{is_authenticated}")
|
71
|
+
end
|
72
|
+
|
73
|
+
socket = ScClient.new('ws://localhost:8000/socketcluster/')
|
74
|
+
socket.set_basic_listener(on_connect, on_disconnect, on_connect_error)
|
75
|
+
socket.set_authentication_listener(on_set_authentication, on_authentication)
|
76
|
+
socket.connect
|
77
|
+
```
|
78
|
+
|
79
|
+
#### Connecting to server
|
80
|
+
|
81
|
+
- For connecting to server
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
# This will send websocket handshake request to socketcluster-server
|
85
|
+
socket.connect
|
86
|
+
```
|
87
|
+
|
88
|
+
- By default reconnection to server is disabled, so to enable automatic reconnection
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
# This will set automatic-reconnection to socketcluster-server repeat it infinitely
|
92
|
+
socket.set_reconnection(true)
|
93
|
+
```
|
94
|
+
|
95
|
+
- To set delay of reconnecting to server
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
# This will set automatic-reconnection to socketcluster-server with delay of 2 seconds and repeat it infinitely
|
99
|
+
socket.set_delay(2)
|
100
|
+
socket.connect
|
101
|
+
```
|
102
|
+
|
103
|
+
- To disable automatic reconnection to server
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
# This will disable reconnection to socketcluster-server
|
107
|
+
socket.set_reconnection(false)
|
108
|
+
```
|
109
|
+
|
110
|
+
- For reconnection to the server you need to set reconnection strategy
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
# This will set the reconnection strategy
|
114
|
+
socket.set_reconnection_listener(reconnect_interval, max_reconnect_interval, max_attempts)
|
115
|
+
|
116
|
+
# default reconnection strategy
|
117
|
+
socket.set_reconnection_listener(2000, 30_000, nil)
|
118
|
+
```
|
119
|
+
|
120
|
+
For example,
|
121
|
+
```ruby
|
122
|
+
socket.set_reconnection_listener(3000, 30_000, 10) # (reconnect_inverval, max_reconnect_interval, max_attempts)
|
123
|
+
socket.set_reconnection_listener(3000, 30_000) # (reconnect_inverval, max_reconnect_interval)
|
124
|
+
```
|
125
|
+
|
126
|
+
- You can set reconnect_interval, max_reconnect_interval and max_attempts directly as well
|
127
|
+
```ruby
|
128
|
+
socket.reconnect_interval = 2000
|
129
|
+
socket.max_reconnect_interval = 20_000
|
130
|
+
socket.max_attempts = 10
|
131
|
+
```
|
132
|
+
|
133
|
+
- To allow reconnection to server, you need to reconnect
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
# This will set automatic-reconnection to socketcluster-server with delay of 2 seconds and repeat it till the maximum attempts are finished
|
137
|
+
socket.connect
|
138
|
+
```
|
139
|
+
|
140
|
+
|
141
|
+
Emitting and listening to events
|
142
|
+
--------------------------------
|
143
|
+
|
144
|
+
#### Event emitter
|
145
|
+
|
146
|
+
- To emit a specified event on the corresponding socketcluster-server. The object sent can be String, Boolean, Long or JSONObject.
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
# Emit an event
|
150
|
+
socket.emit(event_name, message);
|
151
|
+
|
152
|
+
socket.emit("chat", "Hi")
|
153
|
+
```
|
154
|
+
|
155
|
+
- To emit a specified event with acknowledgement on the corresponding socketcluster-server. The object sent can be String, Boolean, Long or JSONObject.
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
# Emit an event with acknowledgment
|
159
|
+
socket.emitack(event_name, message, ack_emit)
|
160
|
+
|
161
|
+
socket.emitack('chat', 'Hello', ack_emit)
|
162
|
+
|
163
|
+
ack_emit = lambda do |key, error, object|
|
164
|
+
puts "Got ack data => #{object} and error => #{error} and key => #{key}"
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
#### Event Listener
|
169
|
+
|
170
|
+
- To listen an event from the corresponding socketcluster-server. The object received can be String, Boolean, Long or JSONObject.
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
# Receive an event
|
174
|
+
socket.on(event, message)
|
175
|
+
|
176
|
+
socket.on('ping', message)
|
177
|
+
|
178
|
+
message = lambda do |key, object|
|
179
|
+
puts "Got data => #{object} from key => #{key}"
|
180
|
+
end
|
181
|
+
```
|
182
|
+
|
183
|
+
- To listen an event from the corresponding socketcluster-server and send the acknowledgment back. The object received can be String, Boolean, Long or JSONObject.
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
# Receive an event and send the acknowledgement back
|
187
|
+
socket.onack(event, message_with_acknowledgment)
|
188
|
+
|
189
|
+
socket.onack('ping', ack_message)
|
190
|
+
|
191
|
+
ack_message = lambda do |key, object, block_ack_message|
|
192
|
+
puts "Got ack data => #{object} from key => #{key}"
|
193
|
+
block_ack_message.call('error lorem', 'data ipsum')
|
194
|
+
end
|
195
|
+
```
|
196
|
+
|
197
|
+
Implementing Pub-Sub via channels
|
198
|
+
---------------------------------
|
199
|
+
|
200
|
+
#### Creating a channel
|
201
|
+
|
202
|
+
- For creating and subscribing to channels
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
# Subscribe to channel
|
206
|
+
socket.subscribe('yell')
|
207
|
+
|
208
|
+
# Subscribe to channel with acknowledgement
|
209
|
+
socket.subscribeack('yell', ack_subscribe)
|
210
|
+
|
211
|
+
ack_subscribe = lambda do |channel, error, _object|
|
212
|
+
puts "Subscribed successfully to channel => #{channel}" if error == ''
|
213
|
+
end
|
214
|
+
```
|
215
|
+
|
216
|
+
- For getting list of created channels
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
# Get all subscribed channel
|
220
|
+
channels = socket.subscribed_channels
|
221
|
+
```
|
222
|
+
|
223
|
+
#### Publishing an event on channel
|
224
|
+
|
225
|
+
- For publishing an event
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
# Publish to a channel
|
229
|
+
socket.publish('yell', 'Hi')
|
230
|
+
|
231
|
+
# Publish to a channel with acknowledgment
|
232
|
+
socket.publishack('yell', 'Hi', ack_publish)
|
233
|
+
|
234
|
+
ack_publish = lambda do |channel, error, _object|
|
235
|
+
puts "Publish sent successfully to channel => #{channel}" if error == ''
|
236
|
+
end
|
237
|
+
```
|
238
|
+
|
239
|
+
#### Listening to channel
|
240
|
+
|
241
|
+
- For listening to a channel event
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
# Listen to a channel
|
245
|
+
socket.onchannel('yell', channel_message)
|
246
|
+
|
247
|
+
channel_message = lambda do |key, object|
|
248
|
+
puts "Got data => #{object} from key => #{key}"
|
249
|
+
end
|
250
|
+
```
|
251
|
+
|
252
|
+
#### Un-subscribing to channel
|
253
|
+
|
254
|
+
- For unsubscribing to a channel
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
# Unsubscribe to a channel
|
258
|
+
socket.unsubscribe('yell')
|
259
|
+
|
260
|
+
# Unsubscribe to a channel with acknowledgement
|
261
|
+
socket.unsubscribeack('yell', ack_unsubscribe)
|
262
|
+
|
263
|
+
ack_unsubscribe = lambda do |channel, error, _object|
|
264
|
+
puts "Unsubscribed to channel => #{channel}" if error == ''
|
265
|
+
end
|
266
|
+
```
|
267
|
+
|
268
|
+
Logger
|
269
|
+
------
|
270
|
+
|
271
|
+
#### Disabling logger
|
272
|
+
|
273
|
+
- To get logger for logging of events
|
274
|
+
|
275
|
+
```ruby
|
276
|
+
socket.logger.info("Some information")
|
277
|
+
socket.logger.warn("Some warning")
|
278
|
+
```
|
279
|
+
|
280
|
+
- To disable logging of events
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
socket.disable_logging
|
284
|
+
```
|
285
|
+
|
286
|
+
- To enable logging of events
|
287
|
+
|
288
|
+
```ruby
|
289
|
+
socket.enable_logging
|
290
|
+
```
|
291
|
+
|
292
|
+
Development
|
293
|
+
-----------
|
294
|
+
|
295
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
296
|
+
|
297
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
298
|
+
|
299
|
+
Contributing
|
300
|
+
------------
|
301
|
+
|
302
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/opensocket/socketcluster-client-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
303
|
+
|
304
|
+
License
|
305
|
+
-------
|
306
|
+
|
307
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|