gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:04:25 25 4
gpt4 key购买 nike

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

当前的功能是这样的:

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

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

干杯

最佳答案

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

首先,我根据 docs 将 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}
end

# Avoid throwing an error when a subscribed message enters the channel
def handle_info({:redix_pubsub, redix_pid, :subscribed, _}, socket) do
{:noreply, socket}
end

# 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}
end
end

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com