Status socket (online, inactive, offline)

#1

Hey :wink:

I have 3 user status:

  • Online
  • Inactive
  • Offline

I would like to know how I can put the status of the user “Inactive” when he closes his browser, the session remains connected, but the socket is closed, and so it puts the user offline.

Services/SocketService.js :

let socketList = {};
const User = use('App/Models/User');

class SocketService {

	async Add(userId, socketId) {
		socketList[userId] = socketId;
		console.log('SocketService Add:', userId);
		const userSocket = await User.findByOrFail('id', userId);
		const userProfil = await userSocket.profile().fetch()
		if (userSocket) {
			console.log('SocketService Update DB Socket');
			userProfil.status = 'online';
			await userProfil.save();
		} else {

		}
	}

	async Away(userId){
		console.log('SocketService Away:', userId);
		if (!socketList[userId]) return;
		const userSocket = await User.findByOrFail('id', userId);
		const userProfil = await userSocket.profile().fetch()
		if (!userSocket) return;
		userProfil.status = 'away';
		await userProfil.save();
	}

	async Delete(userId) {
		console.log('SocketService Delete:', userId);
		if (!socketList[userId]) return;
		delete socketList[userId];
		console.log('SocketService Deleted');
		const userSocket = await User.findByOrFail('id', userId);
		const userProfil = await userSocket.profile().fetch()
		if (!userSocket) return;
		userProfil.status = 'offline';
		await userProfil.save();
	}

	async Get(userId) {

	}

	ToArray() {

	}

}

module.exports = new SocketService();

Ws/NotificationController.js (server) :

'use strict'

const SocketService = use('App/Services/SocketService');

class NotificationController {
  constructor ({ socket, request, auth }) {
    this.socket = socket
    this.request = request
    this.auth = auth
    this.PushSocket().then(() =>
      console.log('connected socket', socket.id, auth.user.id, socket.topic)
    )
  }

  async PushSocket() {
    await SocketService.Add(this.auth.user.id, this.socket.id);
  }

  async DeleteSocket() {
    await SocketService.Delete(this.auth.user.id);
  }

  onStatus(message) {
    this.socket.broadcastToAll('status', message)
  }

  onMessage(message){
    this.socket.broadcastToAll('message', message)
  }

  onClose() {
    this.DeleteSocket().then(() =>
      console.log('disconnected socket', this.socket.id)
    );
  }
}

module.exports = NotificationController

js/script.js (client) :

'use strict'

class WebSockets {
	constructor() {
		this.ws = adonis.Ws('', {path: 'ws'}).connect()
		this.IsConnected = false;
	}
	Connects() {
		if (this.IsConnected) return;
		console.log('Websocket - Connect');
		this.ws.on('open', () => this.BindEvents());
		this.ws.on('error', error => console.log('WebSocket Error', error));
		this.ws.on('close', () => {
			console.log('Websocket - Close');
			this.IsConnected = false;
		});
	}
	BindEvents() {
		const notif = this.ws.getSubscription('notifications') || this.ws.subscribe('notifications');
		notif.on('ready', () => {
			console.log('Websocket - Ready');
			this.IsConnected = true;
			notif.emit('status', 'online');
		});
		notif.on('status', message => {
			console.log(message)
			$('.nav-userpic .icon-status').removeClass('away offline').addClass(message)
		});
	}
	Disconnect() {
		if (!this.IsConnected) return;
		this.ws.close();
	}
}

const Wss = new WebSockets();
export default Wss;

Thanks :wink:

0 Likes