Phoenix-将Ecto查询结果返回给特定客户端 [英] Phoenix - return Ecto query results to a specific client

查看:79
本文介绍了Phoenix-将Ecto查询结果返回给特定客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试设计一种发生以下情况的方案。

I'm currently trying to devise a scheme where the following happens.

已订阅客户端 A /已连接到主题/频道 T

Client A is subscribed/connected to topic/channel T.

A 发送以 select 查询的形式发送到 T 的消息。

A sends a message in the form of a select query to T.

只有 A 接收查询结果,没有其他订阅者。

Only A receives the query results, no other subscribers.

使用Channel甚至可能吗?我选择Channels的主要原因是为了获得出色的websocket支持-但是我欢迎其他Phoenix解决方案。

Is this even possible using Channels? The main reason I chose Channels was for the excellent websocket support - however I'm open to other Phoenix solutions.

推荐答案

是的,渠道应该完成您想要的工作。您可以使用 push 将查询结果下推给发送查询的用户:

Yes, Channels should do the work you want. You can push the query results down to the user who sent the query using push:

def handle_in("new_query", %{"query" => query}, socket) do

    # do the query and store the result into query_result

    #return back the result using push to the user
    push socket, "new_query", %{query_result: query_result}
    {:ok, socket}
end

如果要将查询结果返回给所有加入该主题的用户,只需使用广播而不是,请参阅文档此处

If you want to return the query result to all users who are joined to the topic, you can simply use broadcast instead of push, see the docs here

这篇关于Phoenix-将Ecto查询结果返回给特定客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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