angular2-actioncable
angular2-actioncable copied to clipboard
perform doesn't sent data properly?
Hello,
I have a channel instanced in my angular app but i want to perform with action 'pull_items' in a ruby api with some data, but doesn't happend properly? or i really dont understand what is the porpose to perform method with the action param and data param.
Here look a example
const cable = this.cable.getActualCable();
const channel = cable.channel('ShoppingCartChannel');
channel.perform('push_items', 'some');
channel.perform('pull_items')
then the channel.connected()
suscription must catch with a calback fn some data
when i debug with develop tools in chrome i see my other channel doing the perform correctly like this
{"command":"message","identifier":"{\"channel\":\"NotificationChannel\"}","data":"{\"action\":\"unreads\"}"}
and 'push_items'
{"command":"message","identifier":"{\"channel\":\"ShoppingCartChannel\"}","data":"\"some\""}
I see a difference between to logs in the key data
then what should i do?
I appreciate your understanding
I run some tests, then i tried
channel.perform('push_items', JSON.parse(
{"action": "push_items", "data": ${JSON.stringify(data)} }));
and it works!, but i think, is not an optimal solution
this.channel.send({action: "push_items", data: data });
here is other solution
To be honest, I've never actually used the perform method but added it as a pass through since it's part of the Action Cable API. Looking at the implementation of the perform method in Action Cable I see:
// Perform a channel action with the optional data passed as an attribute
perform(action, data = {}) {
data.action = action
return this.send(data)
}
So it looks like your data argument needs to be an object instead of a string and your 'push_items' action is getting lost since it can't be added as a property to the 'some' string you're supplying. I should probably change the type of the data parameter to be object instead of any since it doesn't look like primitives will work.
Hi! I have a chat where every dialogs connect the same channel name but diff param - id. And I wanna send a new message to current channel but I got an error.
ERROR TypeError: Cannot read property 'send' of undefined
at Channel.send (angular2-actioncable.js:5696)
I use method "send" like this:
const cable = this.cableService.cable(environment.WS_ENDPOINT);
const chatChannel = cable.channel('ChatChannel', {id : this.id });
chatChannel.send({newMessageData});
Maybe you also have received this error. I will be grateful for your help!
@MariaNSe I don't think you're connected to the channel in time to call send. You can trying subscribing to chatChannel.connected() and calling send within your callback.
Thank you for the answer! It helped me. Yeah, I really didn't connected to the channel but cause it was wrong channel name 🙈 I connect to the channel in ngOnInit() and it works when I call send().