Live Zyre

live.zyre.com is a new experimental service that lets you test Zyre without any installation hassle. Just point your RestMS application to live.zyre.com and run.

Problem is, you don't have a RestMS application to try. So, we'll make one. We'll make it in Perl1 using the RestMS.pm module. This is a microblogging application2 in two halves. The first half just sends one line of text to the "ublog" feed. The second half listens to that feed and prints out everything it receives.

It's simplest if you download the RestMS.pm module straight into where you write and test this application. Here is the first half, uspeak.pl:

#!/usr/bin/perl
#
#   uSpeak app, writes a line of text to the ublog feed
#   perl uspeak.pl "some line of text"
#
#   (c) 2009 iMatix, may be freely used in any way desired 
#   with no conditions or restrictions.
#
use RestMS ();
my $domain = RestMS::Domain->new (hostname => "live.zyre.com");
my $feed = $domain->feed (name => "ublog", type => "fanout");
my $message = RestMS::Message->new;
$message->content (shift);
$message->headers (name => "Jeep Nine Thirst");
$feed->send ($message);

And here is the second half, ulisten.pl just a little more complex since it saves/restores its pipe:

#!/usr/bin/perl
#
#   uListen app, writes a line of text to the ublog feed
#   perl ulisten.pl
#
#   (c) 2009 iMatix, may be freely used in any way desired 
#   with no conditions or restrictions.
#
use RestMS ();
my $domain = RestMS::Domain->new (hostname => "live.zyre.com");
$domain->verbose (0);
my $feed = $domain->feed (name => "ublog", type => "fanout");

#   Cache the pipe name to reuse existing pipes (polite!)
#   This also ensures we'll catch on whatever we missed...
open (FILE, "ulisten.cfg"); @config = <FILE>; close (FILE);
my $pipe = $domain->pipe (name => $config [0]);
open (FILE, ">ulisten.cfg"); print FILE $pipe->name; close (FILE);

#   In any case, connect our pipe to the ublog feed
my $join = $feed->join (pipe => $pipe);

#   Now listen and print whatever people say
while (1) {
    $message = $pipe->recv;
    $pipe->carp ($message->headers (name).":".$message->content);
}

To run, you need certain Perl modules. Sorry about this, it's the way things go. What I did was:

sudo cpan

and then after pressing Enter a lot of times,

install LWP
install Alias
install XML::Simple

It'll depend on what was already on your machine.

Since we're using live.zyre.com, no Zyre install is necessary. I've already hardcoded that domain name into the scripts. So, download the three files - RestMS.pm, ulisten.pl, and uspeak.pl - and then run two perl commands, in two windows:

perl ulisten.pl

and

perl uspeak.pl "hey, I'm running my first restms app!"

If you want to show your 133t skillz, edit uspeak.pl to put your own name.

What does this example show?

  • How to create feeds and pipes
  • How to wait for, and process messages
  • How to make persistent pipes

Enjoy!

Add a New Comment
Page tags: front