Skip to content

NatsJSFastProducer

faststream.nats.publisher.producer.NatsJSFastProducer #

NatsJSFastProducer(
    *,
    parser: Optional[CustomCallable],
    decoder: Optional[CustomCallable],
)

Bases: NatsFastProducer

A class to represent a NATS JetStream producer.

Source code in faststream/nats/publisher/producer.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def __init__(
    self,
    *,
    parser: Optional["CustomCallable"],
    decoder: Optional["CustomCallable"],
) -> None:
    self.serializer: SerializerProto | None = None
    self.codec: CodecProto = DefaultCodec()

    default = NatsParser(pattern="", is_ack_disabled=True)
    self._parser = ParserComposition(parser, default.parse_message)
    self._decoder = ParserComposition(decoder, default.decode_message)

    self.__state: ConnectionState[JetStreamContext] = EmptyConnectionState()

serializer instance-attribute #

serializer: SerializerProto | None = None

codec instance-attribute #

codec: CodecProto = DefaultCodec()

connect #

connect(
    connection: JetStreamContext,
    serializer: Optional[SerializerProto],
    codec: Optional[CodecProto] = None,
) -> None
Source code in faststream/nats/publisher/producer.py
165
166
167
168
169
170
171
172
173
def connect(
    self,
    connection: "JetStreamContext",
    serializer: Optional["SerializerProto"],
    codec: Optional["CodecProto"] = None,
) -> None:
    self.serializer = serializer
    self.codec = codec or DefaultCodec()
    self.__state = ConnectedState(connection)

disconnect #

disconnect() -> None
Source code in faststream/nats/publisher/producer.py
175
176
def disconnect(self) -> None:
    self.__state = EmptyConnectionState()

publish async #

publish(cmd: NatsPublishCommand) -> PubAck
Source code in faststream/nats/publisher/producer.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
@override
async def publish(self, cmd: "NatsPublishCommand") -> "PubAck":
    payload, content_type = await self.codec.encode(cmd.body, self.serializer)

    headers_to_send = {
        "content-type": content_type or "",
        **cmd.headers_to_publish(js=True),
    }

    return await self.__state.connection.publish(
        subject=cmd.destination,
        payload=payload,
        headers=headers_to_send,
        stream=cmd.stream,
        timeout=cmd.timeout,
    )

request async #

request(cmd: NatsPublishCommand) -> Msg
Source code in faststream/nats/publisher/producer.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
@override
async def request(self, cmd: "NatsPublishCommand") -> "Msg":
    payload, content_type = await self.codec.encode(cmd.body, self.serializer)

    reply_to = self.__state.connection._nc.new_inbox()
    future: asyncio.Future[Msg] = asyncio.Future()
    sub = await self.__state.connection._nc.subscribe(
        reply_to,
        future=future,
        max_msgs=1,
    )
    try:
        await sub.unsubscribe(limit=1)

        headers_to_send = {
            "content-type": content_type or "",
            "reply_to": reply_to,
            **cmd.headers_to_publish(js=False),
        }

        with anyio.fail_after(cmd.timeout):
            await self.__state.connection.publish(
                subject=cmd.destination,
                payload=payload,
                headers=headers_to_send,
                stream=cmd.stream,
                timeout=cmd.timeout,
            )

            msg = await future

            if (  # pragma: no cover
                msg.headers
                and (msg.headers.get(Header.STATUS) == NO_RESPONDERS_STATUS)
            ):
                raise nats.errors.NoRespondersError

            return msg
    finally:
        future.cancel()
        # The one-message limit cannot remove an inbox that never receives a reply.
        with (
            anyio.CancelScope(shield=True),
            suppress(
                nats.errors.ConnectionClosedError,
                nats.errors.ConnectionDrainingError,
            ),
        ):
            await sub.unsubscribe()

publish_batch async #

publish_batch(cmd: NatsPublishCommand) -> None
Source code in faststream/nats/publisher/producer.py
56
57
58
59
@override
async def publish_batch(self, cmd: "NatsPublishCommand") -> None:
    msg = "NATS doesn't support publishing in batches."
    raise FeatureNotSupportedException(msg)