socket-controllers icon indicating copy to clipboard operation
socket-controllers copied to clipboard

How to use SocketIO() without OnMessage

Open ayonsaha2011 opened this issue 6 years ago • 10 comments

can we use socket broadcast to room without OnMessage i need to broadcast a emit on post save use like notification

ayonsaha2011 avatar Aug 29 '18 17:08 ayonsaha2011

can you explain more?

rustamwin avatar Dec 24 '18 11:12 rustamwin

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 ?

ikit avatar Oct 25 '19 09:10 ikit

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.

gabheadz avatar Nov 20 '19 16:11 gabheadz

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?

gottipatibalu avatar Jan 09 '20 01:01 gottipatibalu

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);

rustamwin avatar Jan 09 '20 08:01 rustamwin

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.

gottipatibalu avatar Jan 09 '20 12:01 gottipatibalu

So far this is a temporary solution.

rustamwin avatar Jan 09 '20 12:01 rustamwin

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');
    }

gottipatibalu avatar Jan 09 '20 14:01 gottipatibalu

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

stingers avatar Jul 15 '20 19:07 stingers

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

gottipatibalu avatar Jul 16 '20 00:07 gottipatibalu