1

I am creating a chat app in Android with Nodejs and Mongodb.

I get this error:

(node:49148) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Mo nitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

This is the code of js file.

const SocketServer = require('websocket').server
const http = require('http')
const express = require('express')
const app = express()
const mongoClient = require('mongodb').MongoClient

const server = http.createServer((req, res) => {})
const url = "mongodb://localhost:27017"


server.listen(50000, ()=>{
    console.log("Listening on port 50000...")
})

app.listen(60000, () => {
    console.log("Listening on port 60000...")
})

app.use(express.json())

mongoClient.connect(url,  (err, db) => {

    if (err) {
        console.log("Error while connecting mongo client")
    } else {

        const myDb = db.db('myDb')
        const collection = myDb.collection('myTable')

        app.post('/signup', (req, res) => {

            const newUser = {
                name: req.body.name,
                email: req.body.email,
                password: req.body.password
            }

            const query = { email: newUser.email }

            collection.findOne(query, (err, result) => {

                if (result == null) {
                    collection.insertOne(newUser, (err, result) => {
                        res.status(200).send()
                    })
                } else {
                    res.status(400).send()
                }

            })

        })

        app.post('/login', (req, res) => {

            const query = {
                email: req.body.email, 
                password: req.body.password
            }

            collection.findOne(query, (err, result) => {

                if (result != null) {

                    const objToSend = {
                        name: result.name,
                        email: result.email
                    }

                    res.status(200).send(JSON.stringify(objToSend))

                } else {
                    res.status(404).send()
                }

            })

        })

    }

})

wsServer = new SocketServer({httpServer:server})

const connections = []

wsServer.on('request', (req) => {
    const connection = req.accept()
    console.log('new connection')
    connections.push(connection)

    connection.on('message', (mes) => {
        connections.forEach(element => {
            if (element != connection)
                element.sendUTF(mes.utf8Data)
        })
    })

    connection.on('close', (resCode, des) => {
        console.log('connection closed')
        connections.splice(connections.indexOf(connection), 1)
    })

})
halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

0

this is just the warning shown by mongodb package to add option of { useUnifiedTopology: true } to connect function of mongodb. this is problem is already solved Please follow the link below

Warning on Connecting to MongoDB with a Node server

0

I had the same issue at the moment of starting the application. So I rolled back the mongoose version on the package-lock.json & package.json to mongoose: ^5.8.11. And this resolved the issue for me.

Changes made... only by downgrading the mongoose version would be enough in some cases.

0

In order to resolve this deprecation message, you can pass in an additional options argument to mongoClient.connect(...).

mongoClient.connect(url,  { useUnifiedTopology: true }, (err, db) => {

This should resolve the issue for now. In future versions, it's not needed anymore, since this will be the default behavior.

More information on useUnifiedTopology can be found here: https://mongodb.github.io/node-mongodb-native/3.3/reference/unified-topology/

At the time of writing the node driver has seven topology classes, including the newly introduced unified topology. Each legacy topology type from the core module targets a supported topology class: Replica Sets, Sharded Deployments (mongos) and Standalone servers. On top of each of these rests a thin topology wrapper from the “native” layer which introduces the concept of a “disconnect handler”, essentially a callback queue for handling naive retryability.

The goal of the unified topology is threefold: - fully support the drivers Server Discovery and Monitoring, Server Selection and Max Staleness specifications - reduce the maintenance burden of supporting the topology layer in the driver by modeling all supported topology types with a single engine - remove confusing functionality which could be potentially dangerous for our users

Fabian Strathaus
  • 3,192
  • 1
  • 4
  • 26