Enm
Erlang driver for nanomsg
Install / Use
/learn @basho/EnmREADME
enm is an
Erlang port driver
that wraps the nanomsg C library, allowing Erlang
systems to communicate with other nanomsg endpoints. enm supports
idioms and approaches common to standard Erlang networking facilities such
as gen_tcp and gen_udp.
enm is currently based on version 1.0.0 of nanomsg, and enm itself
is new, so its features are experimental and subject to change.
Starting and Stopping
You can start enm as a normal application, using
application:start(enm) and application:stop(enm). You can also call
enm:start_link/0 or enm:start/0, and call enm:stop/0 to stop it.
Just Open a Socket
enm supports all nanomsg scalability protocols and transports. You can
open a socket providing a particular scalability protocol using functions
named for each protocol. For example, the enm:pair/0 function opens a
pair-type socket for one-to-one communication, and the enm:req/0 and
enm:rep/0 functions open the request and reply ends, respectively, of the
reqrep scalability protocol. The arity 0 versions of the enm
scalability protocol functions listed below use default settings for the
open sockets, while the arity 1 versions allow a list of socket
options to be passed in order to control socket settings.
req: open the request end of thereqrepprotocolrep: open the reply end of thereqrepprotocolpair: open a pair socket for one-to-one communicationsbus: open a bus socket for many-to-many communicationspub: open the publication end of thepubsubprotocolsub: open the subscriber end of thepubsubprotocolpush: open the pushing end of thepipelineprotocolpull: open the pulling end of thepipelineprotocolsurveyor: open the query end of thesurveyprotocolrespondent: open the response end of thesurveyprotocol
If successful, these functions — both their arity 0 and arity 1
versions — all return {ok,Socket}.
Once opened, sockets can be bound or connected using the enm:bind/2 or
enm:connect/2 functions respectively. Bind and connect information can
alternatively be provided via socket options when
sockets are first opened via the functions listed above.
Functions
In addition to the scalability protocol functions,
enm supports the following functions:
send(Socket, Data): sendDataonSocket.Datais an Erlangiolist, thus allowing lists of binaries and characters, or nested lists thereof, to be sent.recv(Socket): receive data fromSocket. This function blocks indefinitely until data arrive. Returns{ok,Data}on success, or an error tuple on failure.Datadefaults to a binary unless the socket was opened inlistmode or was set intolistmode viasetopts/2.recv(Socket, Timeout): same asrecv/1but if no data arrive withinTimeoutmilliseconds, return{error,etimedout}.bind(Socket, Address): bindSockettoAddress, whereAddresssupports one of the nanomsg transport types: inproc, ipc, or TCP.Addresscan be either a string or binary using the nanomsg URL address format, such as"inproc://foo"to bind to an intraprocess address or"tcp://*:12345"to listen on all your host's network interfaces on port 12345, or it can be one of theenmaddress record types.getopts(Socket, Options): return the current setting onSocketfor each of the option names listed inOptions. If successful, returns{ok, OptionList}where each element of the list provides the name and setting of one of the requested options. Ifgetoptsfails it return an error tuple.setopts(Socket, OptionList): apply each of the option settings inOptionListtoSocket. Returnsokif successful or an error tuple on failure.controlling_process(Socket, Pid): the current controlling process forSocketcan call this function to transfer its control to the process represented byPid. The controlling process of aSocketis initially the one that opens it, and it's the one that receives data messages as Erlang messages if the socket is in an active mode.shutdown(Socket, EndpointId): removes the endpoint associated withEndpointId, created viabind/2orconnect/2, fromSocket.close(Socket): closesSocket.
If you're already familiar with standard Erlang networking capabilities,
you'll find these functions similar to functions supplied by standard
modules such as gen_tcp, gen_udp and inet.
Address Record Types
To help avoid errors with mistyped string and binary address URLs, enm
provides three record types you can use for addresses instead:
#nn_inproc{addr=Address}: for intra-process addresses.Addressis a name in either string or binary form.#nn_ipc{path=Path}: for IPC addresses.Pathcan be either an absolute pathname or a pathname relative to the current working directory, in either string or binary form.#nn_tcp{interface=Interface, addr=Address, port=Port}: for TCP addresses.Interfacecan be the atomany, a network address in string or tuple form, or a string representing a network interface name.Addresscan be a hostname or a network address in either string or binary form.Portis a port number.
Using these types, which is completely optional, requires including the
enm.hrl file.
Socket Options
enm supports several socket options that can be set either when the
socket is opened, or modified later during operation. Most socket options
can also be read from enm sockets. enm supports the following options:
type: indicates the type of socket. For example, thennreqtype indicates a socket opened via thereqfunction, andnnsurveyorindicates a socket implementing the query end of thesurveyprotocol. This option can only be read from anenmsocket and cannot be set.active: this controls how messages are delivered from anenmsocket to its controlling Erlang process.- The default setting,
{active,true}, means that the driver reads data from the socket as soon as they arrive and sends them as Erlang messages to the controlling process. - The
{active,false}setting puts anenmsocket in passive mode; data from such a socket are retrieved only via theenm:recv/{1,2}functions. - The
{active,once}setting allows the driver to deliver one message from the socket to the controlling process, after which the socket flips automatically to{active,false}mode. This allows the application to receive nanomsg messages as Erlang messages only when it's ready to handle them. - The
{active,N}mode, whereNrepresents an integer, is similar to{active,once}mode except that it allows the driver to receiveNmessages on the socket and deliver them as Erlang messages to the controlling process before flipping the socket into{active,false}mode. When the socket flips to passive mode,enmsends a{X_passive,Socket}message to the controlling process, with the socket's actual type name substituted for "X" (for example,{nnpair_passive, Socket}ifSocketis apairsocket).
- The default setting,
raw: this option, which defaults to false, controls whether the underlying nanomsg socket is opened with theAF_SPdomain (the default, or set via{raw,false}) or theAF_SP_RAWdomain (set via{raw,true}). Using the atomrawby itself is equivalent to{raw,true}. See the nanomsg nn_socket man page for more details on theAF_SPandAF_SP_RAWsocket domains.mode: this controls the form of the data delivered or retrieved from the socket. The default,binary, means that data from the socket are delivered to the application as Erlang binaries, whereas thelistsetting means socket data are delivered as Erlang lists. Using the atombinaryby itself is equivalent to{mode,binary}, andlistby itself is equivalent to{mode,list}.bind: this option allows you to open a socket and then immediately bind it to the given address. See thebindfunction description for more details on the allowable forms for the bind address. Note, however, that the bind endpoint identifier is thrown away in this case. If you need to later manage the endpoint viashutdown, use thebindfunction instead.connect: this option allows you open a socket and then immediately connect it to the given address. See theconnectfunction description for more details on the allowable forms for the connect address. Note, however, that the connect endpoint identifier is thrown away in this case. If you need to later manage the endpoint viashutdown, use theconnectfunction instead.deadline: forsurveyorsockets, set the surveyor deadline to specify how long, in milliseconds, to wait for responses to arrive.subscribe: forsubsockets, subscribe to the named topic, specified either as a string or a binary. Topic names must be less than 256 characters in length (this is anenmlimit, not a nanomsg limit). Applying thesubscribeoption to a socket type other thansubresults in abadargexception.unsubscribe: forsubsockets, unsubscribe from the named topic, specified either as a string or a binary. As for thesubscribeoption, topic names must be less than 256 characters in length. Applying theunsubscribeoption to a socket type other thansubresults in abadargexception.resend_ivl: forreqsockets, set the request resend inter
Related Skills
node-connect
349.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
