1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Socket IO Broadcast from api/router when Heroku Scheduler executes - How?

Discussão em 'Angular' iniciado por K N, Julho 21, 2025.

  1. K N

    K N Guest

    I would like to broadcast or emit to all clients connected through SocketIO whenever a Heroku Scheduler job is executed. I have setup a Heroku Scheduler that executes every 10 min. It runs a GET request that sits in a module called router. The goal it to make updates to client every 10 min. I have tried out with broadcast and emit, but i am fairly new in this game.

    I am missing something - maybe SocketIO needs a client request and not a heroku scheduler job to executes, or my server setup is somehow broken. In any case it won't work.

    My router

    const express = require('express');
    const bodyParser = require('body-parser');
    const cors = require('cors');

    const app = express();
    var server = require('http').createServer(app);
    var io = require('socket.io')(server);

    const api = require('./routes/api');
    const PORT = [3000, process.env.PORT];

    app.use(cors());

    app.use(express.urlencoded({
    extended: true
    }));

    app.use(express.json());

    //Middleware that attach io to req so that I can use it in router.
    app.use(function (req, res, next) {
    req.io = io;
    next();
    })

    app.use('/api', api);

    server.listen(PORT[1], function(){
    console.log("app listen on localhost port something" )
    });


    My router (which is executed every 10 min.).

    router.get('/event/emitEventUpdates', (req, res)=>{
    //Every 10 min. the events on client must be updated
    console.log("i server emitEventUpdates");
    req.io.on('connection', function (socket) {
    socket.emit('message', {type:'new-message', text: "message from server"});
    server"});
    res.send({message : "all done from API"})
    })
    })


    My client is an Angular 6 app that makes connection. The Observable looks like this:

    let observable = new Observable(observer => {
    this.socket.on('message', (data) => {
    console.log("Received message from Websocket Server")
    })
    return () => {
    this.socket.disconnect();
    }
    });


    I would imagine that whenever the observable is active, the console should write something to the console. I can add additional information about it if needed, but I think the problem is server side.

    Continue reading...

Compartilhe esta Página