How can I retrieve a list of websocket connections? - javascript

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.

Related

Sending HTTP messages to a specific client connection/web session using cURL/NodeJs scripts with SSEs

Hi so first I apologize if my query may seem unclear, it’s first trying to do what I’m doing and I haven’t full idea around the intricacies and lingo lol.
So basically I’m running a NodeJs web server with React handling my front end. I’ve got Express to help manipulate user sessions and I just came by Server-Sent-Events as a way to send one-way messages(which is what I need to do). So far I’m able to send updates and messages via cURL on the terminal and running JS scripts, however these updates/messages go to every active client session but I want/need to be able to send these messages to specific active client sessions/connections.
Example: 5 client connections are established (session IDs A,B,C,D,E), now I want to send an alert message to session E only and manually.
I’m still green with NodeJs/Express and the concept of SSEs however I’m learning as I go for this pet project.
Send help
What you want is how SSE works. It is a dedicated connection between a client and a server process.
however these updates/messages go to every active client session
If that is what you see then your node script is running the exact same code for each client.
I think your question might be higher up - how to organize the data messaging? That is too big a topic for a single StackOverflow question, because it will depend on so many factors specific to your use case.
But one way would be to have an SQL database, with one record for each user. The node script polls that database table and if the record for the current user changes, it sends the new data to them. Then to send data to user E, you just edit the database record for user E.

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.

Confirm is prompt one by one on users screen javascript

I'm making project using socket.io nodejs,When server emits to all users in a room, In client side there is confrim javascript function,Which is being called one by one in all users screen,below is client side code,
socket.on('resetcount',function(){
if (confirm('Do you want to reset board?')) {
socket.emit('count',roomnumber);
}
});
Is it possible that it will prompt at the same time in all users screen, Or socket.io server works this way?
exactly the same time is very difficult. If you fire an event, you are stuck with rendering time of the client (browser/internet connection /hardware). But if approximately(+- couple of sec) the same time is acceptable for you. Then socket.io is the way to go

Resuming XMPP connection upon page refresh

I am not able to maintain the connection on page refresh.
Initially upon every refresh Strophe connection object is lost. So, the first approach I took was reconnecting on refresh.
I am saving the JID, SID and RID in localStorage send the strophe attach call with saved details.
AuthHandler.attach_params(jid_frm_storage, sid_frm_storage, rid_frm_storage);
The attach callback is in actual AuthHandler Object that I have
created and is logging perfectly that means it is okay to assume that
my attach was successful but still I am not able to send or receive
any messages
My second approach I saved the latest RID in locastorage and used it to attach as I came to know upon researching on already posted problems online that RID changes frequently. (Although please note I am saving RID only when either i send message or receive a message).
I also tried sending presence.
ConnectionHandler.connection.send($pres());
But no luck here either!
In my last approach I re-authorized, .i.e I connected and re-logged in and then sent the auth, still no luck.
How can I resume my connection upon refresh. What are the things That I need to do from Strophes end in order to reconnect and resume the same conversation.
NOTE: I am not that clear with how XMPP works internally but I have been able to push this far by learning-by-doing approach. I know it would be great to wait and learn a bit on xmpp before rushing forward but I can't afford that right now as we are prototyping fast.
Will really appreciate if steps required to accomplish this can be explained in detail.
Solved this:
The best way to solve this is by saving the login credentials in localStorage (mind you it might not meet your security requirements, met mine though) and then make a proper connection again, but this time with same credentials as before.

Push a notification to another user

This question isn't a code-level issue but merely a functionality question / brainstorm.
Within my PHP script I want to send a notification to another user in real time, there's 1 way I've thought of to do this, if you know any better ones be sure to leave them in the comments!
My idea for this functionality is to insert into a databse table with the user's id and the message, then on the user's end constantly loop a select request looking for notifications corresponding to their id within $_SESSION, if it's found a message then delete it from the table and display it to the user.
This seems like it could "strain" my database and I'm wondering if there's a cleaner way to do this, it would also be much appreciated if somebody could post a javascript loop with a 3 second delay and an ajax post to a php file within it,
Thanks all,
James
The cleaner way to do this would be with websockets. Polling, long polling, and streaming are going to have exactly the problem you thought you were going to have.
The message recipient needs to be listening via broadcast also through websockets. The server will notify all websockets listening for that particular event.
You don't want to block with database read and writes. Just take the action from one user and send it to all the other websockets listening for that event (the other user's client side instances)
For event history you would consider persisting to a database with a message queue.
With a properly indexed, well-structured table, it won't be a strain to the db at all. Of course, this assumes your interval is reasonable (3 seconds you mentioned is great). That's how all real-time session-checking websites work. Those that need more than that, such as chat systems, basically anything that passes data more frequently and/or in larger packets, they use websockets.
Use Websockets or Server-Sent-Events
just an idea:
User-1 send message to the Server {"message" : "hello", "target" : "User-2"}
Server checks the message and redirect it to the target User
User-2 listening for events from Websocket or Server-Sent-Events

Categories

Resources