Frontend application to backend server websocket connections can have one producer - python-3.x

I am working on frontend application, where front end application sends video (live) frames to backend server, backend server processes the video frames and sends the data(text) back to front end application via websocket connection. On backend websocket server side, whenever client sends video frames, these frames are added to the queue along with connection id. Is it possible to write a method which polls the queue, process the frames and generate the text data for specific client which will be sent to the client. This method should have one instance only and should be shared by all websocket connections. The method uses the shared variables and should have exclusive access. I can't mix frames from different client to generate text data.
If I write a producer then each client connection will have separate producer and these producers will use shared variable which is not desired. Right now I wrote websocket connection without consumer-producer and put lock around the critical code. But it doesn't seem good solution. Backend is in python and front end in reactjs. Any suggestions?

Related

Connection between multiple clients to a single server in Nodejs

I need to create 20 clients which make requests simultaneously to a server in nodejs using websocket. I am able to create the connection between a single server and a client using websocket. But when it comes to creating 20 clients, I am not having any idea to proceed. Please give any suggestions to proceed.
You wouldn't need to create 20 html pages. Same HTML page can be loaded by multiple clients.
On server-side, the 'request' event will fire every time a client connects to your websocket server. Your websocket server will be able to handle multiple clients out of the box. However, you will need to ascertain 'which' client this particular request originated from. That can be done by using tokens or credentials, or any other custom protocol that you want to establish between your client and server.
Check the server-side usage example for websocket module here: https://www.npmjs.com/package/websocket#server-example

Websocket Dropped Frames?

Trying to solve a perplexing issue w/streaming audio over websockets. We are using Nexmo (Twilio competitor) which enables bidirectional streaming of call audio over websockets. Nexmo connects to our websocket server and starts sending 16khz sampled audio frames of length 640 bytes each.
Everything was working great until recently the websocket audio suddenly started dropping clumps of frames, resulting in gaps in the audio.
But the most interesting thing is the following:
When Nexmo connects directly to our digitalocean vps, frames are dropped
When Nexmo connects via an ngrok tunnel, everything starts working again
Any ideas on where to look for a real solution would be awesome.
Make certain the process which is receiving the websocket traffic has a separate thread just to handle this traffic ... any system will drop traffic if its too busy with other tasks ... if your receiving end has some event loop its maintaining while getting preempted by incoming websocket interrupts you will drop packets
I did a project where receiving end was the browser which was running an event loop to perform the audio rendering while simultaneously also handling the websocket traffic - not a good idea since the critical portions of this event loop must not be allowed to get preempted ... I had to create a webworker process on browser side to handle all the websocket traffic to then populate a circular audio buffer ... this webworker was viewed as a client by the browser event loop which was rendering the audio yet was now permitted to never get preempted by incoming traffic ... only when browser event loop reached its lull period did it then request to retrieve another gulp of data buffered up by webworker audio buffer queue

Sending data from RabbitMQ to Node.JS via Socket.IO

I am going to design a system where there is a two-way communication between clients and a web application. The web application can receive data from the client so it can persist it to a DB and so forth, while it can also send instructions to the client. For this reason, I am going to use Node.JS and Socket.IO.
I also need to use RabbitMQ since I want that if the web application sends an instruction to a client, and the client is down (hence the socket has dropped), I want it to be queued so it can be sent whenever the client connects again and creates a new socket.
From the client to the web application it should be pretty straightforward, since the client uses the socket to send the data to the Node.JS app, which in turn sends it to the queue so it can ultimately be forwarded to the web application. From this direction, if the socket is down, there is no internet connection, and hence the data is not sent in the first place, or is cached on the client.
My concern lies with the other direction, and I would like an answer before I design it this way and actually implement it, so I can avoid hitting any brick walls. Let's say that the web application tries to send an instruction to the client. If the socket is available, the web app forwards the instruction to the queue, which in turn forwards it to the Node.JS app, which in turn uses the socket to forward it to the client. So far so good. If on the other hand, the internet connection from the client has dropped, and hence the socket is currently down, the web app will still send the instruction to the queue. My question is, when the queue forwards the instruction to Node.JS, and Node.JS figures out that the socket does not exist, and hence cannot send the instruction, will the queue receive a reply from Node.JS that it could not forward the data, and hence that it should remain in the queue? If that is the case, it would be perfect. When the client manages to connect to the internet, it will perform a handshake once again, the queue will once again try to send to Node.JS, only this time Node.JS manages to send the instruction to the client.
Is this the correct reasoning of how those components would interact together?
this won't work the way you want it to.
when the node process receives the message from rabbitmq and sees the socket is gone, you can easily nack the message back to the queue.
however, that message will be processed again immediately. it won't sit there doing nothing. the node process will just pick it up again. you'll end up with your node / rabbitmq thrashing as it just nacks a message over and over and over and over, waiting for the socket to come back online.
if you have dozens or hundreds of messages for a client that isn't connected, you'll have dozens or hundreds of messages thrashing round in circles like this. it will destroy the performance of both your node process and rabbitmq.
my recommendation:
when the node app receives the message from rabbitmq, and the socket is not available to the client, put the message in a database table and mark it as waiting for that client.
when the client re-connects, check the database for any pending messages and forward them all at that point.

Synchronisation: Client, Server Chat

I am writing a Client, Server-based chat. The Server is the central component and handles all the incoming messages and outgoing messages. The clients are that chat users. They see the chat in a frame and can also write chat messages. These messages are sent over to the server. The server in turn updates all clients.
My problem is synchronisation of the clients. Since the server is multi-threaded, both messages can be received from clients and updates (in form of messages) have to be sent out aswell. Since each client is getting updated in in its own thread, there is no guarantee that all clients will receive the same messages. We have a snychronisation problem.
How do I solve it?
I have messed with timestamps and a buffer. But this is not a good solution again because there is no guarantee that after assigning a timestamp the message will be put into the buffer immediately afterwards.
I shall add that I do not know the clients. That is, I only have one open connection in each thread on the server. I do not have an array of clients or something like that to keep track of all the clients.
I suggest that you implement a queue for each client proxy (that's the object that manages the communication with each client).
Each iteration of your server object's (on its own thread) work:
1. It reads messages from the queues of all client proxies first
2. Decides if it needs to send out any messages based on its internal logic and incoming messages
3. Prepares and puts any outgoing messages to the queues of all its client proxies.
The client proxy thread work schedule is this:
1. Read from the communication.
2. Write to the queue from client proxy to server (if received any messages).
3. Read from the queue from server to client proxy.
4. Write to communication channel to client (if needed).
You may have to have a mutex on each queue.
Hope that helps

nodejs as websocket client

I need to send image to client whenever it's available in the server.
The client is not a browser but some kind of device(linux box) behind NAT (3G network) that will get the image as a file, write it to disk then display it in a screen (no browser here).
I'm new to nodejs and websocket.
Can I use a nodejs as a websocket client in the device and a nodejs as a server?
Can you provide an example of using nodejs to execute a websocket client?
In this case, can nodejs detect 3G network loss then resume or restart the websocket?
What happen if connection loss happen while nodejs server is pushing an image to the device nodejs (client)? is there any resume solution?
how about Binary Js to handle file transfer between the 2 nodejs?
Can I do that with some pub/sub mecanism using a MOM like ActiveMQ?
Socket.io (which is probably what you'll want to use server-side) has a client module that works in node. It handles maintaining a connection to the server.
What you'll probably want to do is not push the image itself over the WebSocket connection. Instead, send a "new image available" notification message over the WebSocket, and when the client receives the message, make a regular HTTP GET request to the server to download the image.
Since you're making a regular HTTP request, you can use the Range header if you need to resume an interrupted download.

Resources