diff --git a/.bookignore b/.bookignore
index e272259..ca876ab 100644
--- a/.bookignore
+++ b/.bookignore
@@ -1,7 +1,8 @@
_book/
+_docs/
_examples/
-tools/
+_tools/
Makefile
building_the_book.md
diff --git a/.gitignore b/.gitignore
index 8584717..4bf2a3a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
_book/
+_docs/
.vscode
.idea/
diff --git a/Makefile b/Makefile
index eb76aa3..0207e23 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
build: init examples
- gitbook build . docs
+ gitbook build . _docs
init:
gitbook install
diff --git a/docs/LICENSE b/docs/LICENSE
deleted file mode 100644
index 261eeb9..0000000
--- a/docs/LICENSE
+++ /dev/null
@@ -1,201 +0,0 @@
- Apache License
- Version 2.0, January 2004
- http://www.apache.org/licenses/
-
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
- 1. Definitions.
-
- "License" shall mean the terms and conditions for use, reproduction,
- and distribution as defined by Sections 1 through 9 of this document.
-
- "Licensor" shall mean the copyright owner or entity authorized by
- the copyright owner that is granting the License.
-
- "Legal Entity" shall mean the union of the acting entity and all
- other entities that control, are controlled by, or are under common
- control with that entity. For the purposes of this definition,
- "control" means (i) the power, direct or indirect, to cause the
- direction or management of such entity, whether by contract or
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
- outstanding shares, or (iii) beneficial ownership of such entity.
-
- "You" (or "Your") shall mean an individual or Legal Entity
- exercising permissions granted by this License.
-
- "Source" form shall mean the preferred form for making modifications,
- including but not limited to software source code, documentation
- source, and configuration files.
-
- "Object" form shall mean any form resulting from mechanical
- transformation or translation of a Source form, including but
- not limited to compiled object code, generated documentation,
- and conversions to other media types.
-
- "Work" shall mean the work of authorship, whether in Source or
- Object form, made available under the License, as indicated by a
- copyright notice that is included in or attached to the work
- (an example is provided in the Appendix below).
-
- "Derivative Works" shall mean any work, whether in Source or Object
- form, that is based on (or derived from) the Work and for which the
- editorial revisions, annotations, elaborations, or other modifications
- represent, as a whole, an original work of authorship. For the purposes
- of this License, Derivative Works shall not include works that remain
- separable from, or merely link (or bind by name) to the interfaces of,
- the Work and Derivative Works thereof.
-
- "Contribution" shall mean any work of authorship, including
- the original version of the Work and any modifications or additions
- to that Work or Derivative Works thereof, that is intentionally
- submitted to Licensor for inclusion in the Work by the copyright owner
- or by an individual or Legal Entity authorized to submit on behalf of
- the copyright owner. For the purposes of this definition, "submitted"
- means any form of electronic, verbal, or written communication sent
- to the Licensor or its representatives, including but not limited to
- communication on electronic mailing lists, source code control systems,
- and issue tracking systems that are managed by, or on behalf of, the
- Licensor for the purpose of discussing and improving the Work, but
- excluding communication that is conspicuously marked or otherwise
- designated in writing by the copyright owner as "Not a Contribution."
-
- "Contributor" shall mean Licensor and any individual or Legal Entity
- on behalf of whom a Contribution has been received by Licensor and
- subsequently incorporated within the Work.
-
- 2. Grant of Copyright License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- copyright license to reproduce, prepare Derivative Works of,
- publicly display, publicly perform, sublicense, and distribute the
- Work and such Derivative Works in Source or Object form.
-
- 3. Grant of Patent License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- (except as stated in this section) patent license to make, have made,
- use, offer to sell, sell, import, and otherwise transfer the Work,
- where such license applies only to those patent claims licensable
- by such Contributor that are necessarily infringed by their
- Contribution(s) alone or by combination of their Contribution(s)
- with the Work to which such Contribution(s) was submitted. If You
- institute patent litigation against any entity (including a
- cross-claim or counterclaim in a lawsuit) alleging that the Work
- or a Contribution incorporated within the Work constitutes direct
- or contributory patent infringement, then any patent licenses
- granted to You under this License for that Work shall terminate
- as of the date such litigation is filed.
-
- 4. Redistribution. You may reproduce and distribute copies of the
- Work or Derivative Works thereof in any medium, with or without
- modifications, and in Source or Object form, provided that You
- meet the following conditions:
-
- (a) You must give any other recipients of the Work or
- Derivative Works a copy of this License; and
-
- (b) You must cause any modified files to carry prominent notices
- stating that You changed the files; and
-
- (c) You must retain, in the Source form of any Derivative Works
- that You distribute, all copyright, patent, trademark, and
- attribution notices from the Source form of the Work,
- excluding those notices that do not pertain to any part of
- the Derivative Works; and
-
- (d) If the Work includes a "NOTICE" text file as part of its
- distribution, then any Derivative Works that You distribute must
- include a readable copy of the attribution notices contained
- within such NOTICE file, excluding those notices that do not
- pertain to any part of the Derivative Works, in at least one
- of the following places: within a NOTICE text file distributed
- as part of the Derivative Works; within the Source form or
- documentation, if provided along with the Derivative Works; or,
- within a display generated by the Derivative Works, if and
- wherever such third-party notices normally appear. The contents
- of the NOTICE file are for informational purposes only and
- do not modify the License. You may add Your own attribution
- notices within Derivative Works that You distribute, alongside
- or as an addendum to the NOTICE text from the Work, provided
- that such additional attribution notices cannot be construed
- as modifying the License.
-
- You may add Your own copyright statement to Your modifications and
- may provide additional or different license terms and conditions
- for use, reproduction, or distribution of Your modifications, or
- for any such Derivative Works as a whole, provided Your use,
- reproduction, and distribution of the Work otherwise complies with
- the conditions stated in this License.
-
- 5. Submission of Contributions. Unless You explicitly state otherwise,
- any Contribution intentionally submitted for inclusion in the Work
- by You to the Licensor shall be under the terms and conditions of
- this License, without any additional terms or conditions.
- Notwithstanding the above, nothing herein shall supersede or modify
- the terms of any separate license agreement you may have executed
- with Licensor regarding such Contributions.
-
- 6. Trademarks. This License does not grant permission to use the trade
- names, trademarks, service marks, or product names of the Licensor,
- except as required for reasonable and customary use in describing the
- origin of the Work and reproducing the content of the NOTICE file.
-
- 7. Disclaimer of Warranty. Unless required by applicable law or
- agreed to in writing, Licensor provides the Work (and each
- Contributor provides its Contributions) on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- implied, including, without limitation, any warranties or conditions
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
- PARTICULAR PURPOSE. You are solely responsible for determining the
- appropriateness of using or redistributing the Work and assume any
- risks associated with Your exercise of permissions under this License.
-
- 8. Limitation of Liability. In no event and under no legal theory,
- whether in tort (including negligence), contract, or otherwise,
- unless required by applicable law (such as deliberate and grossly
- negligent acts) or agreed to in writing, shall any Contributor be
- liable to You for damages, including any direct, indirect, special,
- incidental, or consequential damages of any character arising as a
- result of this License or out of the use or inability to use the
- Work (including but not limited to damages for loss of goodwill,
- work stoppage, computer failure or malfunction, or any and all
- other commercial damages or losses), even if such Contributor
- has been advised of the possibility of such damages.
-
- 9. Accepting Warranty or Additional Liability. While redistributing
- the Work or Derivative Works thereof, You may choose to offer,
- and charge a fee for, acceptance of support, warranty, indemnity,
- or other liability obligations and/or rights consistent with this
- License. However, in accepting such obligations, You may act only
- on Your own behalf and on Your sole responsibility, not on behalf
- of any other Contributor, and only if You agree to indemnify,
- defend, and hold each Contributor harmless for any liability
- incurred by, or claims asserted against, such Contributor by reason
- of your accepting any such warranty or additional liability.
-
- END OF TERMS AND CONDITIONS
-
- APPENDIX: How to apply the Apache License to your work.
-
- To apply the Apache License to your work, attach the following
- boilerplate notice, with the fields enclosed by brackets "[]"
- replaced with your own identifying information. (Don't include
- the brackets!) The text should be enclosed in the appropriate
- comment syntax for the file format. We also recommend that a
- file or class name and description of purpose be included on the
- same "printed page" as the copyright notice for easier
- identification within third-party archives.
-
- Copyright [yyyy] [name of copyright owner]
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
diff --git a/docs/developer/connecting.html b/docs/developer/connecting.html
deleted file mode 100644
index ef5dd0f..0000000
--- a/docs/developer/connecting.html
+++ /dev/null
@@ -1,2039 +0,0 @@
-
-
-
-
Most client libraries provide several ways to connect to the NATS server, gnatsd. The server itself is identified by a standard URL with the nats protocol. Throughout these ../_examples we will rely on a test server, provided by nats.io, at nats://demo.nats.io:4222, where 4222 is the default port for NATS.
-
Connecting to a Specific Server
-
For example, to connect to the demo server with a URL:
// If connecting to the default port, the URL can be simplified
-// to just the hostname/IP.
-// That is, the connect below is equivalent to:
-// nats.Connect("nats://demo.nats.io:4222")
-nc, err := nats.Connect("demo.nats.io")
-if err !=nil{
- log.Fatal(err)
-}
-defer nc.Close()
-
-// Do something with the connection
-
-
-
-
-
-
Connection nc =Nats.connect("nats://demo.nats.io:4222");
-
-// Do something with the connection
-
-nc.close();
-
-
-
-
-
let nc =NATS.connect("nats://demo.nats.io:4222");
-nc.on('connect',(c)=>{
- // Do something with the connection
- doSomething();
- // When done close it
- nc.close();
-});
-nc.on('error',(err)=>{
- failed(err);
-});
-
-
-
-
-
nc = NATS()
-await nc.connect(servers=["nats://demo.nats.io:4222"])
-
-# Do something with the connection
-
-await nc.close()
-
-
-
-
-
-
require'nats/client'
-
-NATS.start(servers:["nats://demo.nats.io:4222"])do|nc|
- # Do something with the connection
-
- # Close the connection
- nc.close
-end
-
-
-
-
-
// will throw an exception if connection fails
- let nc =awaitconnect("nats://demo.nats.io:4222");
- // Do something with the connection
-
- // Close the connection
- nc.close();
-
-
-
-
-
Connecting to the Default Server
-
Some libraries also provide a special way to connect to a default url, which is general nats://localhost:4222:
nc, err := nats.Connect(nats.DefaultURL)
-if err !=nil{
- log.Fatal(err)
-}
-defer nc.Close()
-
-// Do something with the connection
-
-
-
-
-
-
Connection nc =Nats.connect();
-
-// Do something with the connection
-
-nc.close();
-
-
-
-
-
let nc =NATS.connect();
-nc.on('connect',(c)=>{
- // Do something with the connection
- doSomething();
- // When done close it
- nc.close();
-});
-nc.on('error',(err)=>{
- failed(err);
-});
-
-
-
-
-
nc = NATS()
-await nc.connect()
-
-# Do something with the connection
-
-await nc.close()
-
-
-
-
-
-
require'nats/client'
-
-NATS.start do|nc|
- # Do something with the connection
-
- # Close the connection
- nc.close
-end
-
-
-
-
-
// will throw an exception if connection fails
-let nc =awaitconnect();
-// Do something with the connection
-
-// When done close it
-nc.close();
-
-
-// alternatively, you can use the Promise pattern
-let nc1: Client;
-connect()
- .then((c)=>{
- nc1 = c;
- // Do something with the connection
- nc1.close();
- });
- // add a .catch/.finally
-
-
-
-
-
Setting a Connect Timeout
-
Each library has its own, language preferred way, to pass connection options. For example, to set the maximum time to connect to a server to 10 seconds:
nc, err := nats.Connect(nats.DefaultURL, nats.Timeout(10*time.Second))
-if err !=nil{
- log.Fatal(err)
-}
-defer nc.Close()
-
-// Do something with the connection
-
-
-
-
-
-
Options options =newOptions.Builder().
- server("nats://demo.nats.io:4222").
- connectionTimeout(Duration.ofSeconds(10)).// Set timeout
- build();
-Connection nc =Nats.connect(options);
-
-// Do something with the connection
-
-nc.close();
-
-
-
-
-
// connection timeout is not supported on node-nats
-
-
-
-
-
nc = NATS()
-await nc.connect(connect_timeout=2)
-
-# Do something with the connection
-
-await nc.close()
-
-
-
-
-
-
# There is currently no connect timeout as part of the Ruby NATS client API, but you can use a timer to mimic it.
-require'nats/client'
-
-timer =EM.add_timer(5)do
- NATS.connect do|nc|
- # Do something with the connection
-
- # Close the connection
- nc.close
- end
-end
-EM.cancel_timer(timer)
-
-
-
-
-
let nc =awaitconnect({
- url:"nats://demo.nats.io:4222",
- timeout:1000
-});
-
-
-
-
-
-
The available options are discussed more below, in other pages, and in the documentation for your client library.
-
Connecting to a Cluster
-
When connecting to a cluster, there are a few things to think about.
-
-
Passing a URL for each cluster member (semi-optional)
-
The connection algorithm
-
The reconnect algorithm (discussed later)
-
Server provided URLs
-
-
When a client connects to the server, the server may provide a list of URLs for additional known servers. This allows a client to connect to one server and still have other servers available during reconnect. However, the initial connection cannot depend on these additional servers. Rather, the additional connection will try to connect to each of the URLs provided in the connect call and will fail if it is unable to connect to any of them. Note, failure behavior is library dependent, please check the documentation for your client library on information about what happens if the connect fails.
servers :=[]string{"nats://localhost:1222",
- "nats://localhost:1223",
- "nats://localhost:1224",
-}
-
-nc, err := nats.Connect(strings.Join(servers,","))
-if err !=nil{
- log.Fatal(err)
-}
-defer nc.Close()
-
-// Do something with the connection
-
-
-
-
-
-
Options options =newOptions.Builder().
- server("nats://localhost:1222").
- server("nats://localhost:1223").
- server("nats://localhost:1224").
- build();
-Connection nc =Nats.connect(options);
-
-// Do something with the connection
-
-nc.close();
-
-
-
-
-
let nc =NATS.connect({
- servers:[
- "nats://demo.nats.io:4222",
- "nats://localhost:4222"
- ]}
-);
-
-nc.on('connect',(c)=>{
- // Do something with the connection
- doSomething();
- // When done close it
- nc.close();
-});
-nc.on('error',(err)=>{
- failed(err);
-});
-
-
-
-
-
nc = NATS()
-await nc.connect(servers=[
- "nats://127.0.0.1:1222",
- "nats://127.0.0.1:1223",
- "nats://127.0.0.1:1224"
- ])
-
-# Do something with the connection
-
-await nc.close()
-
-
-
-
-
-
require'nats/client'
-
-NATS.start(servers:["nats://127.0.0.1:1222","nats://127.0.0.1:1223","nats://127.0.0.1:1224"])do|nc|
- # Do something with the connection
-
- # Close the connection
- nc.close
-end
-
-
-
-
-
// will throw an exception if connection fails
-let nc =awaitconnect({
- servers:[
- "nats://demo.nats.io:4222",
- "nats://localhost:4222"
- ]
-});
-// Do something with the connection
-
-// When done close it
-nc.close();
-
-
-
-
-
Reconnecting
-
Most, if not all, of the client libraries will reconnect to the server if they are disconnected due to a network problem. The reconnect logic can differ by library, so check your client libraries. In general, the client will try to connect to all of the servers it knows about, either through the URLs provided in connect or the URLs provided by its most recent server. The library may have several options to help control reconnect behavior.
require'nats/client'
-
-NATS.start(servers:["nats://127.0.0.1:1222","nats://127.0.0.1:1223","nats://127.0.0.1:1224"], reconnect:false)do|nc|
- # Do something with the connection
-
- # Close the connection
- nc.close
-end
-
-
-
-
-
// will throw an exception if connection fails
-let nc =awaitconnect({
- reconnect:false,
- servers:["nats://demo.nats.io:4222"]
-});
-nc.close();
-
-
-
-
-
Set the Number of Reconnect Attempts
-
Applications can set the maximum reconnect attempts. Generally, this will limit the actual number of attempts total, but check your library documentation. For example, in Java, if the client knows about 3 servers and the maximum reconnects is set to 2, it will not try all of the servers. On the other hand, if the maximum is set to 6 it will try all of the servers twice before considering the reconnect a failure and closing.
// Set max reconnects attempts
-nc, err := nats.Connect("demo.nats.io", nats.MaxReconnects(10))
-if err !=nil{
- log.Fatal(err)
-}
-defer nc.Close()
-
-// Do something with the connection
-
-
-
-
-
-
Options options =newOptions.Builder().
- server("nats://demo.nats.io:4222").
- maxReconnects(10).// Set max reconnect attempts
- build();
-Connection nc =Nats.connect(options);
-
-// Do something with the connection
-
-nc.close();
-
-
-
-
-
let nc =NATS.connect({
- maxReconnectAttempts:10,
- servers:["nats://demo.nats.io:4222"]
-});
-
-
-
-
-
nc = NATS()
-await nc.connect(
- servers=["nats://demo.nats.io:4222"],
- max_reconnect_attempts=10,
- )
-
-# Do something with the connection
-
-await nc.close()
-
-
-
-
-
-
require'nats/client'
-
-NATS.start(servers:["nats://127.0.0.1:1222","nats://127.0.0.1:1223","nats://127.0.0.1:1224"], max_reconnect_attempts:10)do|nc|
- # Do something with the connection
-
- # Close the connection
- nc.close
-end
-
-
-
-
-
// will throw an exception if connection fails
-let nc =awaitconnect({
- maxReconnectAttempts:10,
- servers:["nats://demo.nats.io:4222"]
-});
-nc.close();
-
-
-
-
-
Pausing Between Reconnect Attempts
-
It doesn’t make much sense to try to connect to the same server over and over. To prevent this sort of thrashing, and wasted reconnect attempts, libraries provide a wait setting. This setting will pause the reconnect logic if the same server is being tried multiple times. In the previous example, if you have 3 servers and 6 attempts, the Java library would loop over the three servers. If none were connectable, it will then try all three again. However, the Java client doesn’t wait between each attempt, only when trying the same server again, so in that example the library may never wait. If on the other hand, you only provide a single server URL and 6 attempts, the library will wait between each attempt.
// Set reconnect interval to 10 seconds
-nc, err := nats.Connect("demo.nats.io", nats.ReconnectWait(10*time.Second))
-if err !=nil{
- log.Fatal(err)
-}
-defer nc.Close()
-
-// Do something with the connection
-
-
-
-
-
-
Options options =newOptions.Builder().
- server("nats://demo.nats.io:4222").
- reconnectWait(Duration.ofSeconds(10)).// Set Reconnect Wait
- build();
-Connection nc =Nats.connect(options);
-
-// Do something with the connection
-
-nc.close();
-
-
-
-
-
let nc =NATS.connect({
- reconnectTimeWait:10*1000,//10s
- servers:["nats://demo.nats.io:4222"]
-});
-
-
-
-
-
nc = NATS()
-await nc.connect(
- servers=["nats://demo.nats.io:4222"],
- reconnect_time_wait=10,
- )
-
-# Do something with the connection
-
-await nc.close()
-
-
-
-
-
-
require'nats/client'
-
-NATS.start(servers:["nats://127.0.0.1:1222","nats://127.0.0.1:1223","nats://127.0.0.1:1224"], reconnect_time_wait:10)do|nc|
- # Do something with the connection
-
- # Close the connection
- nc.close
-end
-
-
-
-
-
// will throw an exception if connection fails
-let nc =awaitconnect({
- reconnectTimeWait:10*1000,//10s
- servers:["nats://demo.nats.io:4222"]
-});
-nc.close();
-
-
-
-
-
Avoiding the Thundering Herd
-
When a server goes down, there is a possible anti-pattern called the Thundering Herd where all of the clients try to reconnect immediately creating a denial of service attack. In order to prevent this, most NATS client libraries randomize the servers they attempt to connect to. This setting has no effect if only a single server is used, but in the case of a cluster, randomization, or shuffling, will ensure that no one server bears the brunt of the client reconnect attempts.
require'nats/client'
-
-NATS.start(servers:["nats://127.0.0.1:1222","nats://127.0.0.1:1223","nats://127.0.0.1:1224"], dont_randomize_servers:true)do|nc|
- # Do something with the connection
-
- # Close the connection
- nc.close
-end
-
-
-
-
-
// will throw an exception if connection fails
-let nc =awaitconnect({
- noRandomize:false,
- servers:["nats://127.0.0.1:4443",
- "nats://demo.nats.io:4222"
- ]
-});
-nc.close();
-
-
-
-
-
Listening for Reconnect Events
-
Because reconnect is primarily under the covers many libraries provide an event listener you can use to be notified of reconnect events. This event can be especially important for applications sending a lot of messages.
// Connection event handlers are invoked asynchronously
-// and the state of the connection may have changed when
-// the callback is invoked.
-nc, err := nats.Connect("demo.nats.io",
- nats.DisconnectHandler(func(nc *nats.Conn){
- // handle disconnect event
- }),
- nats.ReconnectHandler(func(nc *nats.Conn){
- // handle reconnect event
- }))
-if err !=nil{
- log.Fatal(err)
-}
-defer nc.Close()
-
-// Do something with the connection
-
-
nc = NATS()
-
-asyncdefdisconnected_cb():
- print("Got disconnected!")
-
-asyncdefreconnected_cb():
- # See who we are connected to on reconnect.
- print("Got reconnected to {url}".format(url=nc.connected_url.netloc))
-
-await nc.connect(
- servers=["nats://demo.nats.io:4222"],
- reconnect_time_wait=10,
- reconnected_cb=reconnected_cb,
- disconnected_cb=disconnected_cb,
- )
-
-# Do something with the connection.
-
-
-
-
-
-
require'nats/client'
-
-NATS.start(servers:["nats://127.0.0.1:1222","nats://127.0.0.1:1223","nats://127.0.0.1:1224"])do|nc|
- # Do something with the connection
- nc.on_reconnect do
- puts "Got reconnected to #{nc.connected_server}"
- end
-
- nc.on_disconnect do|reason|
- puts "Got disconnected! #{reason}"
- end
-end
-
-
-
-
-
// will throw an exception if connection fails
-let nc =awaitconnect({
- maxReconnectAttempts:10,
- servers:["nats://demo.nats.io:4222"]
-});
-// first argument is the connection (same as nc in this case)
-// second argument is the url of the server where the client
-// connected
-nc.on('reconnect',(conn, server)=>{
- console.log('reconnected to', server);
-});
-nc.close();
-
-
-
-
-
Buffering Messages During Reconnect Attempts
-
There is another setting that comes in to play during reconnection. This setting controls how much memory the client library will hold in the form of outgoing messages while it is disconnected. During a short reconnect, the client will generally allow applications to publish messages but because the server is offline, will be cached in the client. The library will then send those messages on reconnect. When the maximum reconnect buffer is reached, messages will no longer be publishable by the client.
// Set reconnect buffer size in bytes (5 MB)
-nc, err := nats.Connect("demo.nats.io", nats.ReconnectBufSize(5*1024*1024))
-if err !=nil{
- log.Fatal(err)
-}
-defer nc.Close()
-
-// Do something with the connection
-
-
-
-
-
-
Options options =newOptions.Builder().
- server("nats://demo.nats.io:4222").
- reconnectBufferSize(5*1024*1024).// Set buffer in bytes
- build();
-Connection nc =Nats.connect(options);
-
-// Do something with the connection
-
-nc.close();
-
-
-
-
-
// Reconnect buffer size is not configurable on node-nats
-
-
-
-
-
# Asyncio NATS client currentply does not implement a reconnect buffer
-
-
-
-
-
# There is currently no reconnect pending buffer as part of the Ruby NATS client.
-
-
-
-
-
// Reconnect buffer size is not configurable on ts-nats
-
-
-
-
-
-
As mentioned throughout this document, each client library may behave slightly differently. Please check the documentation for the library you are using.
NATS messaging involves the electronic exchange of data among computer applications and provides a layer between the application and the underlying physical network. Application data is encoded as a message and sent by a publisher. The message is received, decoded, and processed by one or more subscribers. A subscriber can process a NATS message synchronously or asynchronously, depending on the client library used.
-
Asynchronous
-
Asynchronous processing uses a callback message handler to process messages. When a message arrives, the registered callback handler receives control to process the message. The client or the consuming application is not blocked from performing other work while it is waiting for a message. Asynchronous processing lets you create multi-threaded dispatching designs.
-
Synchronous
-
Synchronous processing requires that application code explicitly call a method to process an incoming message. Typically the message request is a blocking call that suspends processing until a message becomes available and if no message is available, the period for which the message processing call blocks, would be set by the client. Synchronous processing is typically used by a server whose purpose is to wait for and process incoming request messages and to send replies to the requesting application.
NATS makes it easy for programs to communicate across different environments, languages, and systems because all a client has to do is parse the message. NATS lets programs share common message-handling code, isolate resources and interdependencies, and scale by easily handling an increase in message volume.
-
Publish Subscribe
-
NATS implements a publish subscribe message distribution model as a one-to-many communication. A publisher sends a message on a subject and any active subscriber listening on that subject receives the message. Subscribers can also register interest in wildcard subjects. NATS and NATS Streaming combine to offer two qualities of service:
-
-
At Most Once Delivery (NATS w/TCP reliability) - In the basic NATS platform, if a subscriber is not listening on the subject (no subject match), or is not active when the message is sent, the message is not received. NATS is a fire-and-forget messaging system. If you need higher levels of service, you can either use NATS Streaming, or build the additional reliability into your client(s) yourself.
-
-
At Least Once Delivery (NATS Streaming) - Some applications require higher levels of service and more stringent delivery guarantees but at the potential cost of lower message throughput and higher end-to-end delivery latency. These applications rely on the underlying messaging transport to ensure that messages are delivered to subscribers irrespective of network outages or whether or not a subscriber is offline at a particular point in time.
Try NATS publish subscribe on your own, using a live server by walking through the pub-sub tutorial.
-
Request Reply
-
NATS supports two flavors of request reply messaging: point-to-point or one-to-many. Point-to-point involves the fastest or first to respond. In a one-to-many exchange, you set a limit on the number of responses the requestor may receive.
-
In a request-response exchange, publish request operation publishes a message with a reply subject expecting a response on that reply subject. You can request to automatically wait for a response inline.
-
The request creates an inbox and performs a request call with the inbox reply and returns the first reply received. This is optimized in the case of multiple responses.
Try NATS request reply on your own, using a live server by walking through the request/reply tutorial.
-
Queue Subscribers & Sharing Work
-
NATS provides a load balancing feature called queue subscriptions. Using queue subscribers will load balance message delivery across a group of subscribers which can be used to provide application fault tolerance and scale workload processing.
-
To create a queue subscription, subscribers register a queue name. All subscribers with the same queue name form the queue group. As messages on the registered subject are published, one member of the group is chosen randomly to receive the message. Although queue groups have multiple subscribers, each message is consumed by only one.
-
Queue subscribers can be asynchronous, in which case the message handler callback function processes the delivered message. Synchronous queue subscribers must build in logic to process the message. Queue subscribers are ideal for auto scaling as you can add or remove them anytime, without any configuration changes or restarting the server or clients.