11const rabbit = require ( "rabbitmq-stream-js-client" )
2- const { randomUUID } = require ( "crypto" )
32
43const rabbitUser = process . env . RABBITMQ_USER || "rabbit"
54const rabbitPassword = process . env . RABBITMQ_PASSWORD || "rabbit"
65
76async function main ( ) {
8- const streamName = `example- ${ randomUUID ( ) } `
7+ const streamName = `stream-offset-tracking-javascript `
98 console . log ( `Creating stream ${ streamName } ` )
109
1110 const client = await rabbit . connect ( {
@@ -18,26 +17,26 @@ async function main() {
1817 } )
1918 await client . createStream ( { stream : streamName , arguments : { } } )
2019 const publisher = await client . declarePublisher ( { stream : streamName } )
21- const totalMessages = 100
20+ const messageCount = 100
2221
23- console . log ( `Publishing ${ totalMessages } messages` )
24- for ( let i = 0 ; i < totalMessages ; i ++ ) {
25- const messageBody = i === totalMessages - 1 ? "marker" : `hello ${ i } `
26- await publisher . send ( Buffer . from ( messageBody ) )
22+ console . log ( `Publishing ${ messageCount } messages` )
23+ for ( let i = 0 ; i < messageCount ; i ++ ) {
24+ const body = i === messageCount - 1 ? "marker" : `hello ${ i } `
25+ await publisher . send ( Buffer . from ( body ) )
2726 }
2827
2928 const startFrom = rabbit . Offset . offset ( 0n )
3029 let firstOffset = startFrom . value
3130 let lastOffset = startFrom . value
32- let messageCount = 0
33- const consumerRef = "offset-tracking-consumer "
31+ let messageReceivedCount = 0
32+ const consumerRef = "offset-tracking-tutorial "
3433 const consumer = await client . declareConsumer ( { stream : streamName , offset : startFrom , consumerRef } , ( message ) => {
35- messageCount ++
34+ messageReceivedCount ++
3635 if ( message . offset === startFrom . value ) {
3736 console . log ( "First message received" )
3837 firstOffset = message . offset
3938 }
40- if ( messageCount % 10 === 0 ) {
39+ if ( messageReceivedCount % 10 === 0 ) {
4140 console . log ( "Storing offset" )
4241 client . storeOffset ( { stream : streamName , reference : consumerRef , offsetValue : message . offset } )
4342 }
0 commit comments