Search code examples
starcounter

Data updated in Starcounter DB, push updates to active sessions?


Say for example I have a something like this declared in Starcounter

[Database]
public class User 
{
    public string Username;
    public string Email;
} 

I have a page listing one row from the DB with an update button with PuppetJS and all working fine.

If I change the value from another session or directly in the DB, is there anyway to directly update the values to any client who are active by pushing the new values to the clients?

*** Edit: I added following to my TestPage.json.cs file :

    void Handle(Input.Update action)
    {
        Transaction.Commit();
        Session.ForAll(s =>
            {
                if (s.Data is TestPage)
                    s.CalculatePatchAndPushOnWebSocket();
            });
    }

This push updates directly to other sessions nicely. Still wonder if there is some better way to do this.


Solution

  • The code that you've presented in your edit is exactly the way to go:

    void Handle(Input.Update action)
    {
        Transaction.Commit();
        Session.ForAll(s =>
            {
                if (s.Data is TestPage)
                    s.CalculatePatchAndPushOnWebSocket();
            });
    }
    

    What it does is:

    • commit changes to db
    • for every running session, check if that session has a TestPage instance attached to it
    • if the above is positive, revaluate the bound data and send patches if required

    More about pushing changes over WebSocket can be found here: http://starcounter.io/guides/web/sessions/.