Phoenix - return Ecto query results to a specific client

醉酒当歌 提交于 2019-12-08 02:43:46

问题


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

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

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

Only A receives the query results, no other subscribers.

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.


回答1:


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



来源:https://stackoverflow.com/questions/37373766/phoenix-return-ecto-query-results-to-a-specific-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!