Skip to content

arimay/message_channel

Repository files navigation

MessageChannel

Yet another observer pattern library via Observable, DRb, MQTT, Redis.

Features

  • Thread based observer pattern library.

  • Implemented as a wrapper for Obserbable, DRb, MQTT, Redis.

  • Observer can communicate without using sockets.

  • DRb can communicate between processes without using an external server.

  • MQTT and Redis can create apps that can communicate in other languages.

  • Distribute topic as a character string and data as a JSON-encoded character string.

Installation

Add this line to your application’s Gemfile:

gem 'message_channel'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install message_channel
or
$ gem install -l message_channel-x.x.x.gem

Usage

Example 1

require  "message_channel"

Signal.trap( :INT ) do
  exit
end

channel  =  MessageChannel.new

channel.listen( "hello" ) do |topic, **items|
  p [topic, items]
end

while  true
  channel.notify  "hello", {at: Time.now.to_s}
  sleep  1
end

Example 2

require  "message_channel"

Signal.trap( :INT ) do
  exit
end

channel  =  MessageChannel.new

Thread.start do
  while  true
    topic, items  =  channel.listen( "hello", timeout: 0.5 )
    p [topic, items]
  end
end

Thread.start do
  while  true
    channel.notify  "hello", {at: Time.now.to_s}
    sleep  rand
  end
end

sleep

Reference

Create a new MessageChannel::* with conditions.

MessageChannel.new( uri = nil, type: nil, host: nil, port: nil, db: nil )
  • Result:

    • MessageChannel::* object.

  • Parameter:

    • uri: scheme://host:port/params (default: nil)

      • "observer"

      • "druby://127.0.0.1:8787"

      • "mqtt://127.0.0.1:1883"

      • "redis://127.0.0.1:6379/0"

    • type: Channel type. (default: "observer")

      • "observer"

      • "druby"

      • "mqtt"

      • "redis"

    • host: Service host. (default: nil)

    • port: Service port. (default: nil)

    • db: Database. (default: nil)

Notify through the channel.

MessageChannel::Observer#notify( topic, **items )
MessageChannel::Druby#notify( topic, **items )
MessageChannel::Mqtt#notify( topic, **items )
MessageChannel::Redis#notify( topic, **items )
  • Result:

    • nil.

  • Parameter:

    • topic: a notification topic.

    • items: notification items.

Listen to notification through the channel.

MessageChannel::Observer#listen( *patterns, timeout: nil, &block )
MessageChannel::Druby#listen( *patterns, timeout: nil, &block )
MessageChannel::Mqtt#listen( *patterns, timeout: nil, &block )
MessageChannel::Redis#listen( *patterns, timeout: nil, &block )
  • with block: (async)

    • Result:

      • nil.

    • Parameter:

      • patterns: mask patterns.

  • without block: (await)

    • Result: return nil if timeout.

      • topic: a notified topic.

      • items: notified items.

    • Parameter:

      • patterns: mask patterns.

      • timeout: nil or wait second.

    • Block Parameter:

      • topic: a notified topic.

      • items: notified items.

Unlisten to notification.

MessageChannel::Observer#unlisten( *patterns )
MessageChannel::Druby#unlisten( *patterns )
MessageChannel::Mqtt#unlisten( *patterns )
MessageChannel::Redis#unlisten( *patterns )
  • Result:

    • nil.

  • Parameter:

    • patterns: mask patterns.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/arimay/message_channel.

License

The gem is available as open source under the terms of the MIT License.

Copyright (c) ARIMA Yasuhiro <arima.yasuhiro@gmail.com>

About

Yet another observer pattern library via Observable, DRb, MQTT, Redis or Mongo.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published