Connecting to specific clients in a chat app - javascript

Im making a simple chat app that sends messages through a redis pub/sub channel. I do not use any login options just lets the user select a username and chat. I am trying to implement private chat where one client is able to communicate to another connect client. I am new to chat client programming, I am trying to figure out how to communicate with only one client. What are some options I could consider to get the ability to chat 1 on 1? I don't mind changing my architecture to incorporate a new tool. Any suggestions is appreciated

I did something similar with raw sockets. I held user objects which contained the connected socket, the name, and whether they are chatting. If a user is not in chat he can type a command /list users which gives the list of users currently connected. When he has found someone to chat with he enters the command /chat bob. If the user bob is already chatting he will receive a message from the server saying that bob is in a chat already. Otherwise a chat session will be established between the two users. They now will be in chat until one of them enters /endchat. Then they will be able to find new people to chat with.
The trick is implementing the server so it can track a list of users and their chatting status and who they are in chat with so when ever they type /endchat the other user can be notified.

The combination of Nodejs + Socket.io would solve your problem i suppose. Specifically rooms functionality is what you are looking for here.
Lets say you have two users bob and gob. Considering both are online (although you can add Redis as a session store and for storing history later, which i did in my case). Say bob wants to chat with gob he selects gob. Now make bob join a room using
var roomname = 'bob'>'gob'?'bob'+'gob':'gob'+'bob' ;
//this type of naming ensures roomname is same for both users
socket.join(roomname);
This will work only if both have chosen each other. So now when gob selects bob both are joined in the same room. Whenever someone sends a message make them emit to that room using
socket.broadcast.to(roomname).emit('message_event',msgObj);
Make sure you have have added the listener
socket.on('message_event',messageHandler);
When chat is finished make them leave the room using
socket.leave(roomname);

Related

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.

We are using angular-agora-rtc web, how to check user is active in Agora server.?

We are using sample from https://github.com/AgoraIO-Community/Angular-Agora-RTC The channel name is hard coded in the sample. When 2 users need to connect for a call. Ideally both should be on same channel. Is there a way for user-1 to check whether user-2 is online using agora-web-sdk?. If the user is online, how can user-1 share a channel-id with user-2?
One-to-one video call implementation using agoara sdk. For 2 user to have a call, they should be in same channel. Link above has the sample I am trying. Not sure how two user share same channel name with each other being in 2 different mediums at real-time.
https://github.com/AgoraIO-Community/Angular-Agora-RTC
If agoara provides an API to check if user-2 or user-1 is online after these user's have created clients in web. Is there a way to exchange "channel id" between active users.
There is no such answer to this question anywhere on internet believe me i have searched for it, only thing that came to my mind is to use some type of User Base like Firebase, PHP MySQL etc to setup user and then login those users in your app.
After logging in, set various flags for user presence like is_online, last_active, is_broadcasting, is_audience etc to determine which user is online.
You can then put channel name along with other user details when the user login and starts the Broadcast.
After the user is broadcasting, use those flags you used before and query them to get the broadcasting users and show them where ever you want for other users to join their broadcast.
This as simple as it can be in terms of explaining a really frustrating problem but in the end its only you who would have to implement any type of logic you can come up with.

Tokbox Destroy Session - By Admin Not Part of Session

We are using Tokbox to implement a set of pre-defined chat rooms.
Each room has a moderator and a specific group of users who can join the chat room only when the moderator has activated the chat room.
This has gone very well and quite smoothly.
My question is: How could we set up say a "super user" so that they may terminate any active chat session that might be going on? This superuser is not necessarily an active participant in any room. But, we would like to set up some sort of admin page where there is a button for each room that says "emergency terminate" or something like that.
We have database roles and user setup. We are just trying to figure out the best approach to do this with the Tokbox API.
TokBox Developer Evangelist here.
A client connected to a session with a moderator token can disconnect other connected clients in that session.
There is no concept of "super user", but you can use the use the OpenTok REST API to force disconnect connected clients from any session. You can also use the OpenTok PHP SDK to accomplish this.
use OpenTok\OpenTok;
$opentok = new OpenTok($apiKey, $apiSecret);
$sessionId = ""; // the session the client is connected to
$connectionId = ""; // this connection Id of client you want to force disconnect
$opentok->forceDisconnect($sessionId, $connectionId);
As you can see, you would need to know the connectionId of the client that you want to disconnect. The connectionId is part of the Connection Event that is dispatched with connectionCreated and connectionDestroyed events on the client side. You can also use Session Monitoring to receive these connection events via a webhook on your server.

Sent to specific clients/groups?

Let's say I'm making a chat, is it possible to sent messages to specific users and particularly groups of users? Can I hook up ID's to clients that have been logged on and groups that have been started, so I can sent messages to the server using these ID's and the server just sends these messages to the correct people? The chat app would look much like a chat from an MMO game where you can receive broadcast (from entire network), room, private, group and party messages in the same window.
Also I have a database behind this, is it possible I can use this for the above? Like using the same user ID's as in the database? Likewise I'm storing groups with an ID.
You can create rooms or even private chats using web sockets, there are lots of examples out there. I have only used Socket.IO and they are finally 1.0 production ready. Check their docs: http://socket.io/docs/rooms-and-namespaces/
// Join
io.on('connection', function(socket){
socket.join('some room');
});
// Emit
io.to('some room').emit('some event'):

Socket IO : Best way to retrieve socket of specific connected client

I am trying build a chat server using MEAN Stack (not using redis), which uses socket.io for enabling real time chat. For private messaging, I want to implement the built-in "room" feature of socket.io. The flow goes as follows :
User1 wants to start a conversation with User2, so User1 emits an event, which the server catches.
In the server, I store this conversation in database and join User1 to a room. If User2 is online, then I join User2 also to this room. For achieving this step, I need access to the socket of User2.
After a good amount of research, I found this method to store the "socket" of every connected user in an array(or dictionary).
I was wondering if there is any other method to retrieve the socket other than storing it in an array.
No, no better way, except if you use different namespace for the two users, but i don't think it's your case.
I implemented a basic POC (proof of concept) a couple years ago using faye.
In my case I created a unique channel for active users. The channel name was the same as the unique user identifier from mongo ( _id ). This allowed me to send messages to the correct user without having to store information about their connection.
Please note that I did not do any performance testing and that you would need to have a lot of connections open.
I did a similar implementation using faye by broadcasting to channels, in that case I did store in the database who was connected to a specific channel.

Categories

Resources