nats.py¶
Python3 client for the NATS messaging system.
Getting Started¶
pip install nats-py
You can also optionally install nkeys in order to use the NATS v2.0 decentralized auth features using JWTs.
pip install nats-py[nkeys]
NATS Hello World in Python 🐍
import asyncio
import nats
async def main():
# Connect to NATS!
nc = await nats.connect("demo.nats.io")
# Receive messages on 'foo'
sub = await nc.subscribe("foo")
# Publish a message to 'foo'
await nc.publish("foo", b'Hello from Python!')
# Process a message
msg = await sub.next_msg()
print("Received:", msg)
# Make sure all published messages have reached the server
await nc.flush()
# Close NATS connection
await nc.close()
if __name__ == '__main__':
asyncio.run(main())
Documentation
- Modules
connect()- Asyncio Client
- JetStream
jetstream()publish()subscribe()pull_subscribe()pull_subscribe_bind()PullSubscription- Manager
JetStreamManagerJetStreamManager.find_stream_name_by_subject()JetStreamManager.stream_info()JetStreamManager.add_stream()JetStreamManager.update_stream()JetStreamManager.delete_stream()JetStreamManager.purge_stream()JetStreamManager.streams_info()JetStreamManager.streams_info_iterator()JetStreamManager.consumers_info()JetStreamManager.get_msg()JetStreamManager.delete_msg()JetStreamManager.get_last_msg()
- KeyValue
- API
HeaderStatusCodeBasePubAckPlacementExternalStreamStreamSourceStreamSourceInfoLostStreamDataStreamStateRetentionPolicyStorageTypeDiscardPolicyStoreCompressionRePublishSubjectTransformStreamConfigPeerInfoClusterInfoStreamInfoStreamsListIteratorAckPolicyDeliverPolicyReplayPolicyConsumerConfigSequenceInfoConsumerInfoAccountLimitsTierAPIStatsAccountInfoRawStreamMsgKeyValueConfigStreamPurgeRequestObjectStoreConfigObjectLinkObjectMetaOptionsObjectMetaObjectInfo
- NUID
- Errors
ErrorTimeoutErrorNoRespondersErrorStaleConnectionErrorOutboundBufferLimitErrorUnexpectedEOFFlushTimeoutErrorConnectionClosedErrorSecureConnRequiredErrorSecureConnWantedErrorSecureConnFailedErrorBadSubscriptionErrorBadSubjectErrorSlowConsumerErrorBadTimeoutErrorAuthorizationErrorNoServersErrorJsonParseErrorMaxPayloadErrorDrainTimeoutErrorConnectionDrainingErrorConnectionReconnectingErrorInvalidUserCredentialsErrorInvalidCallbackTypeErrorProtocolErrorNotJSMessageErrorMsgAlreadyAckdError- JetStream Errors
ErrorAPIErrorServiceUnavailableErrorServerErrorNotFoundErrorBadRequestErrorNoStreamResponseErrorTooManyStalledMsgsErrorFetchTimeoutErrorConsumerSequenceMismatchErrorBucketNotFoundErrorBadBucketErrorKeyValueErrorKeyDeletedErrorKeyNotFoundErrorKeyWrongLastSequenceErrorNoKeysErrorKeyHistoryTooLargeErrorInvalidBucketNameErrorInvalidObjectNameErrorBadObjectMetaErrorLinkIsABucketErrorDigestMismatchErrorObjectNotFoundErrorObjectDeletedErrorObjectAlreadyExists
- Deprecated Errors
NatsErrorErrConnectionClosedErrDrainTimeoutErrInvalidUserCredentialsErrInvalidCallbackTypeErrConnectionReconnectingErrConnectionDrainingErrMaxPayloadErrStaleConnectionErrJsonParseErrSecureConnRequiredErrSecureConnWantedErrSecureConnFailedErrBadSubscriptionErrBadSubjectErrSlowConsumerErrTimeoutErrBadTimeoutErrAuthorizationErrNoServers
- Releases
- Guides
Site¶
License¶
Unless otherwise noted, the NATS source files are distributed under the Apache Version 2.0 license found in the LICENSE file.