= nng(7)
//
// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

== NAME

nng - nanomsg next generation

== SYNOPSIS

*cc* ['flags'] 'files' *-lnng* ['libraries']

== DESCRIPTION

_NNG_ provides a common messaging framework intended to
solve common communication problems in distributed applications.
It offers a number of _protocols_, and also a number of _transports_.

The _protocols_ implement the semantics associated with particular
communications scenarios, such as RPC style services, service discovery,
publish/subscribe, and so forth.

The _transports_ provide support for underlying transport methods, such
as TCP, IPC, websockets, and so forth.

_NNG_ is designed to permit easy creation of new _transports_ and,
to a lesser extent, new _protocols_.

_NNG_ is wire compatible with the SP protocols described in
the nanomsg project; projects using
https://github.com/nanomsg/nanomsg[_libnanomsg_] can inter-operate with
nng as well as other conforming implementations.  (One such implementation
is https://github.com/go-mangos/mangos[_mangos_].)
Applications using _NNG_
which wish to communicate with other libraries must ensure that they only
use protocols or transports offered by the other library.

_NNG_ also offers a compatible API, permitting legacy code to
be recompiled or relinked against _NNG_.  When doing this, support for
certain enhancements or features will likely be absent, requiring the
application developer to use the new-style API.

_NNG_ is implemented in pure C; if you need bindings for
other languages please check the http://nanomsg.org/[website].

=== Protocols

[horizontal]
xref:nng_bus.7.adoc[nng_bus(7)]:: Bus protocol
xref:nng_pair.7.adoc[nng_pair(7)]:: Pair protocol
xref:nng_pub.7.adoc[nng_pub(7)]:: Publisher side of publish/subscribe protocol
xref:nng_pull.7.adoc[nng_pull(7)]:: Pull side of pipeline protocol
xref:nng_push.7.adoc[nng_push(7)]:: Push side of pipeline protocol
xref:nng_sub.7.adoc[nng_sub(7)]:: Subscriber side of publish/subscribe protocol
xref:nng_rep.7.adoc[nng_rep(7)]:: Reply side of request/reply protocol
xref:nng_req.7.adoc[nng_req(7)]:: Request side of request/reply protocol
xref:nng_respondent.7.adoc[nng_respondent(7)]:: Respondent side of survey protocol
xref:nng_surveyor.7.adoc[nng_surveyor(7)]:: Surveyor side of survey protocol

=== Transports

[horizontal]
xref:nng_inproc.7.adoc[nng_inproc(7)]:: Intra-process transport
xref:nng_ipc.7.adoc[nng_ipc(7)]:: Inter-process transport
xref:nng_tls.7.adoc[nng_tls(7)]:: TLSv1.2 over TCP transport
xref:nng_tcp.7.adoc[nng_tcp(7)]:: TCP (and TCPv6) transport
xref:nng_ws.7.adoc[nng_ws(7)]:: WebSocket transport
xref:nng_zerotier.7.adoc[nng_zerotier(7)]:: ZeroTier transport

=== Conceptual Overview

_NNG_ presents a _socket_ view of networking.
The sockets are constructed using protocol-specific functions, as a given
socket implements precisely one protocol.

Each socket can be used to send and receive messages (if the protocol)
supports it, and implements the appropriate protocol semantics.
For example, xref:nng_sub.7.adoc[_sub_] sockets automatically filter incoming
messages to discard those for topics that have not been subscribed.

_NNG_ sockets are message oriented, so that messages are either delivered
wholly, or not at all.  Partial delivery is not possible.
Furthermore, _NNG_ does not provide any other delivery or ordering guarantees;
messages may be dropped or reordered
(Some protocols, such as xref:nng_req.7.adoc[_req_] may offer stronger
guarantees by performing their own retry and validation schemes.)

Each socket can have zero, one, or many endpoints, which are either
_listeners_ or _dialers_.
(A given socket may freely choose whether it uses listeners, dialers, or both.)
These endpoints provide access to underlying transports, such as TCP, etc.

Each endpoint is associated with a URL, which is a service address.
For dialers, this will be the service address that will be contacted, whereas
for listeners this is where the listener will accept new connections.

Endpoints do not themselves transport data.
They are instead responsible for the creation of _pipes_, which can be
thought of as message-oriented connected streams.
Pipes frequently correspond to a single underlying byte stream.
For example both IPC and TCP transports implement their
pipes using a 1:1 relationship with a connected operating system socket.

Endpoints create pipes as needed.
Listeners will create them when a new client connection request arrives,
and dialers will generally create one, then wait for it to disconnect before
reconnecting.

Most applications should not have to worry about endpoints or pipes at
all; the socket abstraction should provide all the functionality needed
other than in a few specific circumstances.

[[raw_mode]]
==== Raw Mode

(((cooked mode)))(((raw mode)))
Most applications will use sockets in normal, or _cooked_, mode.
This mode provides the full semantics of the protocol.
For example, xref:nng_req.7.adoc[_req_] sockets will automatically
match a reply to a request, and resend requests periodically if no reply
was received.

There are situations, such as with xref:nng_device.3.adoc[proxies],
where it is desirable to bypass these semantics and simply pass messages
to and from the socket with no extra semantic handling.
This is possible using _raw_ mode sockets.

Raw mode sockets are generally constructed with a different function,
such as xref:nng_req_open.3.adoc[`nng_req0_open_raw()`].
Using these sockets, the application can simply send and receive messages,
and is responsible for supplying any additional socket semantics.
Typically this means that the application will need to inspect message
headers on incoming messages, and supply them on outgoing messages.

TIP: The xref:nng_device.3.adoc[`nng_device()`] function only works with raw mode
sockets, but as it only forwards the messages, no additional application
processing is needed.

==== URLs

(((URL)))
_NNG_ uses ((universal resource locators)) (URLs)
following the format specified in
https://tools.ietf.org/html/rfc3986[RFC 3986],
including some schemes that are unique
to SP.
(((URL, canonicalized)))
The URLs used in _NNG_ are canonicalized as follows, mostly in
accordance with
https://tools.ietf.org/html/rfc3986#section-6.2.2[RFC 3986 6.2.2]:

  . The URL is parsed into scheme, userinfo, host, port, path, query and
    fragment components.  (Not all of these members are necessarily present.)
  . The scheme, hostname, and port if present, are converted to lower case.
  . Percent-encoded values for
    https://tools.ietf.org/html/rfc3986#section-2.3[unreserved characters]
    converted to their unencoded forms.
  . Additionally URL percent-encoded values for characters in the path
    and with numeric values larger than 127 (i.e. not ASCII) are decoded.
  . The resulting path is checked for invalid UTF-8 sequences, consisting
    of surrogate pairs, illegal byte sequences, or overlong encodings.
    If this check fails, then the entire URL is considered invalid.
  . Path segments consisting of `.` and `..` are resolved as per
    https://tools.ietf.org/html/rfc3986#section-6.2.2.3[RFC 3986 6.2.2.3].
  . Further, empty path segments are removed, meaning that duplicate
    slash (`/`) separators are removed from the path.

Note that steps 4, 5, and 7 are not specified by RFC 3986, but performing
them is believed to improve both the usability and security of 
applications, without violating RFC 3986 itself.

TIP: Port numbers may be service names in some instances, but it is recommended
that numeric port numbers be used when known.
If service names are used, it is recommended that they follow the naming
conventions for C identifiers, and not be longer than 32 characters in length.
This will maximize compatibility across systems and minimize opportunities for
confusion when they are parsed on different systems.

=== API

The library API is documented at xref:libnng.3.adoc[libnng(3)].

== SEE ALSO

[.text-left]
xref:libnng.3.adoc[libnng(3)],
xref:nng_compat.3compat.adoc[nng_compat(3compat)]
