Proper procedures for reconnecting WebRTC Peer connection? - javascript

I'm developing a group voice chat application using WebRTC and React.
I have managed to establish a connection and the voice chat works fine on first connection.
The way it works is like this:
User A joins chat room.
User B joins chat room.
User A enables audio through a toggle button - this notifies the signaling server that they're open for any WebRTC connections.
User B enables audio through a toggle button - they now through the server establish the WebRTC connection process to anyone who has enabled audio (being user A in this case).
This has worked fine so far. My issue is establishing a connection a second time around in the scenario of:
Both users have audio enabled. Voice comms working fine.
User B toggles audio - disabling it. User A remains active.
User B toggles audio again to reconnect to user A.
I get the following error:
DOMException: Failed to execute 'setRemoteDescription' on 'RTCPeerConnection': Failed to set remote answer sdp: Called in wrong state: kStable
I understand this error means that there's an issue with the connection order. So I figured this could be to do with not having a adequate disconnection process.
So I implemented a feature that when User B disables audio, it sends all other users in the room a message to remove that persons Peer_Connection object from the peer conection array on each users local state. But this is to no avail.
I'm confused as to why this is happening because if I exit the chat room and re-enter on both users, the connection can be re-established once more, so I assume it has something to do with there being remnants of the old connection causing issues with the new.
(It also works if both users have audio enabled, the one disables, leaves the room, comes back and enables all while the first user had it enabled the whole time. So an error on part with the client who sends the offer?)
As always, any tips and help would be appreciated, thank you.

Reconnection was not possible. For anyone seeking a solution for a similar issue:
Instead of reconnection, simple disable the audiotracks. Locally this means no one gets any audio feed from you. Disabling remote audiotracks mutes the audio for yourself obviously. Muting both means there's no audio coming in or out, which can simulate a disconnection with a toggle switch while the person remains in the same room.

Related

chat problem: socket-io goes offline when mobile is locked - how to see the newly received messages?

i am developing a social network with a chat messenger. it's a responsive web application.
the messages are saved on mongo db, and then sent instantly through socket io.
the problem is when computer is sleeping/mobile is locked,
the socket io connection goes off.
and when coming back from sleep/lock mode, the connection doesn't re-create.
let's say a user got couple of messages when his phone was locked, and the webpage was open there waiting for him.
even if i re-connect the socket when user starts interacting with webpage again, still the user will not see the instant messages he was getting, because the messages from mongo arrive only when entering the app for the first time.
the only solution i can think about is that when coming back from sleep, the web page should reset itself and loads again with all the data from mongo db.
do you have any better idea? is auto refreshing after detecting waking up from sleep mode, is even possible?

How to handle real time notification using WebSocket?

I've made real time chat app in React.js using Socket.io as milion of them in web. So now user A and user B can talk if they have open chat. I would like to add a new feature - when user A has open chat and user B has closed chat. When user A will send the message to user B he will get a popup/notification that he has new message and when he'll click on the notification he will open the chat and see the message.
I didn't found any solution here. Do U guys know how to solve this problem ? Is there any function in Socket.io to handle this ?
This is a very open question. By the way I'll try to explain the main concepts of a possible solution.
First of all, you need to consider that if you want real-time notification for "background-message" client B will need to keep connection open with socket (otherwise you'll need a sort of polling logic). Since B is connected to socket you can consider to work with Socket.io namespace in order to send this notification in another "room".
Another solution can be made by using Push Notification API provided by Firebase, AWS, etc.

Twilio Programmable Chat: No events fired after client reconnect

We are using the Twilio Programmable Chat JavaScript SDK (v. 4.0.0) to build a chat application.
We have several channels for each user and we listen on every channel for messages via the messageAdded-Event.
After a disconnect the client is reconnecting normally, but no events for messages sent in the meantime are fired.
Example:
User A: Client goes offline
User B: Sends message
User A: Client goes online
User A: Client is "connected"
User A: No Event for new message
We would expect that after the client has re-established connection we would get events for the "missed" messages/created channels; especially as the docs state (probably only meant for the client, but whats the point then?):
There is no need to implement shutdown/create cycle on network drops →
SDK reconnects automatically after regaining network.
Is this intended behaviour?
What would be the best way to retrieve "missed" events? Reload channels/messages manually?
(3) Another problem we discovered is, that sometimes the client is not able to reconnect at all, which results in a lot of Retrier attempt is already in progress, Twilsock: request timeout error messages. Any ideas on that?

How can I retrieve a list of websocket connections?

I'm working on a chatbot project where we open a websocket connection to communicate the chat messages.
Sometimes we get duplicate messages back and forth. I suspect that a second client is booted. I want to modify the boot() function so that it checks for existing websocket connections (and either cancel booting or close all other connections first).
Is there a way to get this from a vanilla javascript perspective? or do I maybe need to do something from the backend?
I tried to figure it out by looking at the code but without even a line number on which the error appears, it's pretty hard, anyways.
I suppose this is the problem here:
conn.send('USER_CONNECTED:' + conn.id); // send the connect message to ONE user
conn.broadcast('USER_DISCONNECTED:' + conn.id); // send the disconnect to ALL users
So it seems that you're simply missing a broadcast call when a user connects.

Twilio TaskRouter: change worker activity when disconnected

Let's say worker X logged in to our call center app and went online in twilio. He will now be able to receive reservations and respond to people. X lives in a third world country where the internet connection is very unreliable. His internet suddenly cut off while online and he is no longer able to respond to reservations. His activity is still Online and he has no way of changing it because he doesn't have an internet connection. Is there a way to tell twilio to go Offline when the WebSocket connection is lost between the client and the server?
I'm aware that we have disconnectActivitySid in TaskRouter.js but it is only fired on window.unload event and doesn't cover the cases where the browser crashes or we have internet connectivity problems.
Twilio developer evangelist here.
The only way to do this today would be to have the Worker receive a next Reservation, but have a Workflow ReservationTimeout of a semi-low value (10-15 seconds) and to utilize the Workspace’s TimeoutActivitySid to push the Worker to Offline.
With that setup, if the Worker loses connection (but is Idle), and receives a Reservation and is able to reconnect before that Reservation Times out (in that 15 second window), they will presented the Reservation with the JS SDK. So nothing will be lost. If they do not reconnect within that timeout window, the Worker will be pushed to Offline, and another Worker will be eligible to pick up the Task.
Let me know if that helps at all.

Categories

Resources