How to exclude some users when broadcast? - phoenix-framework

As broadcast! will send message to all users subscribed to topic except sender, is it possible to except certain users? or send to a specific user?
I need users to push events to a channel, but only admin user will receive messages, other users will not receive messages, but only send them.
I can solve this at client side by simply let users ignore messages they receive via broadcast! and only admin user process received messages, but how to solve this at server side?
In short, join a channel, but can read only, or send only?

If you store the user on the socket as explained in https://hexdocs.pm/phoenix/Phoenix.Token.html
defmodule MyApp.UserSocket do
use Phoenix.Socket
def connect(%{"token" => token}, socket) do
case Phoenix.Token.verify(socket, "user", token, max_age: 1209600) do
{:ok, user_id} ->
socket = assign(socket, :user, Repo.get!(User, user_id))
{:ok, socket}
{:error, _} -> #...
end
end
end
Then you can check for the users admin status in the handle_out function for your channel documented here:
defmodule HelloPhoenix.RoomChannel do
intercept ["new_msg"]
...
def handle_out("new_msg", payload, socket) do
if socket.assigns.user.admin do
push socket, "new_msg", payload
end
{:noreply, socket}
end
end
Depending on your volume of messages and number of admins, you may consider having an admin-specific channel for these events. This would prevent messages being sent to processes for non-admin users instead of simply ignoring them.

Related

how to terminate websocket process after web browser page closed

I'm using flask_socketio.
#socketio.on('client_connected')
def handle_client_connect_event(data):
if current_user.is_authenticated:
thread = socketio.start_background_task(background_thread, user_id=current_user.id)
logger.info('server socket client_connected, user:{}', current_user.id)
emit('server_response', {'data': {'job_id': 'job1', 'name': 'hello'}})
else:
logger.info('server socket client_connected, user not authenticated.....')
def background_thread(user_id):
pubsub = redisc.pubsub()
redis_suscriber = redis_channel+str(user_id)
pubsub.psubscribe(redis_suscriber)
logger.info("new thread for: "+redis_suscriber)
for item in pubsub.listen():
logger.info('message received:{}', item)
data_item = item['data']
if isinstance(data_item, bytes):
try:
data_item = json.loads(data_item)
job_id = data_item['job_id']
log_data = data_item['log']
data = {'data': {'job_id': job_id, 'user_id': user_id,
'log': security_util.base64_decrypt(log_data).decode("utf-8")}}
socketio.emit('server_response', data)
except ValueError as e:
logger.error("Error decoding msg to microservice: {}", str(e))
#socketio.on('disconnect')
def disconnected():
if current_user.is_authenticated:
logger.info('user:{} disconnected', current_user.id)
else:
logger.info('client disconnected, user not authenticated.....')
There are js code to establish websocket connection from web browser page to flask server.
When the web browser page is closed, "for item in pubsub.listen():" is still functioning.
So when I open the page and close the page for 3 times, flask server will subscribe the same redis topic for 3 times, and emit the same websocket message for 3 times.
Is there a way to terminate "def background_thread(user_id)" after the web browser page is closed(js client disconnected from websocket server)?
#Miguel Thanks for your reply!
I will try to explain the difficulty I met.
Please forgive me, my English is not very good.
If I open two web pages, there will be two websocket clients: wsclient1 and wsclient2。
And there will be two redis subscribers: rsub1 and rsub2, in two threads.
for item in pubsub.listen():
if ${condition}:
break
If weclient1 disconnect from websocket server, then disconnected() method will be invoked.
How do I define the ${condition}?
'disconnected()'' method and 'redis subscriber' belong to two different threads.
How do I know which one should be terminated?
I don'k know which 'redis subscriber' should mapped to wsclient1.

pynng: how to setup, and keep using, multiple Contexts on a REP0 socket

I'm working on a "server" thread, which takes care of some IO calls for a bunch of "clients".
The communication is done using pynng v0.5.0, the server has its own asyncio loop.
Each client "registers" by sending a first request, and then loops receiving the results and sending back READY messages.
On the server, the goal is to treat the first message of each client as a registration request, and to create a dedicated worker task which will loop doing IO stuff, sending the result and waiting for the READY message of that particular client.
To implement this, I'm trying to leverage the Context feature of REP0 sockets.
Side notes
I would have liked to tag this question with nng and pynng, but I don't have enough reputation.
Although I'm an avid consumer of this site, it's my first question :)
I do know about the PUB/SUB pattern, let's just say that for self-instructional purposes, I chose not to use it for this service.
Problem:
After a few iterations, some READY messages are intercepted by the registration coroutine of the server, instead of being routed to the proper worker task.
Since I can't share the code, I wrote a reproducer for my issue and included it below.
Worse, as you can see in the output, some result messages are sent to the wrong client (ERROR:root:<Worker 1>: worker/client mismatch, exiting.).
It looks like a bug, but I'm not entirely sure I understand how to use the contexts correctly, so any help would be appreciated.
Environment:
winpython-3.8.2
pynng v0.5.0+dev (46fbbcb2), with nng v1.3.0 (ff99ee51)
Code:
import asyncio
import logging
import pynng
import threading
NNG_DURATION_INFINITE = -1
ENDPOINT = 'inproc://example_endpoint'
class Server(threading.Thread):
def __init__(self):
super(Server, self).__init__()
self._client_tasks = dict()
#staticmethod
async def _worker(ctx, client_id):
while True:
# Remember, the first 'receive' has already been done by self._new_client_handler()
logging.debug(f"<Worker {client_id}>: doing some IO")
await asyncio.sleep(1)
logging.debug(f"<Worker {client_id}>: sending the result")
# I already tried sending synchronously here instead, just in case the issue was related to that
# (but it's not)
await ctx.asend(f"result data for client {client_id}".encode())
logging.debug(f"<Worker {client_id}>: waiting for client READY msg")
data = await ctx.arecv()
logging.debug(f"<Worker {client_id}>: received '{data}'")
if data != bytes([client_id]):
logging.error(f"<Worker {client_id}>: worker/client mismatch, exiting.")
return
async def _new_client_handler(self):
with pynng.Rep0(listen=ENDPOINT) as socket:
max_workers = 3 + 1 # Try setting it to 3 instead, to stop creating new contexts => now it works fine
while await asyncio.sleep(0, result=True) and len(self._client_tasks) < max_workers:
# The issue is here: at some point, the existing client READY messages get
# intercepted here, instead of being routed to the proper worker context.
# The intent here was to open a new context only for each *new* client, I was
# assuming that a 'recv' on older worker contexts would take precedence.
ctx = socket.new_context()
data = await ctx.arecv()
client_id = data[0]
if client_id in self._client_tasks:
logging.error(f"<Server>: We already have a task for client {client_id}")
continue # just let the client block on its 'recv' for now
logging.debug(f"<Server>: New client : {client_id}")
self._client_tasks[client_id] = asyncio.create_task(self._worker(ctx, client_id))
await asyncio.gather(*list(self._client_tasks.values()))
def run(self) -> None:
# The "server" thread has its own asyncio loop
asyncio.run(self._new_client_handler(), debug=True)
class Client(threading.Thread):
def __init__(self, client_id: int):
super(Client, self).__init__()
self._id = client_id
def __repr__(self):
return f'<Client {self._id}>'
def run(self):
with pynng.Req0(dial=ENDPOINT, resend_time=NNG_DURATION_INFINITE) as socket:
while True:
logging.debug(f"{self}: READY")
socket.send(bytes([self._id]))
data_str = socket.recv().decode()
logging.debug(f"{self}: received '{data_str}'")
if data_str != f"result data for client {self._id}":
logging.error(f"{self}: client/worker mismatch, exiting.")
return
def main():
logging.basicConfig(level=logging.DEBUG)
threads = [Server(),
*[Client(i) for i in range(3)]]
for t in threads:
t.start()
for t in threads:
t.join()
if __name__ == '__main__':
main()
Output:
DEBUG:asyncio:Using proactor: IocpProactor
DEBUG:root:<Client 1>: READY
DEBUG:root:<Client 0>: READY
DEBUG:root:<Client 2>: READY
DEBUG:root:<Server>: New client : 1
DEBUG:root:<Worker 1>: doing some IO
DEBUG:root:<Server>: New client : 0
DEBUG:root:<Worker 0>: doing some IO
DEBUG:root:<Server>: New client : 2
DEBUG:root:<Worker 2>: doing some IO
DEBUG:root:<Worker 1>: sending the result
DEBUG:root:<Client 1>: received 'result data for client 1'
DEBUG:root:<Client 1>: READY
ERROR:root:<Server>: We already have a task for client 1
DEBUG:root:<Worker 1>: waiting for client READY msg
DEBUG:root:<Worker 0>: sending the result
DEBUG:root:<Client 0>: received 'result data for client 0'
DEBUG:root:<Client 0>: READY
DEBUG:root:<Worker 0>: waiting for client READY msg
DEBUG:root:<Worker 1>: received 'b'\x00''
ERROR:root:<Worker 1>: worker/client mismatch, exiting.
DEBUG:root:<Worker 2>: sending the result
DEBUG:root:<Client 2>: received 'result data for client 2'
DEBUG:root:<Client 2>: READY
DEBUG:root:<Worker 2>: waiting for client READY msg
ERROR:root:<Server>: We already have a task for client 2
Edit (2020-04-10): updated both pynng and the underlying nng.lib to their latest version (master branches), still the same issue.
After digging into the sources of both nng and pynng, and confirming my understanding with the maintainers, I can now answer my own question.
When using a context on a REP0 socket, there are a few things to be aware of.
As advertised, send/asend() is guaranteed to be routed to the same peer you last received from.
The data from the next recv/arecv() on this same context, however, is NOT guaranteed to be coming from the same peer.
Actually, the underlying nng call to rep0_ctx_recv() merely reads the next socket pipe with available data, so there's no guarantee that said data is coming from the same peer than the last recv/send pair.
In the reproducer above, I was concurrently calling arecv() both on a new context (in the Server._new_client_handler() coroutine), and on each worker context (in the Server._worker() coroutine).
So what I had previously described as the next request being "intercepted" by the main coroutine was merely a race condition.
One solution would be to only receive from the Server._new_client_handler() coroutine, and have the workers only handle one request. Note that in this case, the workers are no longer dedicated to a particular peer. If this behavior is needed, the routing of incoming requests must be handled at application level.
class Server(threading.Thread):
#staticmethod
async def _worker(ctx, data: bytes):
client_id = int.from_bytes(data, byteorder='big', signed=False)
logging.debug(f"<Worker {client_id}>: doing some IO")
await asyncio.sleep(1 + 10 * random.random())
logging.debug(f"<Worker {client_id}>: sending the result")
await ctx.asend(f"result data for client {client_id}".encode())
async def _new_client_handler(self):
with pynng.Rep0(listen=ENDPOINT) as socket:
while await asyncio.sleep(0, result=True):
ctx = socket.new_context()
data = await ctx.arecv()
asyncio.create_task(self._worker(ctx, data))
def run(self) -> None:
# The "server" thread has its own asyncio loop
asyncio.run(self._new_client_handler(), debug=False)

Crystal Lang Websocket server

I need help with Crystal Lang websockets, I want to know how to upgrade my connection on websocket. I want make simple websocket server
hope this help
require "http/server"
SOCKETS = [] of HTTP::WebSocket
ws_handler = HTTP::WebSocketHandler.new do |socket|
puts "Socket opened"
SOCKETS << socket
socket.on_message do |message|
SOCKETS.each { |socket| socket.send "Echo back from server: #{message}" }
end
socket.on_close do
puts "Socket closed"
end
end
server = HTTP::Server.new([ws_handler])
address = server.bind_tcp "0.0.0.0", 3000
puts "Listening on http://#{address}"
server.listen
https://medium.com/#muhammadtriwibowo/simple-websocket-using-crystal-13b6f67eba61
if you are looking for ready to use something, then you can use Shivneri framework created by me - which provides a javascript library and MVC based approach to create a socket server.
How to create a web socket end point
class ChatController < Shivneri::WebSocketController
#[On("message")]
def receive_message(data : String)
# send message to caller
clients.current.emit("message", "Received message is #{data}")
# send message to all clients
clients.emit("message", "Someone sent message #{data}")
end
end
How to connect to web socket end point using javascript
Shivneri framework provides a javascript library shivneri-ws-client-javascript to help you create a real time web application
var socket = new shivneriWsClient.Instance();
socket.on("message", function(data){
console.log("data", data);
});
await socket.init(`<web-socket-url>`);
// emit event to server
socket.emit("message","Successfully connected")
It provides many functionalities like grouping of clients, events when client is connected & disconnected etc.
For more information, take a look at shivneri websocket doc - https://shivneriforcrystal.com/tutorial/websocket/

how to send message to different user in phoenix framework chat app

I am working with phoenix framework to create different type chat app. In my case, we have chat rooms but not functioning as a normal chat room.
Every user has his own room he can join to his room using different devices (mobile, pc, some other sources).
User A has his own room and user B has his own room, these two members do not connect to a single room as in a normal scenario in the real world.
Now my user A wants to send a message to user B
message data eg:
from : A
to :B
message : test message
This is a snippet from app.js I used to connect to the user's specific room :
let room = socket.channel("room:"+ user, {})
room.on("presence_state", state => {
presences = Presence.syncState(presences, status)
console.log(presences)
render(presences)
})
This is the snippet from back-end for join room function
/web/channel/RoomChannel.ex
def join("room:" <> _user, _, socket) do
send self(), :after_join
{:ok, socket}
end
But now I am stuck in the middle because I cannot find a way to send messages between users.
eg: Cannot identify way to deliver User A's message to user B using this specific scenario
This is the basic architect of this chat app :
This is rest of the code in Roomchannel file
def handle_info(:after_join, socket) do
Presence.track(socket, socket.assigns.user, %{
online_at: :os.system_time(:millisecond)
})
push socket, "presence_state", Presence.list(socket)
{:noreply, socket}
end
def handle_in("message:new", message, socket) do
broadcast! socket, "message:new", %{
user: socket.assigns.user,
body: message,
timestamp: :os.system_time(:millisecond)
}
milisecondstime = :os.system_time(:millisecond)
[room, user] = String.split(Map.get(socket, :topic), ":")
data = Poison.encode!(%{"created_at" => :os.system_time(:millisecond), "updated_at" => :os.system_time(:millisecond), "uuid" => UUID.uuid1(), "date" => :os.system_time(:millisecond), "from" => user, "to" => user, "message" => message})
Redix.command(:redix, ["SET", UUID.uuid1(), data])
{:noreply, socket}
end
Can anyone show me some way by which I can pass messages between user's chat rooms?
I'm using redis to store data and
this is the basic chat app I am following thanks to that developer.
Look into Phoenix.Endpoint.broadcast
You can send message to a given topic using the broadcast/3 function
Another way you can go about this is to subscribe each user to a unique private topic when the user joins the channel join function
def join("room:" <> _user, _, socket) do
send self(), :after_join
MyApp.Endpoint.subscribe("private-room:" <> user) # subscribe the connecting client to their private room
{:ok, socket}
end
Then in handle_in("message:new",message, socket) extract the receipient user id from the message payload and call
Endpoint.broadcast
def handle_in("message:new", message, socket) do
MyApp.Endpoint.broadcast!("private-room:" <> to_user , "message:new",%{
user: socket.assigns.user,
body: message,
timestamp: :os.system_time(:millisecond)
}) #sends the message to all connected clients for the to_user
{:noreply, socket}
end

Send data to a decent user with Kemal over websocket

How can i send data data to a decent user connected via websockets? I know,
Websocket connections yields the context, but how can i filter a decent socket connection for sending data to only 1 (or some) connected user(s) depending on context (env)?
SOCKETS = [] of HTTP::WebSocket
ws "/chat" do |socket,env|
room = env.params.query["room"]
SOCKETS << socket
socket.on_message do |message|
SOCKETS.each { |socket| socket.send message}
end
socket.on_close do
SOCKETS.delete socket
end
end
Must socket contain the room or needs SOCKETS to be a Hash?
You can store sockets as hash and give id to your clients and then you can send saved socket of your recent client.
I mean that
SOCKETS = {} of String => HTTP::WebSocket
socket.on_message do |message|
j = JSON.parse(message)
case j["type"]
when "login"
user_id = j["id"].to_s
SOCKETS[user_id] = socket
when "whisper"
to = j["to"].to_s
from = j["from"]
user = SOCKETS[to].send("#{from} is whispering you!")
end
something like that.

Resources