tinode / chat

Instant messaging platform. Backend in Go. Clients: Swift iOS, Java Android, JS webapp, scriptable command line; chatbots
GNU General Public License v3.0
12.03k stars 1.88k forks source link

Allow URI for mongoDB connection #755

Closed saitensou closed 2 years ago

saitensou commented 2 years ago

Is your feature request related to a problem? Please describe. I was trying to use Tinode with mongoDB atlas, which only allows connections using connection string URL. From the current config I don't see a clear/easy way to incorporate with it

Describe the solution you'd like Allow the URI option in tinode.conf

Describe alternatives you've considered Not much, the connection given at the atlas gives the connection string as

mongodb+srv://__DB_NAME.mongodb.net/PATH_AND_PARAMS__ There is not much to do in current configs allowed

or-else commented 2 years ago

Can you explain the need for this:

serverAPIOptions := mdbopts.ServerAPI(mdbopts.ServerAPIVersion1)
opts.SetServerAPIOptions(serverAPIOptions)

Why is it needed? Why is it needed only when the connection string URI is used?

saitensou commented 2 years ago

I am not very sure on the purpose of this too, as it is specified in nowhere in the documents except the connection guide steps. From the official document it represent options used to configure the API version sent to the server, which I think is only needed when using atlas. Plus some trial and error on the connection, this is only needed when using Atlas (connection string URI).

Not sure if binding it with the whole uri option is the best, but I think it is a sufficient one as "usual mongodb connection" can be tackled using the original, detailed configs instead.

tairov commented 2 years ago

Regarding Mongodb Atlas, there could be compatibility issues related to the DB version. Need to carefully test out all the original queries from chat-server. Here is DNS Seed List Connection Format documentation : https://www.mongodb.com/docs/manual/reference/connection-string/#dns-seed-list-connection-format

or-else commented 2 years ago

Would you do the testing?

saitensou commented 2 years ago

@tairov Are you stating that there could be compatibility issue with the DNS Seed List Connection Format?

My project does not use Seed List Connection Format but the URL provided from Mongo Guide is somewhat alike

mongodb+srv://CREDENTIALS@PROJECT.gmuaq.mongodb.net/DATABASE?retryWrites=true&w=majority

Thus I think it also supports using DNS Seed List Connection Format. If there are compatibility Issue, I am not sure if it is related to ServerAPIVersion, or other settings. But as long as the work around can be set using ApplyURI api it would be fine with the fix I suppose. @or-else what do you think?

Note: Currently official docs supports only ServerAPIVersion 1

tairov commented 2 years ago

@saitensou mongodb+srv: indicates DNS Seed List format used. As I can see you've merged code which uses uri, if it works for you it must be compatible. Regarding ServerAPIVersion1, I haven't found any references. Here is the connection code I've got from Atlas for go-lang:

import "go.mongodb.org/mongo-driver/mongo"

clientOptions := options.Client().
    ApplyURI("mongodb+srv://<username>:<password>@<CLUSTER-NAME>.mongodb.net/?retryWrites=true&w=majority")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
    log.Fatal(err)
}
or-else commented 2 years ago

Please send a pull request if you need any changes in the driver.

or-else commented 2 years ago

The 'Allow URI for mongoDB connection' request is released in 0.19.0. I'm going to close this issue. Please feel free to open another issue if you feel more work is needed.