0

i have a nodejs server which is getting list of a collection from mongodb . Here is its code . since am new to sockets so ..

const express = require("express");
const app = express();
const http = require("http").Server(app);
const socketio = require('socket.io');

after that iam simply getting data in a route . and one thing more all code is in one file and i do need express route as there are other routes in app. here is the mongodb code for getting list

app.post("/getAllOfferManagement",
    async (req, res) => {
        try {
            MongoClient.connect(url,
                function(err, db) {
                    if (err) throw err;
                    var dbo = db.db("realtime");
                    dbo
                        .collection("offer")
                        .find({})
                        .toArray(function(err,
                            result) {
                            if (err) throw err;
                            // console.log('getting it ');
                            res.send(result);
                            db.close();
                        });
                });
        } catch (err) {
            res.send("error");
        }
    }); // its all working fine when i hit the route

http.listen(5000, function() {
    console.log("Server Started!");
});
//serversidecode ends here

Now am getting the data through angular and here is the code for it

$scope.getAllOffer = function() {

    $scope.mongoloader = true;

    //nodejs api endpoint 
    $http.post("http://localhost:5000/getAllOffer").then(function(res) {
        $scope.offersArray = res.data;
        console.log('data here', res.data);
    });
};

the above works fine . but i need to get data in realtime e.g when somone insert new doc in mongodb the the view get updates . am new to sockets so any help is appreciated. Thanks

Molda
  • 5,619
  • 2
  • 23
  • 39

1 Answers1

0

For this u have to add an event to backend and as well as in frontend Backend

io.on('connection', (socket) => {

    console.log(socket.id);


    socket.on('SEND_TITLE', function(data){

        io.emit('RECEIVE_TITLE', data);

        console.log('data',data)

    })

});

For frontend u have to use socket io client module

import io from "socket.io-client";
socket = io('your backend host url')

    socket.on('RECEIVE_TITLE', function(data)
      Console. Log(data);

  });

Frontend syntax could differ in angular. As I am not familiar with angular

For more information visit.

Forclient side.

https://socket.io/docs/client-api/

For server.

https://socket.io/docs/server-api/

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Shubham Tiwari
  • 1,761
  • 11
  • 19
  • well thanks for reply . But i have an api route . I need to use socket only on that api endpoint . I dont know how to use sockets inside express route – ironHide214 Mar 11 '19 at 10:02
  • I think this would help you. [https://stackoverflow.com/questions/47837685/use-socket-io-in-expressjs-routes-instead-of-in-main-server-js-file] – Shubham Tiwari Mar 11 '19 at 10:22