-2

My project has three namespaces: Trainee, Trainer, and Manager. I use three templates for each namespace. I use action cable for notification (which uses websocket) in the project. Each namespace has different notification. How can I create a namespace for a channel?

sawa
  • 165,429
  • 45
  • 277
  • 381
Trần Hồng
  • 91
  • 2
  • 10

1 Answers1

0

You wouldn't create a namespace for a channel, you would just create separate streams to subscribe to, like you would for chat rooms.

You could pass in a "namespace" parameter when creating the subscription, like so:

App.cable.subscriptions.create({
  channel: 'NotificationChannel', namespace: <%= namespace %>
}, {}

then subscribe to a different streams based on the namespace param:

class NotificationChannel < ApplicationCable::Channel
  def subscribed
    stream_from notification_stream
  end

  def receive(data)
    ActionCable.server.broadcast notification_stream, data.fetch('message')
  end

  private

  def notification_stream
    "#{params['namespace']}_stream"
  end

end
Ricky Brown
  • 644
  • 5
  • 8