score:1
To keep the online status, you need to keep the users's online status in the database.
when a user is connected set it as online and on disconnect set it as offline. You can use a socket event to emit the users online status so that it will be reflected realtime
chatUtils
const redis = require('socket.io-redis');
module.exports = (socket)=>{
global.io.adapter(redis({ host: redisUrl, port: redisPort}));
global.io.on('connection', (socket) => {
try{
// Replace setOnlineOrOffline with your method .
setOnlineOrOffline(socket.handshake.query.userId,socket.id,true);
}
catch(e){
console.error(e)
}
socket.on('disconnect', (a) => {
// Replace setOnlineOrOffline with your method .
setOnlineOrOffline(null,socket.id,false);
});
});
};
App.js add
let socketio = require('socket.io');
let chatUtils = require('path-to-chat-util');
var socket = socketio(http, {
transports: ['websocket', 'polling'],
pingInterval: 60000
});
chatUtils(socket);
setOnlineOrOffline Function
module.exports.setOnlineOrOffline = (userId,socketId,status)=>{
if(userId){
// save userId,socketId,status To DB, keep socketId in an array since same user can connect from multiple browser tabs.
// your save to db method.
// emit the status to all users using, add necessary code to send data to all users.
global.io.emit('onlineStatus',status);
return
}
}
If you planning to scale the server horizontally
, use socket.io-redis
module, and keep a redis server
running outside the application servers
.
Source: stackoverflow.com
Related Query
- How to check online user in nodejs, socketio?
- How do I check if the user clicked inside the current component?
- How can I check if user exists in Firebase?
- How to check if a user is logged in and how to save their information? (React)
- How to check if user is confirmed in AWS Amplify?
- How do I check if user is 'logged in' in a react component when using laravel authentication?
- How to get JWT cookies in our react application, how to check the user is login or not I am unable to find how to handle my react application session
- I want to check if user is online in a React app?
- How to use useEffect and the Context API to check if a user is logged in and protect a route?
- Reactjs - How do I check to see if a user clicks a textbox within a table row?
- How to check if user is logged by localStorage and Redirect according to it?
- React - How to check if user is still authenticated
- How can i check if user is authenticated more efficently in react?
- How check if user scrolled to top in react native
- How to show raspberrypi sensor data in react using nodejs and socketio
- How to make React check the user input from the first character?
- How to check if user input is valid?
- Redux how to check if user is logged in inside auth reducer state parameter using sessions
- How to check if the user has internet connection in React
- How to check online status in React.js?
- How to check if the user entered input in input field has the string in format '10h' or '10m' using react and javascript?
- How to check if user is already in "/home" page using react and typescript?
- How to check a user input matches the particular regular expression in react?
- How to stop user from accessing nodejs express routes
- ReactNative: How to check if the user has granted permission to gps
- How to check user authentication in GET method?
- How to check if user already exists firebase with React js
- How to check "Type" user passed in custom hook in React typescript?
- How to check if token is undefined for user authentication
- How to show how long user has been online on my app. "David, online for 12 minutes"
More Query from same tag
- How do I pass a value into an GraphQL query using gatsby and react?
- How to get value reusable Autocomplete component using Formik with Material-UI
- fetch gives response Unexpected token < in JSON
- React is not importing redux action even tho it is exported
- How to map JSON data as a table in React
- Label in BarChart?
- Can you, or should you use localStorage in Redux's initial state?
- React + Meteor: basic dumb component returns undefined
- Jest & react-testing-library - add test for conditionally rendered children
- How do I split string value with Regex expression and return an array and object?
- Can't understand how passing function to update state hook works
- Materia UI simple select not saving selection
- React.js + Immutable.js: best practices for shouldComponentUpdate with filtered lists
- In formik, shorthand input field is not working
- React router v4 nesting
- Using object in ReactJS Context - method in object causes Property 'myMethod' does not exist on type 'unknown'
- animate text in React
- Implantation of counter in react has a value of 0 when I reference in an empty useEffect
- Uncaught TypeError: (0 , _arrays2.default) is not a function
- Using injectGlobal styled-components API with typescript
- Subclassing in React
- Jest mock localStorage methods
- Getting some syntax error while mapping-reactjs
- How to apply innerClass style to searchBox in appBase?
- useSWR mutate function not working as expected
- Programmatically set params in React Router v4
- I render multiple props in one component instead of multiple props in multiple component
- Box-shadow is not applied to element, how to fix that?
- ReactJs dynamic routing code spliting
- useRef current getting its value only on second update