sách gpt4 ăn đã đi

redis - 通过 phoenix channel 从 redis channel 流式传输

In lại 作者:IT王子 更新时间:2023-10-29 06:04:25 25 4
mua khóa gpt4 giày nike

我目前正在尝试替换 用一个小 Phoenix 应用程序。我需要做的是从 获取信息 channel 并将其流式传输到 客户。我一直在尝试使用 Redis.PuSub 和 Phoenix Redis 适配器,但未能完全涵盖我们目前拥有的功能。

当前的功能是这样的:

我们的服务器接收到来自用户的请求并将一些输出记录到 Redis channel 。该 channel 的名称是字符串和键的组合。然后,Ember 客户端使用相同的 key 向 action-cable 发出请求。然后,Action-cable 从具有相同名称的 Redis channel 流式传输记录的信息。我需要知道的是如何在用户发出请求时开始收听具有给定名称的 Redis channel 并将该信息连续流式传输到客户端。我已经设法得到一个或另一个,但不是两个。

我已经为此苦思了一天多了,所以非常感谢任何帮助。

chúc mừng

câu trả lời hay nhất

所以为了解决这个问题,我做了以下事情。

首先,我根据 tài liệu 将 Redix.PubSub 设置为依赖项.然后在 channel 中我做了:

defmodule MyApp.ChannelName do
use Phoenix.Channel

def join(stream_name, _message, socket) do
# Open a link to the redis server
{:ok, pubsub} = Redix.PubSub.start_link()

# Subscribe to the users stream
Redix.PubSub.subscribe(pubsub, stream_name, self())

{:ok, socket}
kết thúc

# Avoid throwing an error when a subscribed message enters the channel
def handle_info({:redix_pubsub, redix_pid, :subscribed, _}, socket) do
{:noreply, socket}
kết thúc

# Handle the message coming from the Redis PubSub channel
def handle_info({:redix_pubsub, redix_pid, :message, %{channel: channel, payload: message}}, socket) do
# Push the message back to the user
push socket, "#{channel}", %{message: message}
{:noreply, socket}
kết thúc
kết thúc

在我的例子中,我要求用户注册一个具有某个名称的 channel ,例如channel_my_api_key。然后我会开始监听 redis channel channel_my_api_key 并通过 push 函数将信息流式传输回用户。注意广播可以代替推送。

还要感谢来自 Elixir 论坛的 Alex Garibay,他帮助我找到了解决方案。您可以找到线程 đây .

关于redis - 通过 phoenix channel 从 redis channel 流式传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39875068/

25 4 0
Đề xuất bài viết: python - Redis 锁定一个 KEY
Đề xuất bài viết: Thông báo lại
Đề xuất bài viết: redis - 为什么 Redis 中没有有序的 hashmap?
Đề xuất bài viết: redis - Redis 2.8 到 3.x 的增量是多少?
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress