Skip to content

Instantly share code, notes, and snippets.

@aricart
Created June 30, 2020 14:15
Show Gist options
  • Select an option

  • Save aricart/a575e518d85d4d78a9062d50281c3840 to your computer and use it in GitHub Desktop.

Select an option

Save aricart/a575e518d85d4d78a9062d50281c3840 to your computer and use it in GitHub Desktop.

Revisions

  1. aricart created this gist Jun 30, 2020.
    9 changes: 9 additions & 0 deletions s1.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    listen: "localhost:4222"
    cluster: {
    listen: "localhost:4333"
    }
    streaming: {
    store: "file"
    dir: "/tmp/stan-cluster/data"
    ft_group: "ft"
    }
    10 changes: 10 additions & 0 deletions s2.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    listen: "localhost:2224"
    cluster: {
    listen: "localhost:3334"
    routes: ["nats://localhost:4333"]
    }
    streaming: {
    store: "file"
    dir: "/tmp/stan-cluster/data"
    ft_group: "ft"
    }
    38 changes: 38 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    const STAN = require('node-nats-streaming')
    let interval;
    const sc = STAN.connect("test-cluster", "me")
    sc.on('error', (err) => {
    console.error(err);
    })
    sc.on('connection_lost', () => {
    console.log('stan connection was lost!')
    })
    sc.on('close', () => {
    clearInterval(interval)
    console.log('closed')
    })
    sc.on('connect', () => {
    console.log(`connected to ${sc.nc.currentServer.url}`)
    const sub = sc.subscribe("test")
    sub.on('message', (msg) => {
    console.log(`[${msg.getSequence()}]: ${msg.getData()}`)
    })

    let count = 0;
    interval = setInterval(()=>{

    sc.publish('test', `${++count}`)
    }, 1000)
    })

    sc.on('disconnect', () => {
    console.log('disconnected')
    })

    sc.on('reconnect', () => {
    console.log(`reconnected to ${sc.nc.currentServer.url}`)
    })