SlideShare a Scribd company logo
1 of 36
Download to read offline
messaging
made simple
Ian Barber
http://phpir.com - http://zero.mq/ib
ianb@php.net - @ianbarber
“0MQ is
unbelievably cool
– if you haven’t
got a project that
needs it, make
one up”
jon gifford - loggly
queue        esb


pipeline    async



pub/sub    gateway
request/response
run() ->                       erlrep.erl
 {ok, Ctx} = erlzmq:context(),
 {ok, Sock} = erlzmq:socket(Ctx, rep),
 ok = erlzmq:bind(Sock,"tcp://*:5454"),
 loop(Sock).

loop(Sock) ->
 {ok, Msg, _F} = erlzmq:recv(Sock),
 Rep = binary_to_list(Msg) ++ " World",
 io:format("Sending ~s~n", [Rep]),
 ok = erlzmq:send(Sock,
              list_to_binary(Rep)),
 loop(Sock).
import zmq                   rep.py
context = zmq.Context()
server = context.socket(zmq.REP)
server.bind("tcp://*:5454")

while True:
    message = server.recv()
    print "Sending", message, "Worldn"
    server.send(message + " World")
request/response

$ctx = new ZMQContext();      req.php
$req =
  new ZMQSocket($ctx, ZMQ::SOCKET_REQ);
$req->connect("tcp://localhost:5454");

$req->send("Hello");
echo $req->recv();
git clone http://github.com/zeromq/erlzmq2.git

make
make test



   http://download.zeromq.org/
   http://github.com/zeromq/zeromq2-1
   http://github.com/zeromq/libzmq
atomic   bytes   multipart



messaging
Post Office Image: http://www.flickr.com/photos/10804218@N00/4315282973
        Post Box Image: http://www.flickr.com/photos/kenjonbro/3027166169
queue
queue
run() ->
 {ok, Ctx} = erlzmq:context(),
 {ok, Front} = erlzmq:socket(Ctx,
                 [xrep, {active,true}]),
 {ok, Back} = erlzmq:socket(Ctx,
                 [xreq, {active,true}]),
 ok = erlzmq:bind(Front, "tcp://*:5454"),
 ok = erlzmq:bind(Back, "tcp://*:5455"),

loop(Front, Back),
ok = erlzmq:close(Front),
ok = erlzmq:close(Back),
                                  que ue.erl
ok = erlzmq:term(Ctx).
loop(Front, Back) ->
 receive
  {zmq, Front, Msg, Flags} ->
    io:format("Sending Back: ~p~n",[Msg]),
    sendall(Back, Msg, Flags),
    loop(Front, Back);
  {zmq, Back, Msg, Flags} ->
    io:format("Sending Front: ~p~n",[Msg]),
    sendall(Front, Msg, Flags),
    loop(Front, Back)
 end.

sendall(To, Part,   [rcvmore|_Flags]) ->
  erlzmq:send(To,   Part, [sndmore]);
sendall(To, Part,   _Flags) ->
  erlzmq:send(To,   Part).
stable / unstable




  Image: http://www.flickr.com/photos/pelican/235461339/
pipeline
logging
processes




                local log
               ag gregators



  log writer
pipeline
<?php                          logger.php
$ctx = new ZMQContext();
$out = $ctx->getSocket(ZMQ::SOCKET_PUSH);
$out->connect("ipc:///tmp/logger");
$msg = array("time" => time());
$msg['msg'] = $_SERVER['argv'][1];
$out->send(json_encode($msg));
loglocal.py
import zmq; bufSz = 3; msgs = []
ctx = zmq.Context()
inp = ctx.socket(zmq.PULL)
out = ctx.socket(zmq.PUSH)
inp.bind("ipc:///tmp/logger")
out.connect("tcp://localhost:5555")

while True:
 msgs.append(inp.recv())
 print "Received Log"
 if(len(msgs) == bufSz):
   print "Forwarding Buffer"
   for i, msg in enumerate(msgs):
    out.send( msg,
              (0,zmq.SNDMORE)[i< bufSz-1])
    msgs = []
logserv.erl
run() ->
 {ok, Ctx} = erlzmq:context(),
 {ok, In} = erlzmq:socket(Ctx,
                   [pull, {active,true}]),
 ok = erlzmq:bind(In, "tcp://*:5555"),
 loop(In).

loop(In) ->
 receive
  {zmq, In, Msg, _F} ->
  {ok,{obj, [{_, Time}, {_, Log}] }, [] }
   = rfc4627:decode(Msg),
  io:format("~B ~s~n", [Time, Log]),
  loop(In)
 end.
pub/sub




  Image: http://www.flickr.com/photos/nikonvscanon/4519133003/
pub/sub
pub/sub
                              repeater.e
                                        rl
init(_) ->
  spawn_link(?MODULE, run, []).

terminate(Pid) ->
  Pid ! {cmd, kill}.

handle_event(Event, Pid) ->
  Pid ! Event.
run() ->
  {ok, Ctx} = erlzmq:context(),
  {ok, Sock} = erlzmq:socket(Ctx, [pub]),
  ok = erlzmq:bind(Sock, "tcp://*:5656"),
  loop(Sock).

loop(Sock) ->
  receive
    {cmd, kill} ->
      ok;
    {Action, Id, Event} ->
      Json = rfc4627:encode({obj,
              [{action, Action},
               {id, Id}, {event, Event}]}),
      erlzmq:send(Sock,
                     list_to_binary(Json)),
      loop(Sock)
  end.
types of transport

     inproc         ipc




   tcp        pgm
distro   sub   web
client
                   sub   web
         sub

event
         sub       sub   web
 pub

          distro   sub   web
client
         sub       db
import zmq
from zmq.devices.basedevice
     import ProcessDevice

pd = ProcessDevice(zmq.STREAMER,
                   zmq.PULL, zmq.PUB)
pd.bind_in("tcp://*:6767")
pd.connect_out("epgm://eth0;239.192.0.1:7601")
pd.setsockopt_out(zmq.RATE, 10000)
pd.start()

# Do other things here


                         eventhub.php
int main(void) {
  void *ctx = zmq_init(1);
  void *in = zmq_socket(ctx, ZMQ_SUB);
  void *out = zmq_socket(ctx, ZMQ_PUB);
  zmq_setsockopt(in, ZMQ_SUBSCRIBE, "", 0);
  int rcin = zmq_connect(in,
              "epgm://;239.192.0.1:7601");
  int rcout = zmq_bind(out,
              "ipc:///tmp/events");
  int rcd =   zmq_device(ZMQ_FORWARDER,
              in, out);

    zmq_close(in); zmq_close(out);
    zmq_term(ctx);
    return 0;
}                             distro.c
$ctx = new ZMQContext();
$in = $ctx->getSocket(ZMQ::SOCKET_SUB);
for($i = 0; $i<100; $i++) {
   $in->setSockOpt(
           ZMQ::SOCKOPT_SUBSCRIBE,
           rand(100000, 999999));
}
$in->connect("ipc:///tmp/events");
$i = 0;
while($i++ < 1000) {
  $who = $in->recv();
  $msg = $in->recv();
  printf("%s %s %s", $who, $msg, PHP_EOL);
}
                                cli ent.php
amqp /= ømq



http://zero.mq/amqp
[{r0mq,
  [{services,
    [
{<<"PIPELINE">>, pull, "tcp://127.0.0.1:5557"},
{<<"PIPELINE">>, push, "tcp://127.0.0.1:5558"},
{<<"PUBSUB">>, pub, "tcp://127.0.0.1:5555"},
{<<"PUBSUB">>, sub, "tcp://127.0.0.1:5556"}
     ]
  }]
}].

                          rabbitmq.config
Helpful Links
http://zero.mq
        Ian Barber
http://zguide.zero.mq
        http://phpir.com
http://zero.mq/ib
        ian.barber@gmail.com @ianbarber




      thanks!

More Related Content

What's hot

The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHPWim Godden
 
React PHP: the NodeJS challenger
React PHP: the NodeJS challengerReact PHP: the NodeJS challenger
React PHP: the NodeJS challengervanphp
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingAsynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingSteve Rhoades
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & ToolsIan Barber
 
Yy
YyYy
Yyyygh
 
Nouveau document texte
Nouveau document texteNouveau document texte
Nouveau document texteSai Ef
 
PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges✅ William Pinaud
 
Any event intro
Any event introAny event intro
Any event introqiang
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous phpWim Godden
 
Assignment no39
Assignment no39Assignment no39
Assignment no39Jay Patel
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyYasuharu Nakano
 
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 TokyoIntroduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 TokyoMasahiro Nagano
 
Controlling Arduino With PHP
Controlling Arduino With PHPControlling Arduino With PHP
Controlling Arduino With PHPThomas Weinert
 
Modern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlModern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlNova Patch
 

What's hot (19)

The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
React PHP: the NodeJS challenger
React PHP: the NodeJS challengerReact PHP: the NodeJS challenger
React PHP: the NodeJS challenger
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingAsynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time Messaging
 
Yg byev2e
Yg byev2eYg byev2e
Yg byev2e
 
C99.php
C99.phpC99.php
C99.php
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & Tools
 
Yy
YyYy
Yy
 
Nouveau document texte
Nouveau document texteNouveau document texte
Nouveau document texte
 
C99[2]
C99[2]C99[2]
C99[2]
 
dotCloud and go
dotCloud and godotCloud and go
dotCloud and go
 
PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges
 
C99
C99C99
C99
 
Any event intro
Any event introAny event intro
Any event intro
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
Assignment no39
Assignment no39Assignment no39
Assignment no39
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
 
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 TokyoIntroduction to CloudForecast / YAPC::Asia 2010 Tokyo
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
 
Controlling Arduino With PHP
Controlling Arduino With PHPControlling Arduino With PHP
Controlling Arduino With PHP
 
Modern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlModern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in Perl
 

Similar to ZeroMQ: Messaging Made Simple

Zeromq - Pycon India 2013
Zeromq - Pycon India 2013Zeromq - Pycon India 2013
Zeromq - Pycon India 2013Srinivasan R
 
Python queue solution with asyncio and kafka
Python queue solution with asyncio and kafkaPython queue solution with asyncio and kafka
Python queue solution with asyncio and kafkaOndřej Veselý
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
A deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio moduleA deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio moduleSaúl Ibarra Corretgé
 
Lego: A brick system build by scala
Lego: A brick system build by scalaLego: A brick system build by scala
Lego: A brick system build by scalalunfu zhong
 
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」matuura_core
 
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
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in GolangBo-Yi Wu
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency명신 김
 
start_printf: dev/ic/com.c comstart()
start_printf: dev/ic/com.c comstart()start_printf: dev/ic/com.c comstart()
start_printf: dev/ic/com.c comstart()Kiwamu Okabe
 
Encrypt all transports
Encrypt all transportsEncrypt all transports
Encrypt all transportsEleanor McHugh
 
Goroutines and Channels in practice
Goroutines and Channels in practiceGoroutines and Channels in practice
Goroutines and Channels in practiceGuilherme Garnier
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomyDongmin Yu
 
Distributed Computing Patterns in R
Distributed Computing Patterns in RDistributed Computing Patterns in R
Distributed Computing Patterns in Rarmstrtw
 
TDC2018SP | Trilha Kotlin - Programacao assincrona utilizando Coroutines
TDC2018SP | Trilha Kotlin - Programacao assincrona utilizando CoroutinesTDC2018SP | Trilha Kotlin - Programacao assincrona utilizando Coroutines
TDC2018SP | Trilha Kotlin - Programacao assincrona utilizando Coroutinestdc-globalcode
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaFrank Lyaruu
 

Similar to ZeroMQ: Messaging Made Simple (20)

Zeromq - Pycon India 2013
Zeromq - Pycon India 2013Zeromq - Pycon India 2013
Zeromq - Pycon India 2013
 
Python queue solution with asyncio and kafka
Python queue solution with asyncio and kafkaPython queue solution with asyncio and kafka
Python queue solution with asyncio and kafka
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
A deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio moduleA deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio module
 
Lego: A brick system build by scala
Lego: A brick system build by scalaLego: A brick system build by scala
Lego: A brick system build by scala
 
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」
 
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
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in Golang
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
start_printf: dev/ic/com.c comstart()
start_printf: dev/ic/com.c comstart()start_printf: dev/ic/com.c comstart()
start_printf: dev/ic/com.c comstart()
 
Npc14
Npc14Npc14
Npc14
 
Encrypt all transports
Encrypt all transportsEncrypt all transports
Encrypt all transports
 
Ssaw08 0624
Ssaw08 0624Ssaw08 0624
Ssaw08 0624
 
Goroutines and Channels in practice
Goroutines and Channels in practiceGoroutines and Channels in practice
Goroutines and Channels in practice
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomy
 
Distributed Computing Patterns in R
Distributed Computing Patterns in RDistributed Computing Patterns in R
Distributed Computing Patterns in R
 
TDC2018SP | Trilha Kotlin - Programacao assincrona utilizando Coroutines
TDC2018SP | Trilha Kotlin - Programacao assincrona utilizando CoroutinesTDC2018SP | Trilha Kotlin - Programacao assincrona utilizando Coroutines
TDC2018SP | Trilha Kotlin - Programacao assincrona utilizando Coroutines
 
R meets Hadoop
R meets HadoopR meets Hadoop
R meets Hadoop
 
Introduction to asyncio
Introduction to asyncioIntroduction to asyncio
Introduction to asyncio
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJava
 

More from Ian Barber

Deployment Tactics
Deployment TacticsDeployment Tactics
Deployment TacticsIan Barber
 
In Search Of: Integrating Site Search (PHP Barcelona)
In Search Of: Integrating Site Search (PHP Barcelona)In Search Of: Integrating Site Search (PHP Barcelona)
In Search Of: Integrating Site Search (PHP Barcelona)Ian Barber
 
In Search Of... (Dutch PHP Conference 2010)
In Search Of... (Dutch PHP Conference 2010)In Search Of... (Dutch PHP Conference 2010)
In Search Of... (Dutch PHP Conference 2010)Ian Barber
 
In Search Of... integrating site search
In Search Of... integrating site search In Search Of... integrating site search
In Search Of... integrating site search Ian Barber
 
Document Classification In PHP - Slight Return
Document Classification In PHP - Slight ReturnDocument Classification In PHP - Slight Return
Document Classification In PHP - Slight ReturnIan Barber
 
Document Classification In PHP
Document Classification In PHPDocument Classification In PHP
Document Classification In PHPIan Barber
 

More from Ian Barber (6)

Deployment Tactics
Deployment TacticsDeployment Tactics
Deployment Tactics
 
In Search Of: Integrating Site Search (PHP Barcelona)
In Search Of: Integrating Site Search (PHP Barcelona)In Search Of: Integrating Site Search (PHP Barcelona)
In Search Of: Integrating Site Search (PHP Barcelona)
 
In Search Of... (Dutch PHP Conference 2010)
In Search Of... (Dutch PHP Conference 2010)In Search Of... (Dutch PHP Conference 2010)
In Search Of... (Dutch PHP Conference 2010)
 
In Search Of... integrating site search
In Search Of... integrating site search In Search Of... integrating site search
In Search Of... integrating site search
 
Document Classification In PHP - Slight Return
Document Classification In PHP - Slight ReturnDocument Classification In PHP - Slight Return
Document Classification In PHP - Slight Return
 
Document Classification In PHP
Document Classification In PHPDocument Classification In PHP
Document Classification In PHP
 

Recently uploaded

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 

Recently uploaded (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

ZeroMQ: Messaging Made Simple

  • 1. messaging made simple Ian Barber http://phpir.com - http://zero.mq/ib ianb@php.net - @ianbarber
  • 2. “0MQ is unbelievably cool – if you haven’t got a project that needs it, make one up” jon gifford - loggly
  • 3.
  • 4.
  • 5.
  • 6. queue esb pipeline async pub/sub gateway
  • 7. request/response run() -> erlrep.erl {ok, Ctx} = erlzmq:context(), {ok, Sock} = erlzmq:socket(Ctx, rep), ok = erlzmq:bind(Sock,"tcp://*:5454"), loop(Sock). loop(Sock) -> {ok, Msg, _F} = erlzmq:recv(Sock), Rep = binary_to_list(Msg) ++ " World", io:format("Sending ~s~n", [Rep]), ok = erlzmq:send(Sock, list_to_binary(Rep)), loop(Sock).
  • 8. import zmq rep.py context = zmq.Context() server = context.socket(zmq.REP) server.bind("tcp://*:5454") while True: message = server.recv() print "Sending", message, "Worldn" server.send(message + " World")
  • 9. request/response $ctx = new ZMQContext(); req.php $req = new ZMQSocket($ctx, ZMQ::SOCKET_REQ); $req->connect("tcp://localhost:5454"); $req->send("Hello"); echo $req->recv();
  • 10.
  • 11. git clone http://github.com/zeromq/erlzmq2.git make make test http://download.zeromq.org/ http://github.com/zeromq/zeromq2-1 http://github.com/zeromq/libzmq
  • 12. atomic bytes multipart messaging
  • 13. Post Office Image: http://www.flickr.com/photos/10804218@N00/4315282973 Post Box Image: http://www.flickr.com/photos/kenjonbro/3027166169
  • 14. queue
  • 15. queue run() -> {ok, Ctx} = erlzmq:context(), {ok, Front} = erlzmq:socket(Ctx, [xrep, {active,true}]), {ok, Back} = erlzmq:socket(Ctx, [xreq, {active,true}]), ok = erlzmq:bind(Front, "tcp://*:5454"), ok = erlzmq:bind(Back, "tcp://*:5455"), loop(Front, Back), ok = erlzmq:close(Front), ok = erlzmq:close(Back), que ue.erl ok = erlzmq:term(Ctx).
  • 16. loop(Front, Back) -> receive {zmq, Front, Msg, Flags} -> io:format("Sending Back: ~p~n",[Msg]), sendall(Back, Msg, Flags), loop(Front, Back); {zmq, Back, Msg, Flags} -> io:format("Sending Front: ~p~n",[Msg]), sendall(Front, Msg, Flags), loop(Front, Back) end. sendall(To, Part, [rcvmore|_Flags]) -> erlzmq:send(To, Part, [sndmore]); sendall(To, Part, _Flags) -> erlzmq:send(To, Part).
  • 17. stable / unstable Image: http://www.flickr.com/photos/pelican/235461339/
  • 19. logging processes local log ag gregators log writer
  • 20. pipeline <?php logger.php $ctx = new ZMQContext(); $out = $ctx->getSocket(ZMQ::SOCKET_PUSH); $out->connect("ipc:///tmp/logger"); $msg = array("time" => time()); $msg['msg'] = $_SERVER['argv'][1]; $out->send(json_encode($msg));
  • 21. loglocal.py import zmq; bufSz = 3; msgs = [] ctx = zmq.Context() inp = ctx.socket(zmq.PULL) out = ctx.socket(zmq.PUSH) inp.bind("ipc:///tmp/logger") out.connect("tcp://localhost:5555") while True: msgs.append(inp.recv()) print "Received Log" if(len(msgs) == bufSz): print "Forwarding Buffer" for i, msg in enumerate(msgs): out.send( msg, (0,zmq.SNDMORE)[i< bufSz-1]) msgs = []
  • 22. logserv.erl run() -> {ok, Ctx} = erlzmq:context(), {ok, In} = erlzmq:socket(Ctx, [pull, {active,true}]), ok = erlzmq:bind(In, "tcp://*:5555"), loop(In). loop(In) -> receive {zmq, In, Msg, _F} -> {ok,{obj, [{_, Time}, {_, Log}] }, [] } = rfc4627:decode(Msg), io:format("~B ~s~n", [Time, Log]), loop(In) end.
  • 23.
  • 24. pub/sub Image: http://www.flickr.com/photos/nikonvscanon/4519133003/
  • 26. pub/sub repeater.e rl init(_) -> spawn_link(?MODULE, run, []). terminate(Pid) -> Pid ! {cmd, kill}. handle_event(Event, Pid) -> Pid ! Event.
  • 27. run() -> {ok, Ctx} = erlzmq:context(), {ok, Sock} = erlzmq:socket(Ctx, [pub]), ok = erlzmq:bind(Sock, "tcp://*:5656"), loop(Sock). loop(Sock) -> receive {cmd, kill} -> ok; {Action, Id, Event} -> Json = rfc4627:encode({obj, [{action, Action}, {id, Id}, {event, Event}]}), erlzmq:send(Sock, list_to_binary(Json)), loop(Sock) end.
  • 28. types of transport inproc ipc tcp pgm
  • 29.
  • 30. distro sub web client sub web sub event sub sub web pub distro sub web client sub db
  • 31. import zmq from zmq.devices.basedevice import ProcessDevice pd = ProcessDevice(zmq.STREAMER, zmq.PULL, zmq.PUB) pd.bind_in("tcp://*:6767") pd.connect_out("epgm://eth0;239.192.0.1:7601") pd.setsockopt_out(zmq.RATE, 10000) pd.start() # Do other things here eventhub.php
  • 32. int main(void) { void *ctx = zmq_init(1); void *in = zmq_socket(ctx, ZMQ_SUB); void *out = zmq_socket(ctx, ZMQ_PUB); zmq_setsockopt(in, ZMQ_SUBSCRIBE, "", 0); int rcin = zmq_connect(in, "epgm://;239.192.0.1:7601"); int rcout = zmq_bind(out, "ipc:///tmp/events"); int rcd = zmq_device(ZMQ_FORWARDER, in, out); zmq_close(in); zmq_close(out); zmq_term(ctx); return 0; } distro.c
  • 33. $ctx = new ZMQContext(); $in = $ctx->getSocket(ZMQ::SOCKET_SUB); for($i = 0; $i<100; $i++) { $in->setSockOpt( ZMQ::SOCKOPT_SUBSCRIBE, rand(100000, 999999)); } $in->connect("ipc:///tmp/events"); $i = 0; while($i++ < 1000) { $who = $in->recv(); $msg = $in->recv(); printf("%s %s %s", $who, $msg, PHP_EOL); } cli ent.php
  • 35. [{r0mq,   [{services,     [ {<<"PIPELINE">>, pull, "tcp://127.0.0.1:5557"}, {<<"PIPELINE">>, push, "tcp://127.0.0.1:5558"}, {<<"PUBSUB">>, pub, "tcp://127.0.0.1:5555"}, {<<"PUBSUB">>, sub, "tcp://127.0.0.1:5556"} ] }] }]. rabbitmq.config
  • 36. Helpful Links http://zero.mq Ian Barber http://zguide.zero.mq http://phpir.com http://zero.mq/ib ian.barber@gmail.com @ianbarber thanks!