1
0
mirror of https://github.com/taigrr/nats.docs synced 2025-01-18 04:03:23 -08:00
nats.docs/docs/nats_protocol/nats-protocol.html
2019-05-31 13:30:30 -05:00

3078 lines
98 KiB
HTML

<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Client Protocol ยท NATS</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<meta name="author" content="The NATS Maintainers">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="nats-client-dev.html" />
<link rel="prev" href="nats-protocol-demo.html" />
<link rel="stylesheet" href="https://cdn.materialdesignicons.com/3.6.95/css/materialdesignicons.min.css">
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="../whats_new/whats_new_20.html">
<a href="../whats_new/whats_new_20.html">
What's New in 2.0
</a>
</li>
<li class="chapter " data-level="1.3" data-path="../faq.html">
<a href="../faq.html">
FAQ
</a>
</li>
<li class="header">Concepts</li>
<li class="chapter " data-level="2.1" data-path="../developer/concepts/intro.html">
<a href="../developer/concepts/intro.html">
What is NATS
</a>
</li>
<li class="chapter " data-level="2.2" data-path="../developer/concepts/subjects.html">
<a href="../developer/concepts/subjects.html">
Subject-Based Messaging
</a>
</li>
<li class="chapter " data-level="2.3" data-path="../developer/concepts/pubsub.html">
<a href="../developer/concepts/pubsub.html">
Publish-Subscribe
</a>
</li>
<li class="chapter " data-level="2.4" data-path="../developer/concepts/reqreply.html">
<a href="../developer/concepts/reqreply.html">
Request-Reply
</a>
</li>
<li class="chapter " data-level="2.5" data-path="../developer/concepts/queue.html">
<a href="../developer/concepts/queue.html">
Queue Groups
</a>
</li>
<li class="chapter " data-level="2.6" data-path="../developer/concepts/acks.html">
<a href="../developer/concepts/acks.html">
Acknowledgements
</a>
</li>
<li class="chapter " data-level="2.7" data-path="../developer/concepts/seq_num.html">
<a href="../developer/concepts/seq_num.html">
Sequence Numbers
</a>
</li>
<li class="header">Developing With NATS</li>
<li class="chapter " data-level="3.1" data-path="../developer/">
<a href="../developer/">
Introduction
</a>
</li>
<li class="chapter " data-level="3.2" data-path="../developer/connecting/intro.html">
<a href="../developer/connecting/intro.html">
Connecting
</a>
<ul class="articles">
<li class="chapter " data-level="3.2.1" data-path="../developer/connecting/default_server.html">
<a href="../developer/connecting/default_server.html">
Connecting to the Default Server
</a>
</li>
<li class="chapter " data-level="3.2.2" data-path="../developer/connecting/specific_server.html">
<a href="../developer/connecting/specific_server.html">
Connecting to a Specific Server
</a>
</li>
<li class="chapter " data-level="3.2.3" data-path="../developer/connecting/cluster.html">
<a href="../developer/connecting/cluster.html">
Connecting to a Cluster
</a>
</li>
<li class="chapter " data-level="3.2.4" data-path="../developer/connecting/connect_timeout.html">
<a href="../developer/connecting/connect_timeout.html">
Setting a Connect Timeout
</a>
</li>
<li class="chapter " data-level="3.2.5" data-path="../developer/connecting/pingpong.html">
<a href="../developer/connecting/pingpong.html">
Ping/Pong Protocol
</a>
</li>
<li class="chapter " data-level="3.2.6" data-path="../developer/connecting/protocol.html">
<a href="../developer/connecting/protocol.html">
Controlling the Client/Server Protocol
</a>
</li>
<li class="chapter " data-level="3.2.7" data-path="../developer/connecting/noecho.html">
<a href="../developer/connecting/noecho.html">
Turning Off Echo'd Messages
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.3" data-path="../developer/reconnect/intro.html">
<a href="../developer/reconnect/intro.html">
Automatic Reconnections
</a>
<ul class="articles">
<li class="chapter " data-level="3.3.1" data-path="../developer/reconnect/disable.html">
<a href="../developer/reconnect/disable.html">
Disabling Reconnect
</a>
</li>
<li class="chapter " data-level="3.3.2" data-path="../developer/reconnect/max.html">
<a href="../developer/reconnect/max.html">
Set the Number of Reconnect Attempts
</a>
</li>
<li class="chapter " data-level="3.3.3" data-path="../developer/reconnect/wait.html">
<a href="../developer/reconnect/wait.html">
Pausing Between Reconnect Attempts
</a>
</li>
<li class="chapter " data-level="3.3.4" data-path="../developer/reconnect/random.html">
<a href="../developer/reconnect/random.html">
Avoiding the Thundering Herd
</a>
</li>
<li class="chapter " data-level="3.3.5" data-path="../developer/reconnect/events.html">
<a href="../developer/reconnect/events.html">
Listening for Reconnect Events
</a>
</li>
<li class="chapter " data-level="3.3.6" data-path="../developer/reconnect/buffer.html">
<a href="../developer/reconnect/buffer.html">
Buffering Messages During Reconnect Attempts
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.4" data-path="../developer/security/intro.html">
<a href="../developer/security/intro.html">
Securing Connections
</a>
<ul class="articles">
<li class="chapter " data-level="3.4.1" data-path="../developer/security/userpass.html">
<a href="../developer/security/userpass.html">
Authenticating with a User and Password
</a>
</li>
<li class="chapter " data-level="3.4.2" data-path="../developer/security/token.html">
<a href="../developer/security/token.html">
Authenticating with a Token
</a>
</li>
<li class="chapter " data-level="3.4.3" data-path="../developer/security/nkey.html">
<a href="../developer/security/nkey.html">
Authenticating with an NKey
</a>
</li>
<li class="chapter " data-level="3.4.4" data-path="../developer/security/creds.html">
<a href="../developer/security/creds.html">
Authenticating with a Credentials File
</a>
</li>
<li class="chapter " data-level="3.4.5" data-path="../developer/security/tls.html">
<a href="../developer/security/tls.html">
Encrypting Connections with TLS
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.5" data-path="../developer/receiving/intro.html">
<a href="../developer/receiving/intro.html">
Receiving Messages
</a>
<ul class="articles">
<li class="chapter " data-level="3.5.1" data-path="../developer/receiving/sync.html">
<a href="../developer/receiving/sync.html">
Synchronous Subscriptions
</a>
</li>
<li class="chapter " data-level="3.5.2" data-path="../developer/receiving/async.html">
<a href="../developer/receiving/async.html">
Asynchronous Subscriptions
</a>
</li>
<li class="chapter " data-level="3.5.3" data-path="../developer/receiving/unsubscribing.html">
<a href="../developer/receiving/unsubscribing.html">
Unsubscribing
</a>
</li>
<li class="chapter " data-level="3.5.4" data-path="../developer/receiving/unsub_after.html">
<a href="../developer/receiving/unsub_after.html">
Unsubscribing After N Messages
</a>
</li>
<li class="chapter " data-level="3.5.5" data-path="../developer/receiving/reply.html">
<a href="../developer/receiving/reply.html">
Replying to a Message
</a>
</li>
<li class="chapter " data-level="3.5.6" data-path="../developer/receiving/wildcards.html">
<a href="../developer/receiving/wildcards.html">
Wildcard Subscriptions
</a>
</li>
<li class="chapter " data-level="3.5.7" data-path="../developer/receiving/queues.html">
<a href="../developer/receiving/queues.html">
Queue Subscriptions
</a>
</li>
<li class="chapter " data-level="3.5.8" data-path="../developer/receiving/drain.html">
<a href="../developer/receiving/drain.html">
Draining Messages Before Disconnect
</a>
</li>
<li class="chapter " data-level="3.5.9" data-path="../developer/receiving/structure.html">
<a href="../developer/receiving/structure.html">
Structured Data
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.6" data-path="../developer/sending/intro.html">
<a href="../developer/sending/intro.html">
Sending Messages
</a>
<ul class="articles">
<li class="chapter " data-level="3.6.1" data-path="../developer/sending/replyto.html">
<a href="../developer/sending/replyto.html">
Including a Reply Subject
</a>
</li>
<li class="chapter " data-level="3.6.2" data-path="../developer/sending/request_reply.html">
<a href="../developer/sending/request_reply.html">
Request-Reply Semantics
</a>
</li>
<li class="chapter " data-level="3.6.3" data-path="../developer/sending/caches.html">
<a href="../developer/sending/caches.html">
Caches, Flush and Ping
</a>
</li>
<li class="chapter " data-level="3.6.4" data-path="../developer/sending/structure.html">
<a href="../developer/sending/structure.html">
Sending Structured Data
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.7" data-path="../developer/events/intro.html">
<a href="../developer/events/intro.html">
Monitoring the Connection
</a>
<ul class="articles">
<li class="chapter " data-level="3.7.1" data-path="../developer/events/events.html">
<a href="../developer/events/events.html">
Listen for Connection Events
</a>
</li>
<li class="chapter " data-level="3.7.2" data-path="../developer/events/slow.html">
<a href="../developer/events/slow.html">
Slow Consumers
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.8" data-path="../developer/tutorials/intro.html">
<a href="../developer/tutorials/intro.html">
Tutorials
</a>
<ul class="articles">
<li class="chapter " data-level="3.8.1" data-path="../developer/tutorials/pubsub.html">
<a href="../developer/tutorials/pubsub.html">
Explore NATS Pub/Sub
</a>
</li>
<li class="chapter " data-level="3.8.2" data-path="../developer/tutorials/reqreply.html">
<a href="../developer/tutorials/reqreply.html">
Explore NATS Request/Reply
</a>
</li>
<li class="chapter " data-level="3.8.3" data-path="../developer/tutorials/queues.html">
<a href="../developer/tutorials/queues.html">
Explore NATS Queueing
</a>
</li>
<li class="chapter " data-level="3.8.4" data-path="../developer/tutorials/custom_dialer.html">
<a href="../developer/tutorials/custom_dialer.html">
Advanced Connect and Custom Dialer in Go
</a>
</li>
</ul>
</li>
<li class="header">NATS Server</li>
<li class="chapter " data-level="4.1" data-path="../nats_server/installation.html">
<a href="../nats_server/installation.html">
Installing
</a>
</li>
<li class="chapter " data-level="4.2" data-path="../nats_server/running.html">
<a href="../nats_server/running.html">
Running
</a>
<ul class="articles">
<li class="chapter " data-level="4.2.1" data-path="../nats_server/windows_srv.html">
<a href="../nats_server/windows_srv.html">
Window Service
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4.3" data-path="../nats_server/clients.html">
<a href="../nats_server/clients.html">
Clients
</a>
</li>
<li class="chapter " data-level="4.4" data-path="../nats_server/flags.html">
<a href="../nats_server/flags.html">
Flags
</a>
</li>
<li class="chapter " data-level="4.5" data-path="../nats_server/configuration.html">
<a href="../nats_server/configuration.html">
Configuration
</a>
<ul class="articles">
<li class="chapter " data-level="4.5.1" data-path="../nats_server/securing_nats.html">
<a href="../nats_server/securing_nats.html">
Securing NATS
</a>
<ul class="articles">
<li class="chapter " data-level="4.5.1.1" data-path="../nats_server/tls.html">
<a href="../nats_server/tls.html">
Enabling TLS
</a>
</li>
<li class="chapter " data-level="4.5.1.2" data-path="../nats_server/auth_intro.html">
<a href="../nats_server/auth_intro.html">
Authentication
</a>
<ul class="articles">
<li class="chapter " data-level="4.5.1.2.1" data-path="../nats_server/tokens.html">
<a href="../nats_server/tokens.html">
Tokens
</a>
</li>
<li class="chapter " data-level="4.5.1.2.2" data-path="../nats_server/username_password.html">
<a href="../nats_server/username_password.html">
Username/Password
</a>
</li>
<li class="chapter " data-level="4.5.1.2.3" data-path="../nats_server/tls_mutual_auth.html">
<a href="../nats_server/tls_mutual_auth.html">
TLS Authentication
</a>
</li>
<li class="chapter " data-level="4.5.1.2.4" data-path="../nats_server/nkey_auth.html">
<a href="../nats_server/nkey_auth.html">
NKeys
</a>
</li>
<li class="chapter " data-level="4.5.1.2.5" data-path="../nats_server/accounts.html">
<a href="../nats_server/accounts.html">
Accounts
</a>
</li>
<li class="chapter " data-level="4.5.1.2.6" data-path="../nats_server/jwt_auth.html">
<a href="../nats_server/jwt_auth.html">
JWTs
</a>
</li>
<li class="chapter " data-level="4.5.1.2.7" data-path="../nats_server/auth_timeout.html">
<a href="../nats_server/auth_timeout.html">
Authentication Timeout
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4.5.1.3" data-path="../nats_server/authorization.html">
<a href="../nats_server/authorization.html">
Authorization
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4.5.2" data-path="../nats_server/clustering.html">
<a href="../nats_server/clustering.html">
Clustering
</a>
<ul class="articles">
<li class="chapter " data-level="4.5.2.1" data-path="../nats_server/cluster_config.html">
<a href="../nats_server/cluster_config.html">
Configuration
</a>
</li>
<li class="chapter " data-level="4.5.2.2" data-path="../nats_server/cluster_tls.html">
<a href="../nats_server/cluster_tls.html">
TLS Authentication
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4.5.3" data-path="../gateways/">
<a href="../gateways/">
Gateways
</a>
<ul class="articles">
<li class="chapter " data-level="4.5.3.1" data-path="../gateways/gateway.html">
<a href="../gateways/gateway.html">
Configuration
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4.5.4" data-path="../leafnodes/">
<a href="../leafnodes/">
Leaf Nodes
</a>
<ul class="articles">
<li class="chapter " data-level="4.5.4.1" data-path="../leafnodes/leafnode_conf.html">
<a href="../leafnodes/leafnode_conf.html">
Configuration
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4.5.5" data-path="../nats_server/logging.html">
<a href="../nats_server/logging.html">
Logging
</a>
</li>
<li class="chapter " data-level="4.5.6" data-path="../nats_server/monitoring.html">
<a href="../nats_server/monitoring.html">
Monitoring
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4.6" data-path="../nats_admin/">
<a href="../nats_admin/">
Managing A NATS Server
</a>
<ul class="articles">
<li class="chapter " data-level="4.6.1" data-path="../nats_admin/upgrading_cluster.html">
<a href="../nats_admin/upgrading_cluster.html">
Upgrading a Cluster
</a>
</li>
<li class="chapter " data-level="4.6.2" data-path="../nats_admin/slow_consumers.html">
<a href="../nats_admin/slow_consumers.html">
Slow Consumers
</a>
</li>
<li class="chapter " data-level="4.6.3" data-path="../nats_admin/signals.html">
<a href="../nats_admin/signals.html">
Signals
</a>
</li>
<li class="chapter " data-level="4.6.4" data-path="../sys_accounts/">
<a href="../sys_accounts/">
System Accounts
</a>
<ul class="articles">
<li class="chapter " data-level="4.6.4.1" data-path="../sys_accounts/sys_accounts.html">
<a href="../sys_accounts/sys_accounts.html">
Configuration
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="chapter " data-level="4.7" data-path="../nats_docker/">
<a href="../nats_docker/">
NATS and Docker
</a>
<ul class="articles">
<li class="chapter " data-level="4.7.1" data-path="../nats_docker/tutorial.html">
<a href="../nats_docker/tutorial.html">
Tutorial
</a>
</li>
<li class="chapter " data-level="4.7.2" data-path="../nats_docker/docker_swarm.html">
<a href="../nats_docker/docker_swarm.html">
Docker Swarm
</a>
</li>
</ul>
</li>
<li class="header">NATS Tools</li>
<li class="chapter " data-level="5.1" data-path="../nats_tools/mkpasswd.html">
<a href="../nats_tools/mkpasswd.html">
mkpasswd
</a>
</li>
<li class="chapter " data-level="5.2" data-path="../nats_tools/nk.html">
<a href="../nats_tools/nk.html">
nk
</a>
</li>
<li class="chapter " data-level="5.3" data-path="../nats_tools/nsc/">
<a href="../nats_tools/nsc/">
nsc
</a>
<ul class="articles">
<li class="chapter " data-level="5.3.1" data-path="../nats_tools/nsc/nsc.html">
<a href="../nats_tools/nsc/nsc.html">
Basics
</a>
</li>
<li class="chapter " data-level="5.3.2" data-path="../nats_tools/nsc/streams.html">
<a href="../nats_tools/nsc/streams.html">
Streams
</a>
</li>
<li class="chapter " data-level="5.3.3" data-path="../nats_tools/nsc/services.html">
<a href="../nats_tools/nsc/services.html">
Services
</a>
</li>
<li class="chapter " data-level="5.3.4" data-path="../nats_tools/nsc/signing_keys.html">
<a href="../nats_tools/nsc/signing_keys.html">
Signing Keys
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="5.4" data-path="../nats_tools/nas/">
<a href="../nats_tools/nas/">
nats-account-server
</a>
<ul class="articles">
<li class="chapter " data-level="5.4.1" data-path="../nats_tools/nas/nas_conf.html">
<a href="../nats_tools/nas/nas_conf.html">
Basics
</a>
</li>
<li class="chapter " data-level="5.4.2" data-path="../nats_tools/nas/inspecting_jwts.html">
<a href="../nats_tools/nas/inspecting_jwts.html">
Inspecting JWTs
</a>
</li>
<li class="chapter " data-level="5.4.3" data-path="../nats_tools/nas/dir_store.html">
<a href="../nats_tools/nas/dir_store.html">
Directory Store
</a>
</li>
<li class="chapter " data-level="5.4.4" data-path="../nats_tools/nas/mem_resolver.html">
<a href="../nats_tools/nas/mem_resolver.html">
Memory Resolver
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="5.5" data-path="../nats_tools/nats_top/">
<a href="../nats_tools/nats_top/">
nats-top
</a>
<ul class="articles">
<li class="chapter " data-level="5.5.1" data-path="../nats_tools/nats_top/tutorial.html">
<a href="../nats_tools/nats_top/tutorial.html">
Tutorial
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="5.6" data-path="../nats_tools/natsbench.html">
<a href="../nats_tools/natsbench.html">
nats-bench
</a>
</li>
<li class="header">NATS Streaming Concepts</li>
<li class="chapter " data-level="6.1" data-path="../nats_streaming/intro.html">
<a href="../nats_streaming/intro.html">
Introduction
</a>
<ul class="articles">
<li class="chapter " data-level="6.1.1" data-path="../nats_streaming/relation-to-nats.html">
<a href="../nats_streaming/relation-to-nats.html">
Relation to NATS
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="6.2" data-path="../nats_streaming/client-connections.html">
<a href="../nats_streaming/client-connections.html">
Client Connections
</a>
</li>
<li class="chapter " data-level="6.3" data-path="../nats_streaming/channels/channels.html">
<a href="../nats_streaming/channels/channels.html">
Channels
</a>
<ul class="articles">
<li class="chapter " data-level="6.3.1" data-path="../nats_streaming/channels/message-log.html">
<a href="../nats_streaming/channels/message-log.html">
Message Log
</a>
</li>
<li class="chapter " data-level="6.3.2" data-path="../nats_streaming/channels/subscriptions/subscriptions.html">
<a href="../nats_streaming/channels/subscriptions/subscriptions.html">
Subscriptions
</a>
<ul class="articles">
<li class="chapter " data-level="6.3.2.1" data-path="../nats_streaming/channels/subscriptions/regular.html">
<a href="../nats_streaming/channels/subscriptions/regular.html">
Regular
</a>
</li>
<li class="chapter " data-level="6.3.2.2" data-path="../nats_streaming/channels/subscriptions/durable.html">
<a href="../nats_streaming/channels/subscriptions/durable.html">
Durable
</a>
</li>
<li class="chapter " data-level="6.3.2.3" data-path="../nats_streaming/channels/subscriptions/queue-group.html">
<a href="../nats_streaming/channels/subscriptions/queue-group.html">
Queue Group
</a>
</li>
<li class="chapter " data-level="6.3.2.4" data-path="../nats_streaming/channels/subscriptions/redelivery.html">
<a href="../nats_streaming/channels/subscriptions/redelivery.html">
Redelivery
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="chapter " data-level="6.4" data-path="../nats_streaming/store-interface.html">
<a href="../nats_streaming/store-interface.html">
Store Interface
</a>
</li>
<li class="chapter " data-level="6.5" data-path="../nats_streaming/store-encryption.html">
<a href="../nats_streaming/store-encryption.html">
Store Encryption
</a>
</li>
<li class="chapter " data-level="6.6" data-path="../nats_streaming/clustering/clustering.html">
<a href="../nats_streaming/clustering/clustering.html">
Clustering
</a>
<ul class="articles">
<li class="chapter " data-level="6.6.1" data-path="../nats_streaming/clustering/supported-stores.html">
<a href="../nats_streaming/clustering/supported-stores.html">
Supported Stores
</a>
</li>
<li class="chapter " data-level="6.6.2" data-path="../nats_streaming/clustering/configuration.html">
<a href="../nats_streaming/clustering/configuration.html">
Configuration
</a>
</li>
<li class="chapter " data-level="6.6.3" data-path="../nats_streaming/clustering/auto-configuration.html">
<a href="../nats_streaming/clustering/auto-configuration.html">
Auto Configuration
</a>
</li>
<li class="chapter " data-level="6.6.4" data-path="../nats_streaming/clustering/containers.html">
<a href="../nats_streaming/clustering/containers.html">
Containers
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="6.7" data-path="../nats_streaming/fault-tolerance/ft.html">
<a href="../nats_streaming/fault-tolerance/ft.html">
Fault Tolerance
</a>
<ul class="articles">
<li class="chapter " data-level="6.7.1" data-path="../nats_streaming/fault-tolerance/active-server.html">
<a href="../nats_streaming/fault-tolerance/active-server.html">
Active Server
</a>
</li>
<li class="chapter " data-level="6.7.2" data-path="../nats_streaming/fault-tolerance/standby-server.html">
<a href="../nats_streaming/fault-tolerance/standby-server.html">
Standby Servers
</a>
</li>
<li class="chapter " data-level="6.7.3" data-path="../nats_streaming/fault-tolerance/shared-state.html">
<a href="../nats_streaming/fault-tolerance/shared-state.html">
Shared State
</a>
</li>
<li class="chapter " data-level="6.7.4" data-path="../nats_streaming/fault-tolerance/failover.html">
<a href="../nats_streaming/fault-tolerance/failover.html">
Failover
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="6.8" data-path="../nats_streaming/partitioning.html">
<a href="../nats_streaming/partitioning.html">
Partitioning
</a>
</li>
<li class="header">Developing With NATS Streaming</li>
<li class="chapter " data-level="7.1" data-path="../developer/streaming/">
<a href="../developer/streaming/">
Introduction
</a>
</li>
<li class="chapter " data-level="7.2" data-path="../developer/streaming/connecting.html">
<a href="../developer/streaming/connecting.html">
Connecting to NATS Streaming
</a>
</li>
<li class="chapter " data-level="7.3" data-path="../developer/streaming/publishing.html">
<a href="../developer/streaming/publishing.html">
Publishing to a Channel
</a>
</li>
<li class="chapter " data-level="7.4" data-path="../developer/streaming/receiving.html">
<a href="../developer/streaming/receiving.html">
Receiving Messages from a Channel
</a>
</li>
<li class="chapter " data-level="7.5" data-path="../developer/streaming/durables.html">
<a href="../developer/streaming/durables.html">
Durable Subscriptions
</a>
</li>
<li class="chapter " data-level="7.6" data-path="../developer/streaming/queues.html">
<a href="../developer/streaming/queues.html">
Queue Subscriptions
</a>
</li>
<li class="chapter " data-level="7.7" data-path="../developer/streaming/acks.html">
<a href="../developer/streaming/acks.html">
Acknowledgements
</a>
</li>
<li class="chapter " data-level="7.8" data-path="../developer/streaming/protocol.html">
<a href="../developer/streaming/protocol.html">
The Streaming Protocol
</a>
</li>
<li class="header">NATS Streaming Server</li>
<li class="chapter " data-level="8.1" data-path="../nats_streaming/gettingstarted/install.html">
<a href="../nats_streaming/gettingstarted/install.html">
Installing
</a>
</li>
<li class="chapter " data-level="8.2" data-path="../nats_streaming/gettingstarted/run.html">
<a href="../nats_streaming/gettingstarted/run.html">
Running
</a>
</li>
<li class="chapter " data-level="8.3" data-path="../nats_streaming/gettingstarted/configuring.html">
<a href="../nats_streaming/gettingstarted/configuring.html">
Configuring
</a>
</li>
<li class="chapter " data-level="8.4" data-path="../nats_streaming/gettingstarted/tls.html">
<a href="../nats_streaming/gettingstarted/tls.html">
Securing
</a>
</li>
<li class="chapter " data-level="8.5" data-path="../nats_streaming/gettingstarted/process-signaling.html">
<a href="../nats_streaming/gettingstarted/process-signaling.html">
Process Signaling
</a>
</li>
<li class="chapter " data-level="8.6" data-path="../nats_streaming/gettingstarted/windows-service.html">
<a href="../nats_streaming/gettingstarted/windows-service.html">
Windows Service
</a>
</li>
<li class="chapter " data-level="8.7" data-path="../developer/streaming/embedding.html">
<a href="../developer/streaming/embedding.html">
Embedding NATS Streaming Server
</a>
</li>
<li class="chapter " data-level="8.8" data-path="../nats_streaming/swarm.html">
<a href="../nats_streaming/swarm.html">
Docker Swarm
</a>
</li>
<li class="chapter " data-level="8.9" data-path="../nats_streaming/monitoring/monitoring.html">
<a href="../nats_streaming/monitoring/monitoring.html">
Monitoring
</a>
<ul class="articles">
<li class="chapter " data-level="8.9.1" data-path="../nats_streaming/monitoring/enabling.html">
<a href="../nats_streaming/monitoring/enabling.html">
Enabling
</a>
</li>
<li class="chapter " data-level="8.9.2" data-path="../nats_streaming/monitoring/endpoints.html">
<a href="../nats_streaming/monitoring/endpoints.html">
Endpoints
</a>
</li>
</ul>
</li>
<li class="header">NATS Protocol</li>
<li class="chapter " data-level="9.1" data-path="nats-protocol-demo.html">
<a href="nats-protocol-demo.html">
Protocol Demo
</a>
</li>
<li class="chapter active" data-level="9.2" data-path="nats-protocol.html">
<a href="nats-protocol.html">
Client Protocol
</a>
<ul class="articles">
<li class="chapter " data-level="9.2.1" data-path="nats-client-dev.html">
<a href="nats-client-dev.html">
Developing a Client
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="9.3" data-path="nats-server-protocol.html">
<a href="nats-server-protocol.html">
NATS Cluster Protocol
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >Client Protocol</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h2 id="client-protocol"><a name="client-protocol" class="plugin-anchor" href="#client-protocol"><i class="fa fa-link" aria-hidden="true"></i></a>Client Protocol</h2>
<p>The wire protocol used to communicate between the NATS server and clients is a simple, text-based publish/subscribe style protocol. Clients connect to and communicate with <code>nats-server</code> (the NATS server) through a regular TCP/IP socket using a small set of protocol operations that are terminated by a new line.</p>
<p>Unlike traditional messaging systems that use a binary message format that require an API to consume, the text-based NATS protocol makes it easy to implement clients in a wide variety of programming and scripting languages. In fact, refer to the topic <a href="nats-protocol-demo.html">NATS Protocol Demo</a> to play with the NATS protocol for yourself using telnet.</p>
<p>The NATS server implements a <a href="https://youtu.be/ylRKac5kSOk?t=10m46s" target="_blank">zero allocation byte parser</a> that is fast and efficient.</p>
<h2 id="protocol-conventions"><a name="protocol-conventions" class="plugin-anchor" href="#protocol-conventions"><i class="fa fa-link" aria-hidden="true"></i></a>Protocol conventions</h2>
<p><strong>Control line w/Optional Content</strong>: Each interaction between the client and server consists of a control, or protocol, line of text followed, optionally by message content. Most of the protocol messages don&apos;t require content, only <code>PUB</code> and <code>MSG</code> include payloads.</p>
<p><strong>Field Delimiters</strong>: The fields of NATS protocol messages are delimited by whitespace characters &apos;<code>`&apos; (space) or</code>\t` (tab). Multiple whitespace characters will be treated as a single field delimiter.</p>
<p><strong>Newlines</strong>: NATS uses <code>CR</code> followed by <code>LF</code> (<code>CR+LF</code>, <code>\r\n</code>, <code>0x0D0A</code>) to terminate protocol messages. This newline sequence is also used to mark the end of the message payload in a <code>PUB</code> or <code>MSG</code> protocol message.</p>
<p><strong>Subject names</strong>: Subject names, including reply subject (INBOX) names, are case-sensitive and must be non-empty alphanumeric strings with no embedded whitespace, and optionally token-delimited using the dot character (<code>.</code>), e.g.:</p>
<p><code>FOO</code>, <code>BAR</code>, <code>foo.bar</code>, <code>foo.BAR</code>, <code>FOO.BAR</code> and <code>FOO.BAR.BAZ</code> are all valid subject names</p>
<p><code>FOO. BAR</code>, <code>foo. .bar</code> and<code>foo..bar</code> are <em>not</em> valid subject names</p>
<p><strong>Character Encoding</strong>: Subject names should be ascii characters for maximum interoperability. Due to language constraints and performance, some clients may support UTF-8 subject names, as may the server. No guarantees of non-ASCII support are provided.</p>
<p><strong>Wildcards</strong>: NATS supports the use of wildcards in subject subscriptions.</p>
<ul>
<li>The asterisk character (<code>*</code>) matches a single token at any level of the subject.</li>
<li>The greater than symbol (<code>&gt;</code>), also known as the <em>full wildcard</em>, matches one or more tokens at the tail of a subject, and must be the last token. The wildcarded subject <code>foo.&gt;</code> will match <code>foo.bar</code> or <code>foo.bar.baz.1</code>, but not <code>foo</code>. </li>
<li>Wildcards must be a separate token (<code>foo.*.baz</code> or <code>foo.&gt;</code> are syntactically valid; <code>foo*.bar</code>, <code>f*o.b*r</code> and <code>foo&gt;</code> are not)</li>
</ul>
<p>For example, the wildcard subscriptions <code>foo.*.quux</code> and <code>foo.&gt;</code> both match <code>foo.bar.quux</code>, but only the latter matches <code>foo.bar.baz</code>. With the full wildcard,
it is also possible to express interest in every subject that may exist in NATS: <code>sub &gt; 1</code>, limited of course by authorization settings.</p>
<h2 id="protocol-messages"><a name="protocol-messages" class="plugin-anchor" href="#protocol-messages"><i class="fa fa-link" aria-hidden="true"></i></a>Protocol messages</h2>
<p>The following table briefly describes the NATS protocol messages. NATS protocol operation names are case insensitive, thus <code>SUB foo 1\r\n</code> and <code>sub foo 1\r\n</code> are equivalent.</p>
<p>Click the name to see more detailed information, including syntax:</p>
<table>
<thead>
<tr>
<th>OP Name</th>
<th style="text-align:left">Sent By</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#INFO"><code>INFO</code></a></td>
<td style="text-align:left">Server</td>
<td style="text-align:left">Sent to client after initial TCP/IP connection</td>
</tr>
<tr>
<td><a href="#CONNECT"><code>CONNECT</code></a></td>
<td style="text-align:left">Client</td>
<td style="text-align:left">Sent to server to specify connection information</td>
</tr>
<tr>
<td><a href="#PUB"><code>PUB</code></a></td>
<td style="text-align:left">Client</td>
<td style="text-align:left">Publish a message to a subject, with optional reply subject</td>
</tr>
<tr>
<td><a href="#SUB"><code>SUB</code></a></td>
<td style="text-align:left">Client</td>
<td style="text-align:left">Subscribe to a subject (or subject wildcard)</td>
</tr>
<tr>
<td><a href="#UNSUB"><code>UNSUB</code></a></td>
<td style="text-align:left">Client</td>
<td style="text-align:left">Unsubscribe (or auto-unsubscribe) from subject</td>
</tr>
<tr>
<td><a href="#MSG"><code>MSG</code></a></td>
<td style="text-align:left">Server</td>
<td style="text-align:left">Delivers a message payload to a subscriber</td>
</tr>
<tr>
<td><a href="#PINGPONG"><code>PING</code></a></td>
<td style="text-align:left">Both</td>
<td style="text-align:left">PING keep-alive message</td>
</tr>
<tr>
<td><a href="#PINGPONG"><code>PONG</code></a></td>
<td style="text-align:left">Both</td>
<td style="text-align:left">PONG keep-alive response</td>
</tr>
<tr>
<td><a href="#OKERR"><code>+OK</code></a></td>
<td style="text-align:left">Server</td>
<td style="text-align:left">Acknowledges well-formed protocol message in <code>verbose</code> mode</td>
</tr>
<tr>
<td><a href="#OKERR"><code>-ERR</code></a></td>
<td style="text-align:left">Server</td>
<td style="text-align:left">Indicates a protocol error. May cause client disconnect.</td>
</tr>
</tbody>
</table>
<p>The following sections explain each protocol message.</p>
<h2 id="info"><a name="info" class="plugin-anchor" href="#info"><i class="fa fa-link" aria-hidden="true"></i></a><a name="INFO"></a>INFO</h2>
<h4 id="description"><a name="description" class="plugin-anchor" href="#description"><i class="fa fa-link" aria-hidden="true"></i></a>Description</h4>
<p>As soon as the server accepts a connection from the client, it will send information about itself and the configuration and security requirements that are necessary for the client to successfully authenticate with the server and exchange messages.</p>
<p>When using the updated client protocol (see <a href="#CONNECT"><code>CONNECT</code></a> below), <code>INFO</code> messages can be sent anytime by the server. This means clients with that protocol level need to be able to asynchronously handle <code>INFO</code> messages.</p>
<h4 id="syntax"><a name="syntax" class="plugin-anchor" href="#syntax"><i class="fa fa-link" aria-hidden="true"></i></a>Syntax</h4>
<p><code>INFO {[&quot;option_name&quot;:option_value],...}</code></p>
<p>The valid options are as follows:</p>
<ul>
<li><code>server_id</code>: The unique identifier of the NATS server</li>
<li><code>version</code>: The version of the NATS server</li>
<li><code>go</code>: The version of golang the NATS server was built with</li>
<li><code>host</code>: The IP address used to start the NATS server, by default this will be <code>0.0.0.0</code> and can be configured with <code>-client_advertise host:port</code></li>
<li><code>port</code>: The port number the NATS server is configured to listen on</li>
<li><code>max_payload</code>: Maximum payload size, in bytes, that the server will accept from the client.</li>
<li><code>proto</code>: An integer indicating the protocol version of the server. The server version 1.2.0 sets this to <code>1</code> to indicate that it supports the &quot;Echo&quot; feature.</li>
<li><code>client_id</code>: An optional unsigned integer (64 bits) representing the internal client identifier in the server. This can be used to filter client connections in monitoring, correlate with error logs, etc...</li>
<li><code>auth_required</code>: If this is set, then the client should try to authenticate upon connect.</li>
<li><code>tls_required</code>: If this is set, then the client must perform the TLS/1.2 handshake. Note, this used to be <code>ssl_required</code> and has been updated along with the protocol from SSL to TLS.</li>
<li><code>tls_verify</code>: If this is set, the client must provide a valid certificate during the TLS handshake.</li>
<li><code>connect_urls</code> : An optional list of server urls that a client can connect to. </li>
</ul>
<h5 id="connecturls"><a name="connecturls" class="plugin-anchor" href="#connecturls"><i class="fa fa-link" aria-hidden="true"></i></a>connect_urls</h5>
<p>The <code>connect_urls</code> field is a list of urls the server may send when a client first connects, and when there are changes to server cluster topology. This field is considered optional, and may be omitted based on server configuration and client protocol level.</p>
<p>When a NATS server cluster expands, an <code>INFO</code> message is sent to the client with an updated <code>connect_urls</code> list. This cloud-friendly feature asynchronously notifies a client of known servers, allowing it to connect to servers not originally configured.</p>
<p>The <code>connect_urls</code> will contain a list of strings with an IP and port, looking like this: <code>&quot;connect_urls&quot;:[&quot;10.0.0.184:4333&quot;,&quot;192.168.129.1:4333&quot;,&quot;192.168.192.1:4333&quot;]</code></p>
<h4 id="example"><a name="example" class="plugin-anchor" href="#example"><i class="fa fa-link" aria-hidden="true"></i></a>Example</h4>
<p>Below you can see a sample connection string from a telnet connection to the <code>demo.nats.io</code> site.</p>
<pre class="language-"><code class="lang-sh">% telnet demo.nats.io 4222
Trying 107.170.221.32<span class="token punctuation">..</span>.
Connected to demo.nats.io.
Escape character is <span class="token string">&apos;^]&apos;</span><span class="token keyword">.</span>
INFO <span class="token punctuation">{</span><span class="token string">&quot;server_id&quot;</span><span class="token keyword">:</span><span class="token string">&quot;Zk0GQ3JBSrg3oyxCRRlE09&quot;</span>,<span class="token string">&quot;version&quot;</span><span class="token keyword">:</span><span class="token string">&quot;1.2.0&quot;</span>,<span class="token string">&quot;proto&quot;</span>:1,<span class="token string">&quot;go&quot;</span><span class="token keyword">:</span><span class="token string">&quot;go1.10.3&quot;</span>,<span class="token string">&quot;host&quot;</span><span class="token keyword">:</span><span class="token string">&quot;0.0.0.0&quot;</span>,<span class="token string">&quot;port&quot;</span>:4222,<span class="token string">&quot;max_payload&quot;</span>:1048576,<span class="token string">&quot;client_id&quot;</span>:2392<span class="token punctuation">}</span>
</code></pre>
<h2 id="connect"><a name="connect" class="plugin-anchor" href="#connect"><i class="fa fa-link" aria-hidden="true"></i></a><a name="CONNECT"></a>CONNECT</h2>
<h4 id="description"><a name="description" class="plugin-anchor" href="#description"><i class="fa fa-link" aria-hidden="true"></i></a>Description</h4>
<p>The <code>CONNECT</code> message is the client version of the <code>INFO</code> message. Once the client has established a TCP/IP socket connection with the NATS server, and an <code>INFO</code> message has been received from the server, the client may send a <code>CONNECT</code> message to the NATS server to provide more information about the current connection as well as security information.</p>
<h4 id="syntax"><a name="syntax" class="plugin-anchor" href="#syntax"><i class="fa fa-link" aria-hidden="true"></i></a>Syntax</h4>
<p><code>CONNECT {[&quot;option_name&quot;:option_value],...}</code></p>
<p>The valid options are as follows:</p>
<ul>
<li><code>verbose</code>: Turns on <a href="#OKERR"><code>+OK</code></a> protocol acknowledgements.</li>
<li><code>pedantic</code>: Turns on additional strict format checking, e.g. for properly formed subjects</li>
<li><code>tls_required</code>: Indicates whether the client requires an SSL connection.</li>
<li><code>auth_token</code>: Client authorization token (if <code>auth_required</code> is set)</li>
<li><code>user</code>: Connection username (if <code>auth_required</code> is set)</li>
<li><code>pass</code>: Connection password (if <code>auth_required</code> is set)</li>
<li><code>name</code>: Optional client name</li>
<li><code>lang</code>: The implementation language of the client.</li>
<li><code>version</code>: The version of the client.</li>
<li><code>protocol</code>: <em>optional int</em>. Sending <code>0</code> (or absent) indicates client supports original protocol. Sending <code>1</code> indicates that the client supports dynamic reconfiguration of cluster topology changes by asynchronously receiving <code>INFO</code> messages with known servers it can reconnect to.</li>
<li><code>echo</code>: Optional boolean. If set to <code>true</code>, the server (version 1.2.0+) will not send originating messages from this connection to its own subscriptions. Clients should set this to <code>true</code> only for server supporting this feature, which is when <code>proto</code> in the <code>INFO</code> protocol is set to at least <code>1</code>.</li>
</ul>
<h4 id="example"><a name="example" class="plugin-anchor" href="#example"><i class="fa fa-link" aria-hidden="true"></i></a>Example</h4>
<p>Here is an example from the default string of the Go client:</p>
<pre class="language-"><code>[CONNECT {&quot;verbose&quot;:false,&quot;pedantic&quot;:false,&quot;tls_required&quot;:false,&quot;name&quot;:&quot;&quot;,&quot;lang&quot;:&quot;go&quot;,&quot;version&quot;:&quot;1.2.2&quot;,&quot;protocol&quot;:1}]\r\n
</code></pre><p>Most clients set <code>verbose</code> to <code>false</code> by default. This means that the server should not confirm each message it receives on this connection with a <code>+OK</code> back to the client.</p>
<h2 id="pub"><a name="pub" class="plugin-anchor" href="#pub"><i class="fa fa-link" aria-hidden="true"></i></a><a name="PUB"></a>PUB</h2>
<h4 id="description"><a name="description" class="plugin-anchor" href="#description"><i class="fa fa-link" aria-hidden="true"></i></a>Description</h4>
<p>The <code>PUB</code> message publishes the message payload to the given subject name, optionally supplying a reply subject. If a reply subject is supplied, it will be delivered to eligible subscribers along with the supplied payload. Note that the payload itself is optional. To omit the payload, set the payload size to 0, but the second CRLF is still required.</p>
<h4 id="syntax"><a name="syntax" class="plugin-anchor" href="#syntax"><i class="fa fa-link" aria-hidden="true"></i></a>Syntax</h4>
<p><code>PUB <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>subject</span><span class="token punctuation">&gt;</span></span> [reply-to] <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>#bytes</span><span class="token punctuation">&gt;</span></span>\r\n[payload]\r\n</code></p>
<p>where:</p>
<ul>
<li><code>subject</code>: The destination subject to publish to</li>
<li><code>reply-to</code>: The optional reply inbox subject that subscribers can use to send a response back to the publisher/requestor</li>
<li><code>#bytes</code>: The payload size in bytes</li>
<li><code>payload</code>: The message payload data</li>
</ul>
<h4 id="example"><a name="example" class="plugin-anchor" href="#example"><i class="fa fa-link" aria-hidden="true"></i></a>Example</h4>
<p>To publish the ASCII string message payload &quot;Hello NATS!&quot; to subject FOO:</p>
<p><code>PUB FOO 11\r\nHello NATS!\r\n</code></p>
<p>To publish a request message &quot;Knock Knock&quot; to subject FRONT.DOOR with reply subject INBOX.22:</p>
<p><code>PUB FRONT.DOOR INBOX.22 11\r\nKnock Knock\r\n</code></p>
<p>To publish an empty message to subject NOTIFY:</p>
<p><code>PUB NOTIFY 0\r\n\r\n</code></p>
<h2 id="sub"><a name="sub" class="plugin-anchor" href="#sub"><i class="fa fa-link" aria-hidden="true"></i></a><a name="SUB"></a>SUB</h2>
<h4 id="description"><a name="description" class="plugin-anchor" href="#description"><i class="fa fa-link" aria-hidden="true"></i></a>Description</h4>
<p><code>SUB</code> initiates a subscription to a subject, optionally joining a distributed queue group.</p>
<h4 id="syntax"><a name="syntax" class="plugin-anchor" href="#syntax"><i class="fa fa-link" aria-hidden="true"></i></a>Syntax</h4>
<p><code>SUB <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>subject</span><span class="token punctuation">&gt;</span></span> [queue group] <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>sid</span><span class="token punctuation">&gt;</span></span>\r\n</code></p>
<p>where:</p>
<ul>
<li><code>subject</code>: The subject name to subscribe to</li>
<li><code>queue group</code>: If specified, the subscriber will join this queue group</li>
<li><code>sid</code>: A unique alphanumeric subscription ID, generated by the client</li>
</ul>
<h4 id="example"><a name="example" class="plugin-anchor" href="#example"><i class="fa fa-link" aria-hidden="true"></i></a>Example</h4>
<p>To subscribe to the subject <code>FOO</code> with the connection-unique subscription identifier (sid) <code>1</code>:</p>
<p><code>SUB FOO 1\r\n</code></p>
<p>To subscribe the current connection to the subject <code>BAR</code> as part of distribution queue group <code>G1</code> with sid <code>44</code>:</p>
<p><code>SUB BAR G1 44\r\n</code></p>
<h2 id="unsub"><a name="unsub" class="plugin-anchor" href="#unsub"><i class="fa fa-link" aria-hidden="true"></i></a><a name="UNSUB"></a>UNSUB</h2>
<h4 id="description"><a name="description" class="plugin-anchor" href="#description"><i class="fa fa-link" aria-hidden="true"></i></a>Description</h4>
<p><code>UNSUB</code> unsubcribes the connection from the specified subject, or auto-unsubscribes after the specified number of messages has been received.</p>
<h4 id="syntax"><a name="syntax" class="plugin-anchor" href="#syntax"><i class="fa fa-link" aria-hidden="true"></i></a>Syntax</h4>
<p><code>UNSUB <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>sid</span><span class="token punctuation">&gt;</span></span> [max_msgs]</code></p>
<p>where:</p>
<ul>
<li><code>sid</code>: The unique alphanumeric subscription ID of the subject to unsubscribe from</li>
<li><code>max_msgs</code>: An optional number of messages to wait for before automatically unsubscribing</li>
</ul>
<h4 id="example"><a name="example" class="plugin-anchor" href="#example"><i class="fa fa-link" aria-hidden="true"></i></a>Example</h4>
<p>The following examples concern subject <code>FOO</code> which has been assigned sid <code>1</code>. To unsubscribe from <code>FOO</code>:</p>
<p><code>UNSUB 1\r\n</code></p>
<p>To auto-unsubscribe from <code>FOO</code> after 5 messages have been received:</p>
<p><code>UNSUB 1 5\r\n</code></p>
<h2 id="msg"><a name="msg" class="plugin-anchor" href="#msg"><i class="fa fa-link" aria-hidden="true"></i></a><a name="MSG"></a>MSG</h2>
<h4 id="description"><a name="description" class="plugin-anchor" href="#description"><i class="fa fa-link" aria-hidden="true"></i></a>Description</h4>
<p>The <code>MSG</code> protocol message is used to deliver an application message to the client.</p>
<h4 id="syntax"><a name="syntax" class="plugin-anchor" href="#syntax"><i class="fa fa-link" aria-hidden="true"></i></a>Syntax</h4>
<p><code>MSG <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>subject</span><span class="token punctuation">&gt;</span></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>sid</span><span class="token punctuation">&gt;</span></span> [reply-to] <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>#bytes</span><span class="token punctuation">&gt;</span></span>\r\n[payload]\r\n</code></p>
<p>where:</p>
<ul>
<li><code>subject</code>: Subject name this message was received on</li>
<li><code>sid</code>: The unique alphanumeric subscription ID of the subject</li>
<li><code>reply-to</code>: The inbox subject on which the publisher is listening for responses</li>
<li><code>#bytes</code>: Size of the payload in bytes</li>
<li><code>payload</code>: The message payload data</li>
</ul>
<h4 id="example"><a name="example" class="plugin-anchor" href="#example"><i class="fa fa-link" aria-hidden="true"></i></a>Example</h4>
<p>The following message delivers an application message from subject <code>FOO.BAR</code>:</p>
<p><code>MSG FOO.BAR 9 11\r\nHello World\r\n</code></p>
<p>To deliver the same message along with a reply inbox:</p>
<p><code>MSG FOO.BAR 9 INBOX.34 11\r\nHello World\r\n</code></p>
<h2 id="pingpong"><a name="pingpong" class="plugin-anchor" href="#pingpong"><i class="fa fa-link" aria-hidden="true"></i></a><a name="PINGPONG"></a>PING/PONG</h2>
<h4 id="description"><a name="description" class="plugin-anchor" href="#description"><i class="fa fa-link" aria-hidden="true"></i></a>Description</h4>
<p><code>PING</code> and <code>PONG</code> implement a simple keep-alive mechanism between client and server. Once a client establishes a connection to the NATS server, the server will continuously send <code>PING</code> messages to the client at a configurable interval. If the client fails to respond with a <code>PONG</code> message within the configured response interval, the server will terminate its connection. If your connection stays idle for too long, it is cut off.</p>
<p>If the server sends a ping request, you can reply with a pong message to notify the server that you are still interested. You can also ping the server and will receive a pong reply. The ping/pong interval is configurable.</p>
<p>The server uses normal traffic as a ping/pong proxy, so a client that has messages flowing may not receive a ping from the server.</p>
<h4 id="syntax"><a name="syntax" class="plugin-anchor" href="#syntax"><i class="fa fa-link" aria-hidden="true"></i></a>Syntax</h4>
<p><code>PING\r\n</code></p>
<p><code>PONG\r\n</code></p>
<h4 id="example"><a name="example" class="plugin-anchor" href="#example"><i class="fa fa-link" aria-hidden="true"></i></a>Example</h4>
<p>The following example shows the demo server pinging the client and finally shutting it down.</p>
<pre class="language-"><code>telnet demo.nats.io 4222
Trying 107.170.221.32...
Connected to demo.nats.io.
Escape character is &apos;^]&apos;.
INFO {&quot;server_id&quot;:&quot;Zk0GQ3JBSrg3oyxCRRlE09&quot;,&quot;version&quot;:&quot;1.2.0&quot;,&quot;proto&quot;:1,&quot;go&quot;:&quot;go1.10.3&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:4222,&quot;max_payload&quot;:1048576,&quot;client_id&quot;:2392}
PING
PING
-ERR &apos;Stale Connection&apos;
Connection closed by foreign host.
</code></pre><h2 id="okerr"><a name="okerr" class="plugin-anchor" href="#okerr"><i class="fa fa-link" aria-hidden="true"></i></a><a name="OKERR"></a>+OK/ERR</h2>
<h4 id="description"><a name="description" class="plugin-anchor" href="#description"><i class="fa fa-link" aria-hidden="true"></i></a>Description</h4>
<p>When the <code>verbose</code> connection option is set to <code>true</code> (the default value), the server acknowledges each well-formed protocol message from the client with a <code>+OK</code> message. Most NATS clients set the <code>verbose</code> option to <code>false</code> using the <a href="#CONNECT">CONNECT</a> message</p>
<p>The <code>-ERR</code> message is used by the server indicate a protocol, authorization, or other runtime connection error to the client. Most of these errors result in the server closing the connection.</p>
<p>Handling of these errors usually has to be done asynchronously.</p>
<h4 id="syntax"><a name="syntax" class="plugin-anchor" href="#syntax"><i class="fa fa-link" aria-hidden="true"></i></a>Syntax</h4>
<p><code>+OK</code></p>
<p><code>-ERR <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>error</span> <span class="token attr-name">message</span><span class="token punctuation">&gt;</span></span></code></p>
<p>Some protocol errors result in the server closing the connection. Upon recieving these errors, the connection is no longer valid and the client should clean up relevant resources. These errors include:</p>
<ul>
<li><code>-ERR &apos;Unknown Protocol Operation&apos;</code>: Unknown protocol error</li>
<li><code>-ERR &apos;Attempted To Connect To Route Port&apos;</code>: Client attempted to connect to a route port instead of the client port</li>
<li><code>-ERR &apos;Authorization Violation&apos;</code>: Client failed to authenticate to the server with credentials specified in the <a href="#CONNECT">CONNECT</a> message</li>
<li><code>-ERR &apos;Authorization Timeout&apos;</code>: Client took too long to authenticate to the server after establishing a connection (default 1 second)</li>
<li><code>-ERR &apos;Invalid Client Protocol&apos;</code>: Client specified an invalid protocol version in the <a href="#CONNECT">CONNECT</a> message</li>
<li><code>-ERR &apos;Maximum Control Line Exceeded&apos;</code>: Message destination subject and reply subject length exceeded the maximum control line value specified by the <code>max_control_line</code> server option. The default is 1024 bytes.</li>
<li><code>-ERR &apos;Parser Error&apos;</code>: Cannot parse the protocol message sent by the client</li>
<li><code>-ERR &apos;Secure Connection - TLS Required&apos;</code>: The server requires TLS and the client does not have TLS enabled.</li>
<li><code>-ERR &apos;Stale Connection&apos;</code>: The server hasn&apos;t received a message from the client, including a <code>PONG</code> in too long.</li>
<li><code>-ERR &apos;Maximum Connections Exceeded</code>&apos;: This error is sent by the server when creating a new connection and the server has exceeded the maximum number of connections specified by the <code>max_connections</code> server option. The default is 64k.</li>
<li><code>-ERR &apos;Slow Consumer&apos;</code>: The server pending data size for the connection has reached the maximum size (default 10MB).</li>
<li><code>-ERR &apos;Maximum Payload Violation&apos;</code>: Client attempted to publish a message with a payload size that exceeds the <code>max_payload</code> size configured on the server. This value is supplied to the client upon connection in the initial <a href="#INFO"><code>INFO</code></a> message. The client is expected to do proper accounting of byte size to be sent to the server in order to handle this error synchronously.</li>
</ul>
<p>Protocol error messages where the connection remains open are listed below. The client should not close the connection in these cases.</p>
<ul>
<li><code>-ERR &apos;Invalid Subject&apos;</code>: Client sent a malformed subject (e.g. <code>sub foo. 90</code>)</li>
<li><code>-ERR &apos;Permissions Violation for Subscription to <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>subject</span><span class="token punctuation">&gt;</span></span>&apos;</code>: The user specified in the <a href="#CONNECT">CONNECT</a> message does not have permission to subscribe to the subject.</li>
<li><code>-ERR &apos;Permissions Violation for Publish to <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>subject</span><span class="token punctuation">&gt;</span></span>&apos;</code>: The user specified in the <a href="#CONNECT">CONNECT</a> message does not have permissions to publish to the subject.</li>
</ul>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="nats-protocol-demo.html" class="navigation navigation-prev " aria-label="Previous page: Protocol Demo">
<i class="fa fa-angle-left"></i>
</a>
<a href="nats-client-dev.html" class="navigation navigation-next " aria-label="Next page: Developing a Client">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Client Protocol","level":"9.2","depth":1,"next":{"title":"Developing a Client","level":"9.2.1","depth":2,"path":"nats_protocol/nats-client-dev.md","ref":"nats_protocol/nats-client-dev.md","articles":[]},"previous":{"title":"Protocol Demo","level":"9.1","depth":1,"path":"nats_protocol/nats-protocol-demo.md","ref":"nats_protocol/nats-protocol-demo.md","articles":[]},"dir":"ltr"},"config":{"plugins":["prism","-sharing","-highlight","include-html","toggle-chapters","anchors"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"prism":{"lang":{"ascii":"markup","text":"markup"}},"include-html":{},"toggle-chapters":{},"anchors":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"The NATS Maintainers","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"NATS","gitbook":"*","description":"Administrative, developer and conceptual documentation for the NATS messaging system."},"file":{"path":"nats_protocol/nats-protocol.md","mtime":"2019-05-30T22:52:52.322Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-05-31T18:29:35.398Z"},"basePath":"..","book":{"language":""}});
});
</script>
</div>
<!-- Viz Support -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/viz.js/2.1.2/viz.js"> </script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/viz.js/2.1.2/lite.render.js"> </script>
<!-- Site code -->
<script>
function flash(elem, text, speed) {
if (!elem) {
return;
}
var s = elem.style;
elem.textContent = text;
s.display = 'block';
s.opacity = 1;
(function fade() {
(s.opacity -= .1) < .1 ? s.display = "none" : setTimeout(fade, speed)
})();
}
function copyToClipboard(text, el) {
var copyTest = document.queryCommandSupported('copy');
var elOriginalText = el.getAttribute('data-original-title');
if (copyTest === true) {
var copyTextArea = document.createElement("textarea");
copyTextArea.value = text;
document.body.appendChild(copyTextArea);
copyTextArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'Copied!' : 'Whoops, not copied!';
var parent = el.parentNode.parentNode;
var msgElem = parent.querySelector(".copy-msg");
flash(msgElem, msg, 100);
} catch (err) {
console.log('Oops, unable to copy', err);
}
document.body.removeChild(copyTextArea);
el.setAttribute('data-original-title', elOriginalText);
} else {
// Fallback if browser doesn't support .execCommand('copy')
window.prompt("Copy to clipboard: Ctrl+C or Command+C, Enter", text);
}
}
function processGraphVizSections(elements) {
var elements = document.querySelectorAll("[data-viz]");
var viz = new Viz();
Array.prototype.forEach.call(elements, function (x) {
var engine = x.getAttribute("data-viz");
var image = viz.renderImageElement(x.innerText, {
format: "png",
engine: engine
}).then(function (element) {
x.parentNode.insertBefore(element, x);
});
});
};
function updateLanguageParameter(value) {
const param = "lang";
if (window.location.href.indexOf("?") >= 0) {
const regExp = new RegExp(param + "(.+?)(&|$)", "g");
const newUrl = window.location.href.replace(regExp, param + "=" + encodeURIComponent(value) + "$2");
window.history.pushState("", "", newUrl);
} else {
const newUrl = window.location.href + "?" + param + "=" + encodeURIComponent(value);
window.history.pushState("", "", newUrl);
}
}
function getLanguageParameter() {
var match = RegExp('[?&]lang=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
function docReady() {
window.gitbook.events.bind("page.change", function () {
pageChanged();
});
}
function pageChanged() {
var logos = document.querySelectorAll(".nats");
if (logos && logos.length == 0) {
gitbook.toolbar.createButton({
icon: 'nats',
label: 'NATS Home Page',
position: 'right',
onClick: function () {
var win = window.open("https://nats.io/", '_blank');
win.focus();
}
});
}
document.querySelectorAll('.js-copy').forEach(elem => {
elem.addEventListener("click", function () {
var el = this;
var parent = this.parentNode.parentNode;
var code = parent.querySelector('code');
var text = code.textContent || code.innerText;
copyToClipboard(text, el);
})
});
document.querySelectorAll('.api-lang').forEach(elem => {
elem.addEventListener("click", function () {
var curLang = sessionStorage.getItem('nats-api-language');
var lang = this.getAttribute('data-language');
// Stop the infinite loop
if (curLang == lang) {
return;
}
sessionStorage.setItem('nats-api-language', lang); // So we only do this 1x
updateLanguageParameter(lang)
document.querySelectorAll('.api-lang[data-language=' + lang + ']').forEach(elem => {
elem.click();
});
})
});
if (sessionStorage) {
var curLang = sessionStorage.getItem('nats-api-language');
var queryLang = getLanguageParameter();
var lang = curLang;
if (queryLang) { // query takes precedent
lang = queryLang
}
if (lang) {
document.querySelectorAll('.api-lang[data-language=' + lang + ']').forEach(elem => {
elem.click();
});
}
}
setTimeout(function () {
processGraphVizSections();
}, 1);
}
if (document.readyState != 'loading') docReady();
else if (document.addEventListener) document.addEventListener('DOMContentLoaded', docReady);
else document.attachEvent('onreadystatechange', function () {
if (document.readyState == 'complete') docReady();
});
</script>
<!-- Github Buttons -->
<script async defer src="https://buttons.github.io/buttons.js"></script>
<!-- Styles -->
<style>
.nats {
display: inline-block;
width: 16px;
height: 16px;
background: url(https://nats.io/img/nats-icon-color.svg);
background-size: 16px 16px;
background-repeat: no-repeat;
}
div.graphviz {
background: transparent;
border: 0;
padding-top: 15px;
padding-right: 15px;
padding-bottom: 25px;
padding-left: 15px;
text-align: center;
}
code[data-viz] {
display: none;
}
.tab-wrap {
transition: 0.3s box-shadow ease;
border-radius: 6px;
max-width: 100%;
display: flex;
flex-wrap: wrap;
position: relative;
list-style: none;
background-color: #fff;
margin: 20px 0;
/* box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);*/
}
.tab-wrap:hover {
box-shadow: 0 12px 23px rgba(0, 0, 0, 0.23), 0 10px 10px rgba(0, 0, 0, 0.19);
}
.tab {
display: none;
}
/* Using scss these would be generated, we have to manually create enough for all tabs, start with 8 */
.tab:checked:nth-of-type(1)~.tab__content:nth-of-type(1) {
opacity: 1;
transition: 0.5s opacity ease-in, 0.8s -webkit-transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease, 0.8s -webkit-transform ease;
position: relative;
top: 0;
z-index: 100;
-webkit-transform: translateY(0px);
transform: translateY(0px);
text-shadow: 0 0 0;
}
.tab:checked:nth-of-type(2)~.tab__content:nth-of-type(2) {
opacity: 1;
transition: 0.5s opacity ease-in, 0.8s -webkit-transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease, 0.8s -webkit-transform ease;
position: relative;
top: 0;
z-index: 100;
-webkit-transform: translateY(0px);
transform: translateY(0px);
text-shadow: 0 0 0;
}
.tab:checked:nth-of-type(3)~.tab__content:nth-of-type(3) {
opacity: 1;
transition: 0.5s opacity ease-in, 0.8s -webkit-transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease, 0.8s -webkit-transform ease;
position: relative;
top: 0;
z-index: 100;
-webkit-transform: translateY(0px);
transform: translateY(0px);
text-shadow: 0 0 0;
}
.tab:checked:nth-of-type(4)~.tab__content:nth-of-type(4) {
opacity: 1;
transition: 0.5s opacity ease-in, 0.8s -webkit-transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease, 0.8s -webkit-transform ease;
position: relative;
top: 0;
z-index: 100;
-webkit-transform: translateY(0px);
transform: translateY(0px);
text-shadow: 0 0 0;
}
.tab:checked:nth-of-type(5)~.tab__content:nth-of-type(5) {
opacity: 1;
transition: 0.5s opacity ease-in, 0.8s -webkit-transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease, 0.8s -webkit-transform ease;
position: relative;
top: 0;
z-index: 100;
-webkit-transform: translateY(0px);
transform: translateY(0px);
text-shadow: 0 0 0;
}
.tab:checked:nth-of-type(6)~.tab__content:nth-of-type(6) {
opacity: 1;
transition: 0.5s opacity ease-in, 0.8s -webkit-transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease, 0.8s -webkit-transform ease;
position: relative;
top: 0;
z-index: 100;
-webkit-transform: translateY(0px);
transform: translateY(0px);
text-shadow: 0 0 0;
}
.tab:checked:nth-of-type(7)~.tab__content:nth-of-type(7) {
opacity: 1;
transition: 0.5s opacity ease-in, 0.8s -webkit-transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease, 0.8s -webkit-transform ease;
position: relative;
top: 0;
z-index: 100;
-webkit-transform: translateY(0px);
transform: translateY(0px);
text-shadow: 0 0 0;
}
.tab:checked:nth-of-type(8)~.tab__content:nth-of-type(8) {
opacity: 1;
transition: 0.5s opacity ease-in, 0.8s -webkit-transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease;
transition: 0.5s opacity ease-in, 0.8s transform ease, 0.8s -webkit-transform ease;
position: relative;
top: 0;
z-index: 100;
-webkit-transform: translateY(0px);
transform: translateY(0px);
text-shadow: 0 0 0;
}
.tab:first-of-type:not(:last-of-type)+label {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.tab:not(:first-of-type):not(:last-of-type)+label {
border-radius: 0;
}
.tab:last-of-type:not(:first-of-type)+label {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.tab:checked+label {
background-color: #fff;
box-shadow: 0 -1px 0 #fff inset;
cursor: default;
font-weight: bold;
border: 1px solid #ddd;
border-bottom-color: transparent;
}
.tab:checked+label:hover {
box-shadow: 0 -1px 0 #fff inset;
background-color: #fff;
}
.tab+label {
box-shadow: 0 -1px 0 #eee inset;
border-radius: 6px 6px 0 0;
cursor: pointer;
display: block;
text-decoration: none;
color: #27aae1;
flex-grow: 3;
text-align: center;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
text-align: center;
transition: 0.3s background-color ease, 0.3s box-shadow ease;
height: 50px;
box-sizing: border-box;
padding: 15px;
}
.tab+label:hover {
background-color: #f9f9f9;
box-shadow: 0 1px 0 #f4f4f4 inset;
}
.tab__content {
padding: 2px 2px;
background-color: transparent;
position: absolute;
width: 100%;
z-index: -1;
opacity: 0;
left: 0;
-webkit-transform: translateY(-3px);
transform: translateY(-3px);
border-radius: 6px;
}
.tab__content pre {
margin-bottom: 0px !important;
}
.toolbar-icons {
display: inline-block;
position: relative;
padding-left: 4px;
}
a.toolbar-icons {
text-decoration: none;
}
.toolbar-icons .mdi {
color: #4183c4;
}
.copy-msg {
color: #4183c4;
}
.pull-right {
float: right !important;
}
.pull-left {
float: left !important;
}
</style>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
<script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="../gitbook/gitbook-plugin-search/search.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>