nanomsg is a socket library that provides several common communication patterns. It aims to make the networking layer fast, scalable, and easy to use.
nanomsg was initiated by efforts from Martin Sústrik, the creator of ZeroMQ, nanomsg, and gets further evolved past version 1.1.2+ released in 2017/Q4
import nnpy
# ----------------------------------------# DEF SOCKET ARCHETYPE ACCESS-POINTS:
p1 = nnpy.Socket( nnpy.AF_SP, nnpy.PAIR )
p2 = nnpy.Socket( nnpy.AF_SP, nnpy.PAIR )
# ----------------------------------------# SET TRANSPORT CLASS(-es) TO BE USED:
p1.bind( 'inproc://pairExcellence' ) # p1.bind( 'ipc:///tmp/pair' )
p2.connect( 'inproc://pairExcellence' ) # p2.connect( 'ipc:///tmp/pair' )
# ----------------------------------------# USE THEM TO SEND MESSAGES:
p1.send( 'hello, p2' ); p1.send( 'I am p1' )
# ----------------------------------------# AND ENJOY THE AUTOMATED DETAILS:
print( "P2 RECV'd: {0:}".format( p2.recv() ) ); p2.send( 'nice to meet you' )
print( "P1 RECV'd: {0:}".format( p1.recv() ) )
print( "P2 RECV'd: {0:}".format( p2.recv() ) )
INTRO:
The concept still uses a word socket for naming a communication channel, yet it is rather a well abstracted socket, than a POSIX-compliant socket one may have been used to - let us view these abstracted sockets as rather a pre-wired behaviour archetype, that was encoded into their operations:
a
PAIR
- a simple archetype for one-to-one communicationa
BUS
- an archetype for a simple many-to-many communicationa
REQREP
- archetype that allows to build clusters of stateless services to process user requestsa
PUBSUB
- an archetype that distributes messages to large sets of interested subscribersa
PIPELINE
- archetype aggregates messages from multiple sources and load balances them among many destinationsa
SURVEY
- archetype allows to query state of multiple applications in a single go
NOT A SOCKET AS A SOCKET?
To understand better the breadth of the potential setup and configuration options, one may first notice:
- The nanomsg abstracted-socket access-point may be either "connected" ( using a call to
nn_connect()
) towards another access-point present and ready at some remote infrastructure address or "bound" ( usingnn_bind
) which instantiates such a service access-point for the former one. - There may be multiple connects and binds on a single abstracted nanomsg socket instance.
- There are certain nanomsg socket configuration options that may be set or changed by programmer.
DOCUMENTATION:
While nanomsg borrows a lot from Martin Sústrik's work on ZeroMQ, there are principal differences and other language-specific details documented here.