ActionCable:每个用户一个频道 [英] ActionCable: One channel per user

查看:58
本文介绍了ActionCable:每个用户一个频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 Rails 应用的通知.有 User 模型和控制器,Like 模型与 User 关联,一个 User 有很多 Likes 关联.

I am working on notifications for my rails app. There is User model and controller and Like model associated to User with a User has many Likes association.

这个想法是,如果一个用户喜欢另一个用户的个人资料,该个人资料的所有者将收到实时通知.

The idea is, if a user likes the profile of another user, owner of the profile will get a live notification.

这是我用于理解 ActionCable 的应用程序.我研究了 cable api,pub-sub 对我来说是新的.我能够通过单一渠道进行成功的沟通,并且还成功地使用了 授权.但是,我仍然不明白如何为每个用户使用单个频道(动态生成的频道),以便发布给一个用户的消息不会发送给另一个用户.

This is my app for understanding ActionCable. I studied the cable api, pub-sub was new to me. I was able to make successful communication through a single channel, and also successfully used the authorization. But, still I don't understand how to use single channel per user (dynamically generated channel), so that message posted to one user doesn't go to another user.

是否可以使用动态渠道?

Is dynamic channel possible?

或者,有没有其他方式可以让一个渠道满足所有用户的需求

Or, are there other ways where a single channel satisfies all the users needs

推荐答案

解决方案实际上很简单,但 ActionCable 中引入的一些新术语使它变得混乱.顺便说一下,这是我解决它的方法.

The solution was actually simple, but some new terms introduced in ActionCable made it confusing. By the way, here's how I solved it.

我为不同的用户使用了一个频道,但使用了多个流.这是 ActionCable 文档链接,清楚说明一个非常相同的场景.之前看过文档,但不知为何乍一看没看懂.

I used a single channel but multiple streams for different users. Here's the ActionCable documentation link that clearly states a very identical scenario. I went through the documentation earlier, but somehow I didn't understand it at the first glance.

抱歉没有提供任何直接的代码,我认为文档会完整地解释它.

Sorry for not providing any direct code, I think the documentation will explain it totally.

这篇关于ActionCable:每个用户一个频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆