SlideShare une entreprise Scribd logo
1  sur  41
Télécharger pour lire hors ligne
AnyMQ, Hippie
 and the real-time web

      OSDC.TW 2010 Taipei
   Chia-liang Kao clkao@clkao.org
AnyMQ, Hippie, and the real-time web
clkao
Things I’ve
been playing
    with
• PSGI, Plack
• AnyEvent, AnyMQ, AMQP
• Server-push, comet
• Websocket
• Web::Hippie, Web::Hippie:Pipe
• PSGI, Plack
• AnyEvent, AnyMQ, AMQP
• Server-push, comet
• Websocket
• Web::Hippie, Web::Hippie::Pipe
Plack: Perl Web Superglue
PSGI
$env->{PATH_INFO}

       Plack::Request->new($env)->path_info
$env




[
       '200',
       [ 'Content-Type' => 'text/plain' ],
       [ "Hello World" ],
]
$env
                 Streaming Interface

sub {
     my $responder = shift;
      my $writer = $responder->([
           '200',
           [ 'Content-Type' => 'text/plain' ]);
      # later, of in a callback
      $writer->write(“Hello world!”);
      $writer->close();
}
CGI.pm
MUST DIE!
 ☠! ☠! ☠
• PSGI, Plack
• AnyEvent, AnyMQ, AMQP
• Server-push, comet
• Websocket
• Web::Hippie, Web::Hippie::Pipe
AnyEvent
• event-driven programming, painless
• compatible with many other event loops
  use AnyEvent::HTTP;

  http_get "http://osdc.tw/",
                sub { print $_[1] };
Let’s try
this in POE
sub POE::Kernel::ASSERT_DEFAULT () { 1 }

use HTTP::Request;
use POE qw(Component::Client::HTTP);

POE::Component::Client::HTTP->spawn(
  Alias     => 'ua',                   # defaults to 'weeble'
  Timeout   => 20,                     # defaults to 180 seconds
);

POE::Session->create(
  inline_states => {
    _start => sub {
      POE::Kernel->post(
        'ua',         # posts to the 'ua' alias
        'request',    # posts to ua's 'request' state
        'response', # which of our states will receive the response
        HTTP::Request->new(GET => “http://osdc.tw”),
      );
    },
    _stop => sub {},
    response => &response_handler,
  },
);

POE::Kernel->run();
exit;

sub response_handler {
  my ($request_packet, $response_packet) = @_[ARG0, ARG1];
  my $request_object = $request_packet->[0];
  my $response_object = $response_packet->[0];
}
☹
use AnyEvent::HTTP;

http_get "http://osdc.tw/",
              sub { print $_[1] };




      ☺
AnyEvent::*
  AnyEvent-AIO AnyEvent-APNS AnyEvent-Atom-Stream AnyEvent-BDB AnyEvent-
  Beanstalk AnyEvent-Connection AnyEvent-CouchDB AnyEvent-DBI AnyEvent-DBI-
 Abstract AnyEvent-EditText AnyEvent-FCGI AnyEvent-FCP AnyEvent-FIFO AnyEvent-
   FastPing AnyEvent-Feed AnyEvent-Filesys-Notify AnyEvent-FriendFeed-Realtime
    AnyEvent-GPSD AnyEvent-Gearman AnyEvent-Gearman AnyEvent-Gmail-Feed
AnyEvent-HTTP AnyEvent-HTTP-MXHR AnyEvent-HTTPD AnyEvent-I3 AnyEvent-IRC
 AnyEvent-JSONRPC-Lite AnyEvent-Kanye AnyEvent-MP AnyEvent-MPRPC AnyEvent-
 Memcached AnyEvent-Mojo AnyEvent-Monitor-CPU AnyEvent-Pcap AnyEvent-Plurk
    AnyEvent-Postfix-Logs AnyEvent-RTPG AnyEvent-Redis AnyEvent-RetryTimer
AnyEvent-ReverseHTTP AnyEvent-Riak AnyEvent-Run AnyEvent-SCGI AnyEvent-SMTP
    AnyEvent-SNMP AnyEvent-Subprocess AnyEvent-Superfeedr AnyEvent-Twitter
AnyEvent-Twitter-Stream AnyEvent-Watchdog AnyEvent-Worker AnyEvent-XMLRPC
                         AnyEvent-XMPP AnyEvent-mDNS
AnyMQ
• Inspired by Tatsumaki::MessageQueue
• Refactored to support timeout handler,
  multiple subscription
• Support binding to other message queues
  using moose traits
• Available on CPAN and github
AnyMQ

my $bus = AnyMQ->new;
my $topic = $bus->topic("Foo");

my $sub = $bus->new_listener($topic);
$sub->poll(sub { my $msg = shift; })

$topic->publish($msg)
AnyMQ with AMQP
my $bus = AnyMQ->new_with_traits
          ( traits => [ ‘AMQP’],
            # host => ..., port => ..);
my $topic = $bus->topic("Foo");

my $sub = $bus->new_listener($topic);
$sub->poll(sub { my $msg = shift; })

$topic->publish($msg)
Please help adding
  message queue
     bindings!
• PSGI, Plack
• AnyEvent, AnyMQ, AMQP
• Server-push, comet
• Websocket
• Web::Hippie, Web::Hippie::Pipe
Comet
The word comet came to English by way of the Latin word cometes. This word, in turn, came
from the Greek word κόμη, which means "hair of the head". The Greek scientist and
philosopher Aristotlefirst used the derived form of κόμη, κομήτης, to describe what he saw
as "stars with hair." Theastronomical symbol for comets is (☄), consisting of a small disc with
three hairlike extensions.




• 2006, coined by Alex Russell
• Server push for real time notification
Widely used

• gmail
• plurk
• facebook updates
Lots of hacks!

• multi-part XHR
• forever iframe, with script callbacks
• spinning “loading” indicator for FF and IE
• number of connections limits
• PSGI, Plack
• AnyEvent, AnyMQ, AMQP
• Server-push, comet
• Websocket
• Web::Hippie, Web::Hippie::Pipe
Websocket
• HTML5 Websocket
• Bidirectional communication
ws = new WebSocket("ws://foo.com:5000”));
ws.onmessage = function(ev) { ... }
ws.send(....);

• Available in latest Chrome / Safari
Putting
Everything
 Together
• PSGI, Plack
• AnyEvent, AnyMQ, AMQP
• Server-push, comet
• Websocket
• Web::Hippie, Web::Hippie::Pipe
The Long
Hair Web
Hippie
Hippie
• Abstracts persistent connections:
  Websocket, MXHR
                         Just a normal PSGI-app!
 enable "+Web::Hippie";
 sub { my $env    = shift;
       my $args   = $env->{'hippie.args'};
       my $handle = $env->{'hippie.handle'};

        # Your handler based on PATH_INFO:
        # /init, /error, /message
 }
Only
Websocket
and mxhr?
Maybe Hippies
  should be
more relaxed...
To relax a
hippie, we
 need.....
Hippie::Pipe


     +
Hippie::Pipe
• Abstracts persistent bidirectional
  connections: Websocket, MXHR, poll
 enable "+Web::Hippie";
 enable "+Web::Hippie::Pipe";
 sub { my $env    = shift;
       my $sub = $env->{'hippie.listener'};
       my $bus = $env->{'hippie.bus'};
       my $msg = $env->{'hippie.message'};
       # Your handler based on PATH_INFO:
       # /init, /error, /message
 }
Client side
hpipe = new Hippie.Pipe();
$(hpipe)
    .bind(“ready”, function() {
         hpipe.send({type:    ‘chat.join’,
                     channel: ‘foo’,
                     ident:   ‘clkao’})
     })
    .bind(“disconnected”, function() {})
    .bind(“message.chat.message”,
           function(e, data) {});
hpipe.init();
DEMO
Thank you!

Contenu connexe

Tendances

A reviravolta do desenvolvimento web
A reviravolta do desenvolvimento webA reviravolta do desenvolvimento web
A reviravolta do desenvolvimento webWallace Reis
 
Asynchronous programming patterns in Perl
Asynchronous programming patterns in PerlAsynchronous programming patterns in Perl
Asynchronous programming patterns in Perldeepfountainconsulting
 
Building a desktop app with HTTP::Engine, SQLite and jQuery
Building a desktop app with HTTP::Engine, SQLite and jQueryBuilding a desktop app with HTTP::Engine, SQLite and jQuery
Building a desktop app with HTTP::Engine, SQLite and jQueryTatsuhiko Miyagawa
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and othersYusuke Wada
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworksdiego_k
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteBram Vogelaar
 
Introduction To Moco
Introduction To MocoIntroduction To Moco
Introduction To MocoNaoya Ito
 
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-ThonApache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-ThonMasahiro Nagano
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Anatoly Sharifulin
 
Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101hendrikvb
 
Puppet and the HashiStack
Puppet and the HashiStackPuppet and the HashiStack
Puppet and the HashiStackBram Vogelaar
 
Keeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro frameworkKeeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro frameworkJeremy Kendall
 

Tendances (20)

Intro to PSGI and Plack
Intro to PSGI and PlackIntro to PSGI and Plack
Intro to PSGI and Plack
 
A reviravolta do desenvolvimento web
A reviravolta do desenvolvimento webA reviravolta do desenvolvimento web
A reviravolta do desenvolvimento web
 
Asynchronous programming patterns in Perl
Asynchronous programming patterns in PerlAsynchronous programming patterns in Perl
Asynchronous programming patterns in Perl
 
Plack at YAPC::NA 2010
Plack at YAPC::NA 2010Plack at YAPC::NA 2010
Plack at YAPC::NA 2010
 
Building a desktop app with HTTP::Engine, SQLite and jQuery
Building a desktop app with HTTP::Engine, SQLite and jQueryBuilding a desktop app with HTTP::Engine, SQLite and jQuery
Building a desktop app with HTTP::Engine, SQLite and jQuery
 
Plack at OSCON 2010
Plack at OSCON 2010Plack at OSCON 2010
Plack at OSCON 2010
 
PSGI/Plack OSDC.TW
PSGI/Plack OSDC.TWPSGI/Plack OSDC.TW
PSGI/Plack OSDC.TW
 
Mojo as a_client
Mojo as a_clientMojo as a_client
Mojo as a_client
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suite
 
Lies, Damn Lies, and Benchmarks
Lies, Damn Lies, and BenchmarksLies, Damn Lies, and Benchmarks
Lies, Damn Lies, and Benchmarks
 
Introduction To Moco
Introduction To MocoIntroduction To Moco
Introduction To Moco
 
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-ThonApache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
 
Developing apps using Perl
Developing apps using PerlDeveloping apps using Perl
Developing apps using Perl
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
 
Follow the White Rabbit - Message Queues with PHP
Follow the White Rabbit - Message Queues with PHPFollow the White Rabbit - Message Queues with PHP
Follow the White Rabbit - Message Queues with PHP
 
Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101
 
Puppet and the HashiStack
Puppet and the HashiStackPuppet and the HashiStack
Puppet and the HashiStack
 
Keeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro frameworkKeeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro framework
 

En vedette

Web frameworks don't matter
Web frameworks don't matterWeb frameworks don't matter
Web frameworks don't matterTomas Doran
 
Deploying Plack Web Applications: OSCON 2011
Deploying Plack Web Applications: OSCON 2011Deploying Plack Web Applications: OSCON 2011
Deploying Plack Web Applications: OSCON 2011Tatsuhiko Miyagawa
 
iPhone Video Tools
iPhone Video ToolsiPhone Video Tools
iPhone Video ToolsChris Snider
 
Perl RDBMS Programming(DBI/DBIx::Sunnyのはなし)
Perl RDBMS Programming(DBI/DBIx::Sunnyのはなし)Perl RDBMS Programming(DBI/DBIx::Sunnyのはなし)
Perl RDBMS Programming(DBI/DBIx::Sunnyのはなし)karupanerura
 
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LTWeb Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LTMasahiro Nagano
 

En vedette (7)

Web frameworks don't matter
Web frameworks don't matterWeb frameworks don't matter
Web frameworks don't matter
 
Deploying Plack Web Applications: OSCON 2011
Deploying Plack Web Applications: OSCON 2011Deploying Plack Web Applications: OSCON 2011
Deploying Plack Web Applications: OSCON 2011
 
Perl Vogue
Perl VoguePerl Vogue
Perl Vogue
 
Teng tips
Teng tipsTeng tips
Teng tips
 
iPhone Video Tools
iPhone Video ToolsiPhone Video Tools
iPhone Video Tools
 
Perl RDBMS Programming(DBI/DBIx::Sunnyのはなし)
Perl RDBMS Programming(DBI/DBIx::Sunnyのはなし)Perl RDBMS Programming(DBI/DBIx::Sunnyのはなし)
Perl RDBMS Programming(DBI/DBIx::Sunnyのはなし)
 
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LTWeb Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
 

Similaire à AnyMQ, Hippie, and the real-time web

AnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webAnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webclkao
 
How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server Masahiro Nagano
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rackdanwrong
 
Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門lestrrat
 
Plack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and serversPlack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and serversTatsuhiko Miyagawa
 
Real-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.ioReal-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.ioRick Copeland
 
MeshU Thin & Rack
MeshU Thin & RackMeshU Thin & Rack
MeshU Thin & Rackguestbac5dc
 
VUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareVUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareCosimo Streppone
 
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine YardHow I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine YardSV Ruby on Rails Meetup
 
Implementing Comet using PHP
Implementing Comet using PHPImplementing Comet using PHP
Implementing Comet using PHPKing Foo
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingAsynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingSteve Rhoades
 
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)Wesley Beary
 
fog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the Cloudfog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the CloudWesley Beary
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统yiditushe
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareAlona Mekhovova
 

Similaire à AnyMQ, Hippie, and the real-time web (20)

AnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webAnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time web
 
How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rack
 
Rack
RackRack
Rack
 
Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門
 
Plack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and serversPlack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and servers
 
Blog Hacks 2011
Blog Hacks 2011Blog Hacks 2011
Blog Hacks 2011
 
Network programming
Network programmingNetwork programming
Network programming
 
Real-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.ioReal-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.io
 
MeshU Thin & Rack
MeshU Thin & RackMeshU Thin & Rack
MeshU Thin & Rack
 
VUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareVUG5: Varnish at Opera Software
VUG5: Varnish at Opera Software
 
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine YardHow I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
 
Implementing Comet using PHP
Implementing Comet using PHPImplementing Comet using PHP
Implementing Comet using PHP
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingAsynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time Messaging
 
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
 
fog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the Cloudfog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the Cloud
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
Server Side Swift: Vapor
Server Side Swift: VaporServer Side Swift: Vapor
Server Side Swift: Vapor
 
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middleware
 

Plus de clkao

Open Source, Open Data, Open Government
Open Source, Open Data, Open GovernmentOpen Source, Open Data, Open Government
Open Source, Open Data, Open Governmentclkao
 
Trading with opensource tools, two years later
Trading with opensource tools, two years laterTrading with opensource tools, two years later
Trading with opensource tools, two years laterclkao
 
Websockets at tossug
Websockets at tossugWebsockets at tossug
Websockets at tossugclkao
 
"Lego Programming" with Lorzy
"Lego Programming" with Lorzy"Lego Programming" with Lorzy
"Lego Programming" with Lorzyclkao
 
Devel::NYTProf
Devel::NYTProfDevel::NYTProf
Devel::NYTProfclkao
 
Trading With Open Source Tools
Trading With Open Source ToolsTrading With Open Source Tools
Trading With Open Source Toolsclkao
 
Twopenhack08 Fnord
Twopenhack08 FnordTwopenhack08 Fnord
Twopenhack08 Fnordclkao
 
Svk Br Yapceu2008
Svk Br Yapceu2008Svk Br Yapceu2008
Svk Br Yapceu2008clkao
 
prototype::signatures
prototype::signaturesprototype::signatures
prototype::signaturesclkao
 
Leon & Andrea
Leon & AndreaLeon & Andrea
Leon & Andreaclkao
 

Plus de clkao (10)

Open Source, Open Data, Open Government
Open Source, Open Data, Open GovernmentOpen Source, Open Data, Open Government
Open Source, Open Data, Open Government
 
Trading with opensource tools, two years later
Trading with opensource tools, two years laterTrading with opensource tools, two years later
Trading with opensource tools, two years later
 
Websockets at tossug
Websockets at tossugWebsockets at tossug
Websockets at tossug
 
"Lego Programming" with Lorzy
"Lego Programming" with Lorzy"Lego Programming" with Lorzy
"Lego Programming" with Lorzy
 
Devel::NYTProf
Devel::NYTProfDevel::NYTProf
Devel::NYTProf
 
Trading With Open Source Tools
Trading With Open Source ToolsTrading With Open Source Tools
Trading With Open Source Tools
 
Twopenhack08 Fnord
Twopenhack08 FnordTwopenhack08 Fnord
Twopenhack08 Fnord
 
Svk Br Yapceu2008
Svk Br Yapceu2008Svk Br Yapceu2008
Svk Br Yapceu2008
 
prototype::signatures
prototype::signaturesprototype::signatures
prototype::signatures
 
Leon & Andrea
Leon & AndreaLeon & Andrea
Leon & Andrea
 

AnyMQ, Hippie, and the real-time web

  • 1. AnyMQ, Hippie and the real-time web OSDC.TW 2010 Taipei Chia-liang Kao clkao@clkao.org
  • 5. • PSGI, Plack • AnyEvent, AnyMQ, AMQP • Server-push, comet • Websocket • Web::Hippie, Web::Hippie:Pipe
  • 6. • PSGI, Plack • AnyEvent, AnyMQ, AMQP • Server-push, comet • Websocket • Web::Hippie, Web::Hippie::Pipe
  • 7. Plack: Perl Web Superglue
  • 9. $env->{PATH_INFO} Plack::Request->new($env)->path_info $env [ '200', [ 'Content-Type' => 'text/plain' ], [ "Hello World" ], ]
  • 10. $env Streaming Interface sub { my $responder = shift; my $writer = $responder->([ '200', [ 'Content-Type' => 'text/plain' ]); # later, of in a callback $writer->write(“Hello world!”); $writer->close(); }
  • 12. • PSGI, Plack • AnyEvent, AnyMQ, AMQP • Server-push, comet • Websocket • Web::Hippie, Web::Hippie::Pipe
  • 13. AnyEvent • event-driven programming, painless • compatible with many other event loops use AnyEvent::HTTP; http_get "http://osdc.tw/", sub { print $_[1] };
  • 15. sub POE::Kernel::ASSERT_DEFAULT () { 1 } use HTTP::Request; use POE qw(Component::Client::HTTP); POE::Component::Client::HTTP->spawn(   Alias => 'ua', # defaults to 'weeble'   Timeout => 20, # defaults to 180 seconds ); POE::Session->create(   inline_states => {     _start => sub {       POE::Kernel->post(         'ua', # posts to the 'ua' alias         'request', # posts to ua's 'request' state         'response', # which of our states will receive the response         HTTP::Request->new(GET => “http://osdc.tw”),       );     },     _stop => sub {},     response => &response_handler,   }, ); POE::Kernel->run(); exit; sub response_handler {   my ($request_packet, $response_packet) = @_[ARG0, ARG1];   my $request_object = $request_packet->[0];   my $response_object = $response_packet->[0]; }
  • 16.
  • 18. AnyEvent::* AnyEvent-AIO AnyEvent-APNS AnyEvent-Atom-Stream AnyEvent-BDB AnyEvent- Beanstalk AnyEvent-Connection AnyEvent-CouchDB AnyEvent-DBI AnyEvent-DBI- Abstract AnyEvent-EditText AnyEvent-FCGI AnyEvent-FCP AnyEvent-FIFO AnyEvent- FastPing AnyEvent-Feed AnyEvent-Filesys-Notify AnyEvent-FriendFeed-Realtime AnyEvent-GPSD AnyEvent-Gearman AnyEvent-Gearman AnyEvent-Gmail-Feed AnyEvent-HTTP AnyEvent-HTTP-MXHR AnyEvent-HTTPD AnyEvent-I3 AnyEvent-IRC AnyEvent-JSONRPC-Lite AnyEvent-Kanye AnyEvent-MP AnyEvent-MPRPC AnyEvent- Memcached AnyEvent-Mojo AnyEvent-Monitor-CPU AnyEvent-Pcap AnyEvent-Plurk AnyEvent-Postfix-Logs AnyEvent-RTPG AnyEvent-Redis AnyEvent-RetryTimer AnyEvent-ReverseHTTP AnyEvent-Riak AnyEvent-Run AnyEvent-SCGI AnyEvent-SMTP AnyEvent-SNMP AnyEvent-Subprocess AnyEvent-Superfeedr AnyEvent-Twitter AnyEvent-Twitter-Stream AnyEvent-Watchdog AnyEvent-Worker AnyEvent-XMLRPC AnyEvent-XMPP AnyEvent-mDNS
  • 19. AnyMQ • Inspired by Tatsumaki::MessageQueue • Refactored to support timeout handler, multiple subscription • Support binding to other message queues using moose traits • Available on CPAN and github
  • 20. AnyMQ my $bus = AnyMQ->new; my $topic = $bus->topic("Foo"); my $sub = $bus->new_listener($topic); $sub->poll(sub { my $msg = shift; }) $topic->publish($msg)
  • 21. AnyMQ with AMQP my $bus = AnyMQ->new_with_traits ( traits => [ ‘AMQP’], # host => ..., port => ..); my $topic = $bus->topic("Foo"); my $sub = $bus->new_listener($topic); $sub->poll(sub { my $msg = shift; }) $topic->publish($msg)
  • 22. Please help adding message queue bindings!
  • 23. • PSGI, Plack • AnyEvent, AnyMQ, AMQP • Server-push, comet • Websocket • Web::Hippie, Web::Hippie::Pipe
  • 24. Comet The word comet came to English by way of the Latin word cometes. This word, in turn, came from the Greek word κόμη, which means "hair of the head". The Greek scientist and philosopher Aristotlefirst used the derived form of κόμη, κομήτης, to describe what he saw as "stars with hair." Theastronomical symbol for comets is (☄), consisting of a small disc with three hairlike extensions. • 2006, coined by Alex Russell • Server push for real time notification
  • 25. Widely used • gmail • plurk • facebook updates
  • 26. Lots of hacks! • multi-part XHR • forever iframe, with script callbacks • spinning “loading” indicator for FF and IE • number of connections limits
  • 27. • PSGI, Plack • AnyEvent, AnyMQ, AMQP • Server-push, comet • Websocket • Web::Hippie, Web::Hippie::Pipe
  • 28. Websocket • HTML5 Websocket • Bidirectional communication ws = new WebSocket("ws://foo.com:5000”)); ws.onmessage = function(ev) { ... } ws.send(....); • Available in latest Chrome / Safari
  • 30. • PSGI, Plack • AnyEvent, AnyMQ, AMQP • Server-push, comet • Websocket • Web::Hippie, Web::Hippie::Pipe
  • 33. Hippie • Abstracts persistent connections: Websocket, MXHR Just a normal PSGI-app! enable "+Web::Hippie"; sub { my $env = shift; my $args = $env->{'hippie.args'}; my $handle = $env->{'hippie.handle'}; # Your handler based on PATH_INFO: # /init, /error, /message }
  • 35. Maybe Hippies should be more relaxed...
  • 36. To relax a hippie, we need.....
  • 38. Hippie::Pipe • Abstracts persistent bidirectional connections: Websocket, MXHR, poll enable "+Web::Hippie"; enable "+Web::Hippie::Pipe"; sub { my $env = shift; my $sub = $env->{'hippie.listener'}; my $bus = $env->{'hippie.bus'}; my $msg = $env->{'hippie.message'}; # Your handler based on PATH_INFO: # /init, /error, /message }
  • 39. Client side hpipe = new Hippie.Pipe(); $(hpipe) .bind(“ready”, function() { hpipe.send({type: ‘chat.join’, channel: ‘foo’, ident: ‘clkao’}) }) .bind(“disconnected”, function() {}) .bind(“message.chat.message”, function(e, data) {}); hpipe.init();
  • 40. DEMO