Is it best to wait for the broadcast to update yourself, or assign new state immediately?

I’m working on a live “feed” feature. Right now, when a user creates a new post, the event handler just assigns new state of [new_item | feed_items]. My thought was to broadcast the new post, and just ignore it if you’re the author.

Would there be any advantage or disadvantage to relying 100% on PubSub? This way would have me broadcasting the new feed item to everyone, including myself, and updating page state accordingly.

If a user has two windows open and post in window 1 then window 2 wouldnt get the item in the feed. But if you use pubsub for the users own items too both windows would get updated.

1 Like

Good point, that’s enough of a reason to lean on pubsub more I think.

1 Like

In addition to the “two windows” scenario, there’s also ordering to consider. The Liveview that just created new_item may have PubSub messages waiting in its mailbox; putting the new record on the page before them will result in messages out of order.

One big takeaway from adventures in the various JS frameworks: you’ll have a better experience if you avoid mutating the same state through two different paths, in this case the “direct” path and the PubSub path.

2 Likes