How to close socket connection with already unauthenticated user? - node.js

There is system where users can be logged in and then do chat.
When the user is being authenticated the auth-identificator is saving to the session and session save to the Redis.
On the backend (PHP) user state (authenticated or no) based on the session value (auth-identificator).
Also the system has a chat based on nodejs and sockets.io.
There are 2 user types Admin and User and they can chat with each other.
When user/admin is being connected to the chat room or writes message in there, the nodejs gets headers and among the ones there are cookies, nodejs fetches SESSION_ID from those and check if that session is presented in Redis and has the auth-identificator if it is - we know that user is authenticated and can be connected to the room or we can save his message to the DB. Then received messsage is broadcasted to the users connected to room.
The problem is:
1 - Open browser, authenticate in the system by the admin and open chat
2 - Do the same for the user
Then these can communicate with each other.
BUT when user opens first browser tab with chat and on the second tab he gets logout, and this time admin will write message, already logged out user will continue receiving the messages.
So, the question is: how we can close socket connection with the user?
If user click Logout button, then we can send from PHP to nodejs the message via Redis pub/sub channels and nodejs will close connection, it's OK.
BUT when user open second browser tab and then will clean browser cookies he will immediatelly gets unauthenticated, but on the first tab he continues receive the messages. The same is true for admin.
How can we prevent it?
Thanks.

Related

How to send chat notificattion to user offline/online by socket.io?

I can send messages to others by socket when they are in a room but now I want to send a message when other users are not in a specific room or offline, as a notification.
You need to intergrate database with the socket application then only you store message offline and display to user when online.

How to send message after user's socket disconnect from the server

I'm using net module of node.js to build a chat server based on TCP. I have figured out how to handle the situation where two users both connect to the server. However, for a chat app, even if the user disconnect from the internet, people can still send message to those disconnected users. I just have no idea of how to achieve this.
You need to save messages in a database so that when they log in again you can retrieve sent messages and send them all at once.
There is no way to communicate to a user who has logged off. You just have to queue up the messages and deliver them when they reconnect.

Socket.io send data specific to each user

What is the best method to send user specific data from node server to client using socket io. Should I create each socket connections for users (probably hit the performance) or should I open each channel based on user.
----- Update on 09/16/2014-----
Recently i have tried creating multiple rooms corresponding to each userid based on login and in the UI layer i'm only getting user specific data. Everything works fine.
But if we look into Chrome Web Developer Websocket tab and click the connection and Frames subtab i could still see other user data. Does this means we have to spin off different socket connection for each user rather than creating rooms for user.
I have broadcasted data for 3 id
1.starts with dGhvY
2.starts with bnZIZ
3.starts with dXZIb
Please find screenshot to get more idea

Using socket.io for notifications and chat

I'm very new to node.js and sokcet.io that's why I need to ask you about the plan I have to see if it makes sense or is plain stupid. I need ongoing server/client communication for two reasons: sending real-time notifications to the user when they have one, and two, for chat between users.
Here is my plan for managing notifications:
PHP script finds out user X has a new notification.
Using Elephant.io send a message to server with user X's id as the data.
On the server side, upon receiving the message, if user X is connected emit him a message telling they have a notification.
user X's brower, Upon recieving the message, uses AJAX to poll the database and receive the text for the notification.
For chat, this is my plan (messages should be save on DB):
When user X submits a chat message to user Y, use ajax to send the text to a PHP script and save it on DB. On success, use elephant.io to send a message to user Y telling them that they have a new chat message.
User Y's browser, after receiving the server message, uses AJAX to poll a php script to receive the new text.
Do you think these plans are superior to short polling using AJAX? I appreciate any comments to improve them.
Finally,I'm curious to know how reliable these technologies (node.js, socket.io, elephant.io) are. Do they work well when the server becomes busy? How do they handle exceptions and errors ,etc.

Notifications about unread messages (node.js + sockets.io)

I have a system of personal messages between users on node.js sockets.io, is now faced with the problem that I do not understand how to make the notification about unread messages. Let's say, how to determine what a user who has received a message read this message, and how to make a notification message if the user is not in correspondence?
ps Chat History and active dialogs are stored in mysql.
Assume if the user has an open socket, and the message has been sent, then when the user clicks in a certain area of the page (perhaps the send message box?), an onClick event is fired to notify the server the user has received the message.
If the user is not on the webpage, there's not a way to contact them through sockets.io.

Resources