How to setup a phoenix channel for say hundreds of topics?

212 views Asked by At

Suppose there are some users, say 100. Each user has, say 100 items and a percentage of which are shared to the users by other users. Each item has a name.

So... What I want is, if I edit the name of item1 of user1, it should update all the users who have item1.

I have already setup the permissions necessary and REST api is working over controllers.

Now I want to implement real time updates and channels in phoenix are the right fit. If I have a channel with topic item:*, I can account for all the items of all the users.

So, if there is a user1 with item1, item2, item3...., item100. How will I setup the listeners on javascript side? I don't think this would be right approach?

Can someone help me out with the structure please. Thanks!

UPDATE: Will the following approach work?

forEach(function(items, val) {

        var topic = "item:" + val
        // Join the topic
        var channel = socket.channel(topic, {})

        channel.join()
            .receive("ok", data => {
                console.log("Joined topic", topic)
            })
            .receive("error", resp => {
                console.log("Unable to join topic", topic)
            })

        channel.on("name_changed", resp => {
            console.log("name was changed of this item", resp);
        })
    });
1

There are 1 answers

0
abhinav On

(By @schrockwell on #phoenix channel of Slack)

Make a single “item_updates” channel, and then push messages to it like {item_id: 1, changes: ...}

And then on the server-end, register an outgoing handler on that channel so the user only gets updates for items they have permission to know about.

(I think it should work. Going to test it soon.)