Skip to content

Commit 74a13ad

Browse files
committed
Added documentation
Added documentation in README for socketcluster-client-ruby
1 parent d5c2bef commit 74a13ad

File tree

1 file changed

+242
-1
lines changed

1 file changed

+242
-1
lines changed

README.md

Lines changed: 242 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,243 @@
11
# socketcluster-client-ruby
2-
Ruby client for socketcluster
2+
A Ruby client for socketcluster.io
3+
4+
Refer below examples for more details.
5+
6+
Overview
7+
--------
8+
This client provides following functionality
9+
10+
- Easy to setup and use
11+
- Support for emitting and listening to remote events
12+
- Automatic reconnection
13+
- Pub/sub
14+
- Authentication (JWT)
15+
- Can be used for extensive unit-testing of all server side functions
16+
- Support for ruby >= 2.2.0
17+
18+
Installation
19+
------------
20+
21+
Add this line to your application's Gemfile:
22+
23+
```ruby
24+
gem 'socketclusterclient'
25+
```
26+
27+
And then execute:
28+
29+
$ bundle
30+
31+
Or install it yourself as:
32+
33+
$ gem install socketclusterclient
34+
35+
Usage
36+
-----------
37+
Create instance of `Socket` class by passing url of socketcluster-server end-point
38+
39+
```ruby
40+
# Create a socket instance
41+
socket = ScClient.new('ws://localhost:8000/socketcluster/')
42+
```
43+
**Important Note** : Default url to socketcluster end-point is always *ws://somedomainname.com/socketcluster/*.
44+
45+
#### Registering basic listeners
46+
47+
- Different functions are given as an argument to register listeners
48+
49+
```ruby
50+
require 'socketclusterclient'
51+
require 'logger'
52+
53+
logger = Logger.new(STDOUT)
54+
logger.level = Logger::WARN
55+
56+
on_connect = -> { logger.info('on connect got called') }
57+
58+
on_disconnect = -> { logger.info('on disconnect got called') }
59+
60+
on_connect_error = -> { logger.info('on connect error got called') }
61+
62+
on_set_authentication = lambda do |socket, token|
63+
logger.info("Token received #{token}")
64+
socket.set_auth_token(token)
65+
end
66+
67+
on_authentication = lambda do |socket, is_authenticated|
68+
logger.info("Authenticated is #{is_authenticated}")
69+
end
70+
71+
socket = ScClient.new('ws://localhost:8000/socketcluster/')
72+
socket.set_basic_listener(on_connect, on_disconnect, on_connect_error)
73+
socket.set_authentication_listener(on_set_authentication, on_authentication)
74+
socket.connect
75+
```
76+
77+
#### Connecting to server
78+
79+
- For connecting to server
80+
81+
```ruby
82+
# This will send websocket handshake request to socketcluster-server
83+
socket.connect
84+
```
85+
86+
- By default reconnection to server is enabled, so to configure delay for connection
87+
88+
```ruby
89+
# This will set automatic-reconnection to socketcluster-server with delay of 2 seconds and repeat it infinitely
90+
socket.set_delay(2)
91+
socket.connect
92+
```
93+
94+
- For disabling reconnection to server
95+
96+
```ruby
97+
# This will disable reconnection to socketcluster-server
98+
socket.set_reconnection(false)
99+
```
100+
101+
Emitting and listening to events
102+
--------------------------------
103+
104+
#### Event emitter
105+
106+
- To emit a specified event on the corresponding socketcluster-server. The object sent can be String, Boolean, Long or JSONObject.
107+
108+
```ruby
109+
# Emit an event
110+
socket.emit(event_name, message);
111+
112+
socket.emit("chat", "Hi")
113+
```
114+
115+
- To emit a specified event with acknowledgement on the corresponding socketcluster-server. The object sent can be String, Boolean, Long or JSONObject.
116+
117+
```ruby
118+
# Emit an event with acknowledgment
119+
socket.emitack(event_name, message, ack_emit)
120+
121+
socket.emitack('chat', 'Hello', ack_emit)
122+
123+
ack_emit = lambda do |key, error, object|
124+
puts "Got ack data => #{object} and error => #{error} and key => #{key}"
125+
end
126+
```
127+
128+
#### Event Listener
129+
130+
- To listen an event from the corresponding socketcluster-server. The object received can be String, Boolean, Long or JSONObject.
131+
132+
```ruby
133+
# Receive an event
134+
socket.on(event, message)
135+
136+
socket.on('ping', message)
137+
138+
message = lambda do |key, object|
139+
puts "Got data => #{object} from key => #{key}"
140+
end
141+
```
142+
143+
- To listen an event from the corresponding socketcluster-server and send the acknowledgment back. The object received can be String, Boolean, Long or JSONObject.
144+
145+
```ruby
146+
# Receive an event and send the acknowledgement back
147+
socket.onack(event, message_with_acknowledgment)
148+
149+
socket.onack('ping', ack_message)
150+
151+
ack_message = lambda do |key, object, block_ack_message|
152+
puts "Got ack data => #{object} from key => #{key}"
153+
block_ack_message.call('error lorem', 'data ipsum')
154+
end
155+
```
156+
157+
Implementing Pub-Sub via channels
158+
---------------------------------
159+
160+
#### Creating a channel
161+
162+
- For creating and subscribing to channels
163+
164+
```ruby
165+
# Subscribe to channel
166+
socket.subscribe('yell')
167+
168+
# Subscribe to channel with acknowledgement
169+
socket.subscribeack('yell', ack_subscribe)
170+
171+
ack_subscribe = lambda do |channel, error, _object|
172+
puts "Subscribed successfully to channel => #{channel}" if error == ''
173+
end
174+
```
175+
176+
- For getting list of created channels
177+
178+
```ruby
179+
# Get all subscribed channel
180+
channels = socket.get_subscribed_channels
181+
```
182+
183+
#### Publishing an event on channel
184+
185+
- For publishing an event
186+
187+
```ruby
188+
# Publish to a channel
189+
socket.publish('yell', 'Hi')
190+
191+
# Publish to a channel with acknowledgment
192+
socket.publishack('yell', 'Hi', ack_publish)
193+
194+
ack_publish = lambda do |channel, error, _object|
195+
puts "Publish sent successfully to channel => #{channel}" if error == ''
196+
end
197+
```
198+
199+
#### Listening to channel
200+
201+
- For listening to a channel event
202+
203+
```ruby
204+
# Listen to a channel
205+
socket.onchannel('yell', channel_message)
206+
207+
channel_message = lambda do |key, object|
208+
puts "Got data => #{object} from key => #{key}"
209+
end
210+
```
211+
212+
#### Un-subscribing to channel
213+
214+
- For unsubscribing to a channel
215+
216+
```ruby
217+
# Unsubscribe to a channel
218+
socket.unsubscribe('yell')
219+
220+
# Unsubscribe to a channel with acknowledgement
221+
socket.unsubscribeack('yell', ack_unsubscribe)
222+
223+
ack_unsubscribe = lambda do |channel, error, _object|
224+
puts "Unsubscribed to channel => #{channel}" if error == ''
225+
end
226+
```
227+
228+
Development
229+
-----------
230+
231+
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.
232+
233+
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).
234+
235+
Contributing
236+
------------
237+
238+
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.
239+
240+
License
241+
-------
242+
243+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

0 commit comments

Comments
 (0)