I have action cable channel with identified user
class DesktopNotificationsChannel < ApplicationCable::Channel
def subscribed
reject and return unless current_user.present?
logger.add_tags 'ActionCable DesktopNotificationsChannel', current_user.id
stream_for current_user
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
And i'm connecting this channel from my external application (Desktop application written in C# and I'm using WebSocketSharp library for connections). I can successfully listen all broadcasted messages that sent with,
DesktopNotificationsChannel.broadcast_to(user, data: 'Some Data' )
But i also want to broadcast message from external application. I've tried various ways to broadcast message for specific user but actioncable ignores the message I'm trying to send;
{
"command": "message",
"data": {"status": "current_status"},
"identifier": { "channel": "DesktopNotificationsChannel"}
}
I figured out i have to add user_id or global_id for user that i want to send message in that json but i couldn't succeed.
I also tried change identifier with rails' broadcaster key like desktop_notifications:xxyyyzz
but it also didn't work.
I'm missing something but couldn't figured. How can i send broadcast message for specific user from my non rails application.
CodePudding user response:
I managed to fetch messages by adding stream_from
and receive
method to my channel. I don't know its proper way to do but it but it's working like this.
class DesktopNotificationsChannel < ApplicationCable::Channel
def subscribed
reject and return unless current_user.present?
logger.add_tags 'ActionCable DesktopNotificationsChannel', current_user.id
stream_for current_user
stream_from current_user
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def receive(data)
DesktopNotificationsChannel.broadcast_to(current_user, data:)
end
end