Package: django-nats-client
Django NATS ClientImportant Notes🚨 The latest major version of this package,v0.4.0, has a numerous breaking changes.
Please review itsrelease note.FeaturesWrapper of NATS'snats-pyDjango management command to listen for incoming NATS messagesAutomatically serialize/deserialize message from/to JSON formatEasy-to-call method for publishing NATS messagesSupport NATS JetStream pull subscriptionInstallationpipinstalldjango-nats-clientSetupAddnats_clientintoINSTALLED_APPS# settings.pyINSTALLED_APPS=[...'nats_client',]Put NATS connection configuration in settings# settings.pyNATS_SERVERS='nats://localhost:4222'NATS_NAMESPACE='foo'UsageListen for messagesCreate a new callback method and register# common/nats.pyimportnats_client@nats_client.registerdefnew_message(message:str):print(message)@nats_client.registerdefget_year_from_date(date:str):returndate.year# custom name@nats_client.register('get_current_time')defcurrent_time():returndatetime.datetime.now().strftime('%H:%M')# without decoratordefcurrent_time():returndatetime.datetime.now().strftime('%H:%M')nats_client.register('get_current_time',current_time)# JetStream@nats_client.register(js=True)defnew_message(message:str):print(message)# JetStream from other namespace@nats_client.register(namespace='bar',js=True)defnew_message_from_bar(message:str):print(message)Import previously file inreadymethod of yourapps.py# common/apps.pyclassCommonConfig(AppConfig):...defready(self):importcommon.natsRun listener management commandpythonmanage.pynats_listener# or with autoreload enabled (suite for development)pythonmanage.pynats_listener--reloadPublishing messageimportnats_clientarg='some arg'awaitnats_client.publish('subject_name','method_name',arg,keyword_arg=1,another_keyword_arg=2,)Examplesimportnats_clientawaitnats_client.publish('default','new_message','Hello, world!')awaitnats_client.publish('default','project_created',1,name='ACME')# JetStreamawaitnats_client.publish('default','new_message','Hello, world!',_js=True)awaitnats_client.js_publish('default','new_message','Hello, world!')Request-Replyimportnats_clientarg='some arg'awaitnats_client.request('subject_name','method_name',arg,keyword_arg=1,another_keyword_arg=2,)Examplesimportnats_clientyear=awaitnats_client.request('default','get_year_from_date',datetime.date(2022,1,1))# 2022current_time=awaitnats_client.request('default','get_current_time')# 12:11SettingsKeyTypeRequiredDefaultDescriptionNATS_SERVERstrRequired if noNATS_SERVERSNATS server's hostNATS_SERVERSlist[str]Required if noNATS_SERVERNATS server's hosts (for NATS cluster)NATS_NAMESPACEstrNo'default'Main namespace using for prefixing subject, stream name, and etc.NATS_REQUEST_TIMEOUTintNo1Timeout when usingrequest()(in seconds)NATS_OPTIONSdictNo{}Other configuration to be passed innats.connect()NATS_JETSTREAM_ENABLEDboolNoTrueEnable JetStreamNATS_JETSTREAM_DURABLE_NAMEstrNosettings.NATS_NAMESPACEDurable name which is unique across all subscriptionsNATS_JETSTREAM_CREATE_STREAMboolNoTrueAutomatically create stream named inNATS_NAMESPACENATS_JETSTREAM_CONFIGdictNo{}Extra configuration for JetStream streamsDevelopmentRequirementsDockerPythonPoetryLintingmakelintTestingmaketestFix Formattingmakeyapf