0

I am using Node.js with Express.js and for realtime data I am using socket.io.

I am trying to create on booking app.

So when the user will request through REST api the server will store the information to mongoDB via mongoose and after that the same data will be send to other users.

I am using router for different paths. below is my server.js

var express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const routes = require('./routes/routes');
const port = process.env.PORT || 8080;
app.use(express.json());
app.use(cors())
app.use(express.urlencoded({ extended: false }));
app.set('socketio', io);
app.use(routes);


mongoose.Promise = global.Promise;

mongoose.connect(
    'mongodb://localhost:27017/myapp', 
    { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, }
    ).then(() => {
        console.log('db connected');
    }).catch(err => {
        console.log(err,"hello 1998");
    });

server.listen(8080);

And below is my route

const { Router } = require('express');
var router = Router();
const { main } = require('../helper/db_main')
const { checkAuthentication } = require('../helper/auth')

router.use('/api/v1/services',main,checkAuthentication,require('../controllers/services'));
router.use('/api/v1/category',main,checkAuthentication,require('../controllers/category'));
router.use('/api/v1/socket',main,checkAuthentication,require('../controllers/socket'));


module.exports = router; 

and below is the place where I am trying to send/emit data to specific user but it is not working on client side i.e not able to see emit message on front-end side.

const list_all_category = (req,res) => {
    console.log("Hjdnckjsdck")
    var io = req.app.get('socketio');
    // global.io.to("notify_me").emit("message", "hello ftomr");
    let result_data 
    category.list_all_category().then(save_res => {
        if (save_res)
            result_data =  res.status(200).send(save_res)
        else{
            result = 'fail'
            res.send()
        }
    })

    console.log("Here is ninja",io.id)
    io.on('connection', function(socket){
        console.log(socket.id); // same respective alphanumeric id...
     })
    io.sockets.on('connect', function(socket) {
        const sessionID = socket.id;
        
        
    })
    io.on('notify',function(){
        console.log("Galgotia")
    })
    io.sockets.emit('chat_message', 'world');
   
}
Jasham
  • 191
  • 1
  • 3
  • 15
  • "it is not working" - *what* is the error/problem? – eol Aug 30 '20 at 16:25
  • basically I am not getting the notification on client side. – Jasham Aug 30 '20 at 16:26
  • I am not sure if you are correctly importing socket.io in list_all_category var io = req.app.get('socketio'); Check https://stackoverflow.com/questions/38511976/how-can-i-export-socket-io-into-other-modules-in-nodejs to import socket.io in other files. – Rupjyoti Aug 30 '20 at 16:44
  • Thanks I will try this. – Jasham Aug 30 '20 at 16:51
  • Thanks @Rupjyoti. I got the answer from below post. https://stackoverflow.com/a/37560779/9383409 – Jasham Aug 30 '20 at 20:27

1 Answers1

0

make use of this

socket.broadcast.to('ID').emit( 'send msg', {somedata : somedata_server} ); you can het each socket id for a specific user by using ${Socket.id}

If you are emitting anything always send the socket.id of a user to the client and send them back to the server.