Apologies for any language barriers. I am facing an issue with my app.js server. I have implemented sockets and utilize the join event within the 'connection' event. A function inside the 'connection' event requires a socket parameter, and I am trying to assign the user to a specific room, whose name is stored in the model of my REST API section of the server (derived from the session). Now, the question arises. How can I assign a user who has connected to the correct room within my REST API model? (this model and its service are triggered when a user requests a page, similar to an authorization check). Additionally, there is another service and model responsible for tasks, handled through a REST API. Within this model, I aim to send messages to all necessary users via sockets when a task is added, excluding the sender. Currently, I am unable to achieve this, as the socket from the connection cannot be passed to the REST API model in any way. App.js
io.on('connection', (socket) => {
app.set('socket', socket);
socket.on('disconnect', () => {
socket.disconnect(true);
});
});
Controller responsible for sending data to all services and models
const controller = (ServiceClass, params) => {
return async (req, res, next) => {
const service = new ServiceClass({
session: req.session,
sessionId: req.sessionID,
cookies: req.cookies,
socketIo: req.app.get('socketio'),
socket: req.app.get('socket'),
});
const response = await service.run(params(req));
res.json(response);
};
}
export default controller;
Here, within the model triggered on every user request to the site, I am attempting to assign the user to the correct room
export default class IsLoggedService extends Service {
constructor(context) {
super(context);
}
async execute() {
this.context.socket
.join(`room${userSession.roleId}`);
}
}
I also inform the client about the created task using the rest api service and model
this.context.socket
.to(`room${userSession.roleId}`)
.emit('test', 'test');
I have thoroughly gone through the socket.io documentation, which suggests using a socket to send a message to everyone except the sender. However, this method is not working as expected, as the message is sent to everyone, including the sender. I have attempted to access the socket within the service and model, but to no avail.