Skip to content

Commit

Permalink
Merge pull request #8 from prometh07/v2.0
Browse files Browse the repository at this point in the history
Minor tweaks
  • Loading branch information
thisredone authored Feb 6, 2017
2 parents 0a75702 + 2f89b09 commit 14850ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
42 changes: 25 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
# deepstream-ruby
deepstream.io ruby client


### Install

```
gem install deepstream
```


### Usage
```ruby
ds = Deepstream::Client.new('localhost')
ds = Deepstream::Client.new('localhost',
autologin: false,
verbose: true,
credentials: { username: 'John', password: 'Doe' })
# or
ds = Deepstream::Client.new('ws://localhost:6020')
# or
ds = Deepstream::Client.new('ws://localhost:6020/deepstream')

# log in to the server
ds.login
# you can use new credentials too
ds.login(username: 'John', password: 'betterDoe')

# check if the websocket connection is opened
ds.connected?

# check if the client is logged in
ds.logged_in?

# Emit events
ds.emit 'my_event'
# or
ds.emit 'my_event', foo: 'bar', bar: 'foo'
# or
ds.emit 'my_event', {foo: 'bar', bar: 'foo'}, timeout: 3
# or
ds.emit 'my_event', nil, timeout: 3


# Subscribe to events
ds.on('some_event') do |msg|
ds.on('some_event') do |event_name, msg|
puts msg
end


# Get a record
foo = ds.get('foo')
bar = ds.get('bar', list: 'bar_list') # get a record within a namespace (this one automatically adds it to a list)

# Get a record with a namespace (automaticly add to a list)
foo = ds.get_record('foo', list: 'bar') # record can also be accessed by ds.get('bar/foo')

# Update record
foo.bar = 'bar'
# or
# Update a record
foo.set('bar', 'bar')

# Set the whole record
Expand All @@ -46,13 +52,15 @@ foo.set(foo: 'foo', bar: 1)
# Get a list
foo = ds.get_list('bar')

# Add to list
# Add to the list
foo.add('foo')

# Remove from list
foo.remove('foo')

# Show record names on the list
foo.data
# or
foo.keys

# Access records on the list
Expand Down
12 changes: 12 additions & 0 deletions lib/deepstream/event_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def on(event, &block)
@ack_timeout_registry.add(event, "No ACK message received in time for #{event}")
end
@callbacks[event] = block
rescue => e
@client.on_exception(e)
end
alias subscribe on

Expand All @@ -25,12 +27,16 @@ def listen(pattern, &block)
@listeners[pattern] = block
@client.send_message(TOPIC::EVENT, ACTION::LISTEN, pattern)
@ack_timeout_registry.add(pattern, "No ACK message received in time for #{pattern}")
rescue => e
@client.on_exception(e)
end

def unlisten(pattern)
pattern = pattern.is_a?(Regexp) ? pattern.source : pattern
@listeners.delete(pattern)
@client.send_message(TOPIC::EVENT, ACTION::UNLISTEN, pattern)
rescue => e
@client.on_exception(e)
end

def on_message(message)
Expand All @@ -45,16 +51,22 @@ def on_message(message)

def emit(event, data = nil)
@client.send_message(TOPIC::EVENT, ACTION::EVENT, event, Helpers.to_deepstream_type(data))
rescue => e
@client.on_exception(e)
end

def unsubscribe(event)
@callbacks.delete(event)
@client.send_message(TOPIC::EVENT, ACTION::UNSUBSCRIBE, event)
rescue => e
@client.on_exception(e)
end

def resubscribe
@callbacks.keys.each { |event| @client.send_message(TOPIC::EVENT, ACTION::SUBSCRIBE, event) }
@listeners.keys.each { |pattern| @client.send_message(TOPIC::EVENT, ACTION::LISTEN, pattern) }
rescue => e
@client.on_exception(e)
end

private
Expand Down
4 changes: 4 additions & 0 deletions lib/deepstream/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def remove(record_name)
set(@data) if @data.delete(record_name)
end

def keys
@data
end

def all
@data.map { |record_name| @client.get(record_name) }
end
Expand Down

0 comments on commit 14850ec

Please sign in to comment.