Search code examples
node.jssocket.iofeathersjs

Add active connections to channel when it's created


I am trying to prevent who can see events for a particular page, so I am determining and storing the ids of which users have access to a particular page. In channels.js, I can add users to the new page-level channel in app.on('connection') by iterating through the pages like this: app.channel('page-' + pageId).join(connection)

The problem is that a user doesn't start getting broadcasts from that page until after they refresh the browser and re-connect.

What I want to happen is have all connections for a an allowed user start getting broadcasts when the page is created. Is there a way to do that in channels.js, or can I tell it who to start broadcasting to in a hook for the page creation?

Editing to add the last thing I tried. "Users-pages" is an associative entity that links Users and Pages.

app.service('users-pages').on('created', userPage => {
    const { connections } = app.channel(app.channels).filter(connection =>
      connection.user.id === userPage.userId
    )
    app.channel('page-' + userPage.pageId).join(connections)
})

Solution

  • Using app.channel('page-' + pageId).join(connections) wasn't working for me. But looping through each connection works fine:

    connections.forEach(connection => {
      app.channel('page-' + userPage.pageId).join(connection)
    })