Search code examples
push-diffusion

Topic subscribers may get nothing until an update is published


I'm building a system to publish financial instrument prices. Some instruments update a few times a second and some of them update less often. No matter the update frequency all subscribers need to see the last update or current price of that instrument.

A subscriber may wait some time before seeing a topic update on all topics, leaving their view of the slower topics empty. I could use a SessionPropertiesListener to listen for new audience members and then update each topic with the current prices, but that generates a lot of noise for existing audience members, and feels like the tail is wagging the dog.

I can illustrate my problem with a slowly updating topic:

#!/usr/local/bin/node
var diffusion = require('diffusion');

const diffHost = "localhost";
const exampleTopic = "some/counter";

diffusion.connect({
    host : diffHost,
    port: 8080,
    secure: false,
    principal : 'admin',
    credentials : 'password',
}).then(function(session) {
    console.log('Connected to %s', diffHost);

    var count = 0;
    session.topics.add(exampleTopic).then(function(result) {
        console.log("Created %s", exampleTopic);
        setInterval(function() {
            session.topics.update(exampleTopic, ++count);
            console.log("Updated %s to %d", exampleTopic, count);
        }, 5000);
    }, function(error) {
        console.log('Topic add failed: ', error);
    });
});

Anyone subscribing to some/counter will wait on average 2.5s before seeing anything. I feel like I'm missing a trick here, how can I ensure a subscriber efficiently gets the current price as soon as they subscribe to it?


Solution

  • Your topic some/counter is stateless, which explains why new subscribers receive nothing before it is updated. Instead of creating your topic:

    session.topics.add(exampleTopic)
    

    Replace it with:

    session.topics.add(exampleTopic, count)
    

    .. to create a stateful topic.