socket-controllers
socket-controllers copied to clipboard
How to use SocketIO() without OnMessage
can we use socket broadcast to room without OnMessage i need to broadcast a emit on post save use like notification
can you explain more?
I think I have a similar need. My server is running some tasks and I want to inform all clients of the progress of these tasks... So the question is: How to send a message to all connected clients connected from a server-side event ?
Same need here. Basically actual functionality is great for a request-response model. But in @ikit scenario there should be a way for the backend to send messages on a discretional basis.
I think I have a similar need. My server is running some tasks and I want to inform all clients of the progress of these tasks... So the question is: How to send a message to all connected clients connected from a server-side event ?
Did you find any way to achieve this?
How about this:
const app = require("express")();
const server = require("http").Server(app);
const io = require("socket.io")(server);
app.io = io;
server.listen(3001);
app.get("/", function (req: any, res: any) {
req.app.io.broadcast.emit('hello', 'Hello everyone');
});
useSocketServer(io);
How about this:
const app = require("express")(); const server = require("http").Server(app); const io = require("socket.io")(server); app.io = io; server.listen(3001); app.get("/", function (req: any, res: any) { req.app.io.broadcast.emit('hello', 'Hello everyone'); }); useSocketServer(io);
Thank you. I am kind of using like this as a workaround. Just wondering if there is a right way to do it.
So far this is a temporary solution.
Sure Thank you. I created a decorator to hide that logic and accessing it in my routes.
@Get('/checkStatus')
public async checkStatus(@socket() socketIo: Server) {
socketIo.emit('hello', 'Hello everyone');
}
hello, @gottipatibalu,
Sure Thank you. I created a decorator to hide that logic and accessing it in my routes.
it possible to share or explain how to do that. Thanks
Set socket object on express object like described above and use below decorator to access it from the route Here is the decorator decorator I created.
export function socket(options?: { required?: boolean }) {
return createParamDecorator({
required: options && options.required ? true : false,
value: (action) => {
if (action.request.app) {
return action.request.app.get('socketio');
}
return undefined;
}
});
}
Route Example
@Get('/checkStatus')
public async checkStatus(@socket() socketIo: Server) {
socketIo.emit('hello', 'Hello everyone');
}
I am using routing-controllers to define my routes