Socket.IO with multipul tabs of browser - javascript

I am using socket.io in my node.js application to give real time experience to my users. But due to leak of my experience with socket.io, I have some doubt with browser tab management. Let me explain first.
My website does not allowed login to user from multipul browser at a time. means If someone login from one browser, and then try to login from another browser, I have to kill previous login session. Means my socket.io emit messages to previous browser's all tab for logout, but my second browser's all tab should not get message for logout. How Do I do this?
Another question is I want to count distinct logged in users for my deshboard. But with multipul tabs, count is showing wrong figger. (eg. Single user accessing website from single browser but from 2 or more tabs, on server, socket client is showing each connection for tab, Here I need just one count. How Do I get it?
If some one has example/sample regarding above user case, please share it, so new coumer will gets help from it.
thanks

Related

How can I check whether some specific user has an internet connection?

I maintain a system built with Ruby on Rails. The client has asked me to prevent the system from working when a specific user has no internet connection.
How would you do it?
I need to check if a specific computer has internet connection!
For instance if my pc or your pc or my mother's pc has no internet connection!
This has nothing to do with Ruby on Rails.
Is this user accessing a site that's on a local network but no outside internet access? If so you can have a before_filter or some kind of method on your homepage that tries to ping google.com or some other site outside of the LAN to check for internet.
Use Offline.js
Add offline.min.js file in your vendor/assets/javascripts
Require it in your app/assets/javascripts/application.js as //= require offline.min
Add the following code in the same application.js file
$(document).ready(function() {
Offline.check();
Offline.on('up', function() {
$('a.btn').removeClass('disabled');
});
Offline.on('down', function() {
$('a.btn').addClass('disabled');
});
});
This is in relevance to my code where I just disable all buttons when the connection goes off.
The default behaviour of Offline.js adds a overlay with a please wait message until the internet connection comes back on.
I prefer to handle it this way as it give better user experience. Disabling buttons is a subtle way of preventing a user from working.
So just to clarify, your Rails application exists for users to send food delivery requests to one specific user -- the shop preparing and sending that food out, presumably -- and you want users to be unable to create new delivery requests when the shop/owner isn't currently online?
The simplest way to implement this would be to have an Admin Dashboard area where the owner can click a button to 'open the shop' and allow new orders. The worry, then, is about the shop's connection going offline, resulting in customers whose order are never received. To get around that, you can have each customer order start in an 'unconfirmed' state; when a new order is submitted, the owner is notified, and he can click a button to confirm it, letting the user know that yes, their order has been seen and is being made (and their credit card can be charged then, if you have online payment). This is probably the most reliable solution because what you really want is confirmation that a human has acknowledged the order.
If you do want to tie it directly to online status, then you can have the Admin Dashboard page regularly check that it's being viewed. You could accomplish this with a websockets connection, see Action Cable introduced in Rails 5. The Admin Dashboard page opens a websocket connection upon login; the new-connection event sets accepting_orders = true. The websocket disconnect event sets accepting_orders = false, which will happen soon after their connection is dropped (make sure to check the timeout settings). Without websockets, you could have a background HTTP request occurring on an interval; x seconds without receiving that request could cause Rails to stop accepting new orders.

Allow same user multiple login on different number of devices and how to deal with sessions?

I'm building a chat application with some php, mysql, nodejs, socketio.
I want to allow users to be logged in from nomatterwhat number devices. Like eg. Facebook where it doesn't matter which screen you're looking at. They all reflect the latest changes.
Tested it right now and it works that way. If i open the notifidations the number dissappears on the same page on different devices.
My guess -and started building with that iedea in mind – was that a room is created for each user when a user logs in the first time and any following login from the same user is also added to that room so the changes can easily be broadcasted to the room.
All browsers are aware off the multiple logins because i send a soclet-message to the room setting a variable - multilogin- so that it knows that there are more.
Also needed to see when a user clicks on the logout-buttons it send that information along.
If it is the only one logged in it should kill the session set the user offline.
Actions performed on one device, broadcasts it to the other members in the room etc.
Seems logical?
It works so far but i'm uncertain about the session part of this setup.
The logic in php -inherited from the origanal build of the chat – checks the user logging in to see if the user is already logged in and if so, it destroys the existing session first and then sends a loggout command -which i prevent now when the multilogin parameter is send along- to the other browser.
My question now is, what could be a logical approach towards the sessions?
I was playing with some ideas in my head and then i thought, let's look on the internet about the subject.
An idea could be that only the first loggin in gets the session created and is shared by sending the id to the others so they can be identified as being the same user.
Would that be an approach that one would suggest?
Found a simple solution by letting each device with same user that logs in getting a new sessionid which is send to the others (socket message to room) so their sessionid becomes the same to authenticate themselves to the server. Session id is save enough for that purpose.

Websocket with AngularJS/Asp.net

I had a specific questin about angularjs with websocket. I currently have an application that utilizes a websocket to communicate with a server, this is all nice and dandy - and when I move around pages in angular the websocket persists throughout all of the routes which is neat. Unfortunately the problem is that if the user refreshes the page (for some dumb reason), the websocket disconnects. I was wondering what the best method of handling this is. Should I just have an alert when the user tries to refresh, can I somehow detect that the websocket is closed when the page is refreshed and start a new one? I'm just wondering what the best practice for something like this is.
Thanks
There is nothing you can do, if the user refreshes, it is like restarting an application, all the bootstrapping happens again and connections are created again.
You can use javascript:onbeforeunload to warn the user that if refreshes or leaves he will lose the connection. But your users will hate your for that, it is very annoying.
Consider as well, that the user may open several tabs.
Starting a new connection is the best way. Just make sure that the user can somehow recover his context. If there is a different context per tab, then you will have to put a connectionID parameter in the URL to persist it through refreshes, and if the context is per user session, then a cookie with the session ID will do.

Check if user refreshed, went back, closed browser etc.. socket.io

I have an application where I use socket.io alongside node.js. I can't find / figure out a way to check for events when users leave application i.e. close browser, refresh page, go back etc.. every one is connected in specific room. I need to know when someone leaves the room, know what their socket.io id was etc.. is there a method to achieve this?
Every time a user destroys the JS context by changing page (going back or forward, refreshing, closing the browser, etc), the Socket.io connection is closed, and that's how you get to know that (listen for the .on('disconnect') event on the server).
To deal with rooms, you may want to look at this page: http://socket.io/docs/rooms-and-namespaces/

Multithreading in chat applications in php

I am trying to create a chat application in PHP using AJAX and javascript. When I try to post messages in the same browser window, the messages are posted and displayed properly. But when I opened another window and tried to send message to the first window user, it does it, but when next time I post message from window 1, it posts as if it is posting from window 2 user. I am using sessions to record username, and I believe that everytime a window is loaded, the value in the session variable of user changes and it affects the behaviour of the application in all the windows. ie, username stored as session variable in window 1 is overridden by username in window 2, because both are accessing the same piece of code. And everytime AJAX tries to get new messages from database, it gets for a different user when a new window is loaded.
I was wondering if this could be solved by Multithreading, or if there is any other way to solve this. Do chat applications require Multithreading always? It is quite confusing to me. Please help.
If you want to simulate two different users who should have different sessions who are chatting with each other you can open two different browsers (for example Internet Explorer and Firefox) and log in with one user on each browser. Then the session is not shared.
As far as i know different browser windows in same browser share sessions so the later login will overwrite the first login.

Categories