Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection event open is not equivalent to connected #10886

Closed
mpodolsk opened this issue Oct 15, 2021 · 4 comments
Closed

connection event open is not equivalent to connected #10886

mpodolsk opened this issue Oct 15, 2021 · 4 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@mpodolsk
Copy link

mpodolsk commented Oct 15, 2021

its either a bug or bad documentation.

mongoose.connection emitted connected but never open.

as per docs

open: Equivalent to connected

I used in my app connected event as a ready signal to make sure my app was ready to receive traffic.

what ended up happening is that the app was receiving traffic it was not able to service.

the open event was never emitted and I think that is the one needed for mongoose to operate properly?

Looking at the source code, open and connected are not exactly identical. It would be nice to note the difference even if they are subtle.

    "mongoose": "^5.13.10",
@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Oct 18, 2021
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');


async function test() {
    await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
    useUnifiedTopology: true,});
    await mongoose.connection.dropDatabase();
}

mongoose.connection.on('open', () => {
    console.log('open fire');
});

mongoose.connection.on('connected', () => {
    console.log('connected successfully');
});

test();

@IslandRhythms IslandRhythms added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed docs This issue is due to a mistake or omission in the mongoosejs.com documentation labels Oct 18, 2021
@IslandRhythms
Copy link
Collaborator

If there is a slight difference, I think we should mention that in the docs.

@mpodolsk
Copy link
Author

mpodolsk commented Oct 19, 2021

from the source code is can see there are some differences

@event connected: Emitted when this connection successfully connects to the db. May be emitted multiple times in reconnected scenarios.

@event open: Emitted after we connected and onOpen is executed on all of this connections models.

https://github.com/Automattic/mongoose/blob/master/lib/connection.js#L642
https://github.com/Automattic/mongoose/blob/master/lib/connection.js#L45
https://github.com/Automattic/mongoose/blob/master/lib/connection.js#L46

Also in the code I can see the reconnect event is triggered differently for replica sets.
is there some documentation explaining how and when we emit reconnect when using replicaSet

@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Oct 19, 2021
@vkarpov15 vkarpov15 added this to the 6.0.14 milestone Oct 19, 2021
@vkarpov15 vkarpov15 modified the milestones: 6.0.15, 6.0.17 Dec 3, 2021
@vkarpov15
Copy link
Collaborator

@mpodolsk there's a note here about connection events in replica sets:

"In a replica set, Mongoose will emit 'disconnected' if it loses connectivity to the replica set primary, and 'connected' if it manages to reconnect to a the replica set primary."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

3 participants