Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

nanomsg:
simple
smart
sockets

dirkjan.ochtman.nl


by flickr:ter-burg

TCP/IP

message queues

ØMQ

A little history

pipe: stream of data

queue abstraction

WebSphere -> AMQP

Martin Sústrik had an idea:

a message queue toolkit with sockets API

(ØMQ versus nanomsg)

Patterns & Features

import nnpy

p1 = nnpy.Socket(nnpy.AF_SP, nnpy.PAIR)
p1.bind('inproc://pair')

p2 = nnpy.Socket(nnpy.AF_SP, nnpy.PAIR)
p2.connect('inproc://pair')

p1.send('hello, p2')
p1.send('I am p1')
print p2.recv()
p2.send('nice to meet you')
print p1.recv()
print p2.recv()
hello, p2
nice to meet you
I am p1
diff --git a/pair.py b/pair.py
--- a/pair.py
+++ b/pair.py
@@ -1,10 +1,10 @@
 import nnpy
 
 p1 = nnpy.Socket(nnpy.AF_SP, nnpy.PAIR)
-p1.bind('inproc://pair')
+p1.bind('ipc:///tmp/pair')
 
 p2 = nnpy.Socket(nnpy.AF_SP, nnpy.PAIR)
-p2.connect('inproc://pair')
+p2.connect('ipc:///tmp/pair')
 
 p1.send('hello, p2')
 p1.send('I am p1')
import nnpy
p1 = nnpy.Socket(nnpy.AF_SP, nnpy.PAIR)
p1.bind('ipc:///tmp/pair')
p1.send('hello, p2')
p1.send('I am p1')
print p1.recv()
import nnpy
p2 = nnpy.Socket(nnpy.AF_SP, nnpy.PAIR)
p2.connect('ipc:///tmp/pair')
print p2.recv()
p2.send('nice to meet you')
print p2.recv()
nice to meet you

hello, p2
I am p1
diff --git a/pair.py b/pair.py
--- a/pair.py
+++ b/pair.py
@@ -1,10 +1,10 @@
 import nnpy
 
 p1 = nnpy.Socket(nnpy.AF_SP, nnpy.PAIR)
-p1.bind('inproc://pair')
+p1.bind('tcp://localhost:18913')
 
 p2 = nnpy.Socket(nnpy.AF_SP, nnpy.PAIR)
-p2.connect('inproc://pair')
+p2.connect('tcp://localhost:18913')
 
 p1.send('hello, p2')
 p1.send('I am p1')
import nnpy

rep = nnpy.Socket(nnpy.AF_SP, nnpy.REP)
rep.bind('inproc://foo')

req = nnpy.Socket(nnpy.AF_SP, nnpy.REQ)
req.connect('inproc://foo')

req.send('test!')
print rep.recv()
rep.send('echo!')
print req.recv()
test!
echo!
diff --git a/req-rep.py b/req-rep.py
--- a/req-rep.py
+++ b/req-rep.py
@@ -1,10 +1,10 @@
 import nnpy
 
 rep = nnpy.Socket(nnpy.AF_SP, nnpy.REP)
-rep.bind('inproc://foo')
+rep.connect('inproc://foo')
 
 req = nnpy.Socket(nnpy.AF_SP, nnpy.REQ)
-req.connect('inproc://foo')
+req.bind('inproc://foo')
 
 req.send('test!')
 print rep.recv()
import nnpy

class Server:
    def __init__(self, core):
        self.core = core
    def run(self):
        sock = nnpy.Socket(nnpy.AF_SP, nnpy.REP)
        sock.bind(COMMAND_SERVER)
        while True:
            msg = sock.recv()
            # handle command
            sock.send(rsp)

class Client:
    def __init__(self, addr):
        self.sock = nnpy.Socket(nnpy.AF_SP, nnpy.REQ)
        self.sock.connect(addr)
    def _send(self, msg):
        self.sock.send(msg)
        return self.sock.recv()
import nnpy      

pub = nnpy.Socket(nnpy.AF_SP, nnpy.PUB)
pub.bind('inproc://foo')

sub1 = nnpy.Socket(nnpy.AF_SP, nnpy.SUB)
sub1.connect('inproc://foo')
sub1.setsockopt(nnpy.SUB, nnpy.SUB_SUBSCRIBE, '')

sub2 = nnpy.Socket(nnpy.AF_SP, nnpy.SUB)
sub2.connect('inproc://foo')
sub2.setsockopt(nnpy.SUB, nnpy.SUB_SUBSCRIBE, 'w')

pub.send('hello,')
pub.send('world')
print sub1.recv()
print sub2.recv()
print sub1.recv()

60μs

wrapping up

Questions?