File tree 3 files changed +41
-1
lines changed
3 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -556,6 +556,13 @@ export interface NatsConnection {
556
556
*/
557
557
publishMessage ( msg : Msg ) : void ;
558
558
559
+ /**
560
+ * Replies using the reply subject of the specified message, specifying the
561
+ * data, headers in the message if any.
562
+ * @param msg
563
+ */
564
+ respondMessage ( msg : Msg ) : boolean ;
565
+
559
566
/**
560
567
* Subscribe expresses interest in the specified subject. The subject may
561
568
* have wildcards. Messages are delivered to the {@link SubOpts#callback |SubscriptionOptions callback}
Original file line number Diff line number Diff line change @@ -133,6 +133,17 @@ export class NatsConnectionImpl implements NatsConnection {
133
133
} ) ;
134
134
}
135
135
136
+ respondMessage ( msg : Msg ) {
137
+ if ( msg . reply ) {
138
+ this . publish ( msg . reply , msg . data , {
139
+ reply : msg . reply ,
140
+ headers : msg . headers ,
141
+ } ) ;
142
+ return true ;
143
+ }
144
+ return false ;
145
+ }
146
+
136
147
subscribe (
137
148
subject : string ,
138
149
opts : SubscriptionOptions = { } ,
Original file line number Diff line number Diff line change @@ -1381,7 +1381,7 @@ Deno.test("basics - sync subscription", async () => {
1381
1381
await cleanup ( ns , nc ) ;
1382
1382
} ) ;
1383
1383
1384
- Deno . test ( "basics - respond message" , async ( ) => {
1384
+ Deno . test ( "basics - publish message" , async ( ) => {
1385
1385
const { ns, nc } = await setup ( ) ;
1386
1386
const sub = nc . subscribe ( "q" ) ;
1387
1387
@@ -1403,6 +1403,28 @@ Deno.test("basics - respond message", async () => {
1403
1403
await cleanup ( ns , nc ) ;
1404
1404
} ) ;
1405
1405
1406
+ Deno . test ( "basics - respond message" , async ( ) => {
1407
+ const { ns, nc } = await setup ( ) ;
1408
+ const sub = nc . subscribe ( "q" ) ;
1409
+
1410
+ const nis = new MM ( nc ) ;
1411
+ nis . data = new TextEncoder ( ) . encode ( "not in service" ) ;
1412
+
1413
+ ( async ( ) => {
1414
+ for await ( const m of sub ) {
1415
+ if ( m . reply ) {
1416
+ nis . reply = m . reply ;
1417
+ nc . respondMessage ( nis ) ;
1418
+ }
1419
+ }
1420
+ } ) ( ) . then ( ) ;
1421
+
1422
+ const r = await nc . request ( "q" ) ;
1423
+ assertEquals ( r . string ( ) , "not in service" ) ;
1424
+
1425
+ await cleanup ( ns , nc ) ;
1426
+ } ) ;
1427
+
1406
1428
class MM implements Msg {
1407
1429
data ! : Uint8Array ;
1408
1430
sid : number ;
You can’t perform that action at this time.
0 commit comments