Sha256: af274a8416360998f8c72d2f82a59167d72e21fceb7306a5540d12d01e2b47a9

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require 'noam_lemma'

# This is an example of a Ruby Lemma that publishes messages. It expects a Noam
# server to be running. It also expects that the noam-lemma.rb file is in the
# search path. If you run this example from the project root, the following
# command should work:
#
#   ruby -Ilib example/publisher.rb

publisher = Noam::Lemma.new('example-publisher', [], ["e1", "e2"])

# Using the `advertise` method asks the Lemma to announce it's presence and
# wait for a message from a server that may want to connect to it.
#
# The "local-test" parameter is the room name. Servers with a room name that's
# the same as the Lemma's advertised room name will connect automatically.
publisher.advertise("local-test")

seq = 0
loop do
  # This method block picks an event to send. It's randomized a bit so that we
  # can see things change over time.
  e = if 0.5 < rand()
        "e1"
      else
        "e2"
      end

  # Next, package the event sequence and the current time into a value.
  v = {"seq" => seq, "time" => Time.now.to_s}

  # Attempt to speak the chosen event with the value. Note, the event is either
  # "e1" or "e2" based on how rand() returned.
  begin
    publisher.speak(e, v)

    # If `speak` returns a Noam::Disconnected error, we're unable to speak the
    # message likely because the socket has closed. The connection
    # would have to be restarted.
  rescue Noam::Disconnected
    puts "Disconnected"
    break
  end

  puts "Wrote: #{e} -> #{v.inspect}"

  seq += 1
  # Sleep for a while so that we don't bog down the network.
  sleep(1)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
noam_lemma-0.2.1.3 examples/publisher.rb