Skip to content

Commit ee4b327

Browse files
authored
DOCSP-39164: Flutter: Change baseURL during runtime (#3239)
## Pull Request Info Jira ticket: https://jira.mongodb.org/browse/DOCSP-39164 - [Connect to App Services - Flutter SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39164-flutter-baseurl/sdk/flutter/app-services/connect-to-app/#connect-to-a-specific-server): New "Connect to a Specific Server" section with baseURL info. New "Connect to a Different Server During Runtime" subsection with info about the experimental API. - ### Reminder Checklist Before merging your PR, make sure to check a few things. - [x] Did you tag pages appropriately? - genre - meta.keywords - meta.description - [x] Describe your PR's changes in the Release Notes section - [x] Create a Jira ticket for related docs-app-services work, if any ### Release Notes - **Flutter SDK** - Atlas App Services/Connect to App Services: New "Connect to a Specific Server" section with baseURL info. New "Connect to a Different Server During Runtime" subsection with info about the experimental API. ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)
1 parent 207bd47 commit ee4b327

File tree

6 files changed

+143
-47
lines changed

6 files changed

+143
-47
lines changed

examples/dart/pubspec.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,18 @@ packages:
213213
dependency: transitive
214214
description:
215215
name: ejson
216-
sha256: "2d69382ea016c5f38c9d966681b464ca23d5f594918d4cb7dfecf0a3dad31395"
216+
sha256: f336c6fb6c5c97db8ae59ba8ed207f542241f1db39cf2ef03776d308de3432ff
217217
url: "https://pub.dev"
218218
source: hosted
219-
version: "0.2.0-pre.1"
219+
version: "0.3.0"
220220
ejson_annotation:
221221
dependency: transitive
222222
description:
223223
name: ejson_annotation
224-
sha256: f14948884cf6d91e00c727d5ec6b2395bdcb511541aa7956e4e03b73cd089149
224+
sha256: b265eea722ee340d77d1c36a55a1f963d517a0dabb569b0775664c319a4e3ebf
225225
url: "https://pub.dev"
226226
source: hosted
227-
version: "0.2.0-pre.1"
227+
version: "0.3.0"
228228
faker:
229229
dependency: "direct main"
230230
description:
@@ -437,26 +437,26 @@ packages:
437437
dependency: transitive
438438
description:
439439
name: realm_common
440-
sha256: c4c994217c3f1dafdb3e36b5e3c75c6f4719fd5bd64f64a5acb79a5c962c018f
440+
sha256: c04b64fcbcd0afd78c06939efdedb484417697242039d5bb0b07fa150c9c021d
441441
url: "https://pub.dev"
442442
source: hosted
443-
version: "2.0.0"
443+
version: "2.2.0"
444444
realm_dart:
445445
dependency: "direct main"
446446
description:
447447
name: realm_dart
448-
sha256: "861b007dccf19ecfb1e7f47f2b0c246755b82c40e122d61fde18180dbbaf88a3"
448+
sha256: b523d392ec614ccb358e64451aca4772d56db23f93e8f58dd1d1780dadc92706
449449
url: "https://pub.dev"
450450
source: hosted
451-
version: "2.0.0"
451+
version: "2.2.0"
452452
realm_generator:
453453
dependency: transitive
454454
description:
455455
name: realm_generator
456-
sha256: "3cad739d4491bc5b4ccdd184083fd58f6899e410d0acda7448b60261639e2735"
456+
sha256: a5c3c403add9d886c098509b49517f44f1d8b667ac0178f80f58fea5ce069fb8
457457
url: "https://pub.dev"
458458
source: hosted
459-
version: "2.0.0"
459+
version: "2.2.0"
460460
sane_uuid:
461461
dependency: transitive
462462
description:
@@ -674,4 +674,4 @@ packages:
674674
source: hosted
675675
version: "3.1.2"
676676
sdks:
677-
dart: ">=3.2.0 <4.0.0"
677+
dart: ">=3.3.0 <4.0.0"

examples/dart/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ description: A simple command-line application using Realm Dart SDK.
44
publish_to: none
55

66
environment:
7-
sdk: "^3.1.0"
7+
sdk: ^3.3.0
88

99
dependencies:
10-
realm_dart: ^2.0.0
10+
realm_dart: ^2.2.0
1111
path: ^1.8.2
1212
dart_jsonwebtoken: ^2.4.2
1313
faker: ^2.0.0

examples/dart/test/app_services_test.dart

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import 'package:realm_dart/realm.dart';
33
import "dart:io";
44
import "dart:convert";
55
import "dart:isolate";
6+
import 'utils.dart';
67

78
void main() {
89
const APP_ID = "example-testers-kvjdy";
10+
const EDGE_SERVER_APP_ID = "sync-edge-server-cskhoow";
11+
const baseUrl = 'http://localhost';
12+
const newBaseUrl = 'https://services.cloud.mongodb.com';
913

1014
group('App Services client - ', () {
1115
test('Access App client', () {
@@ -29,24 +33,44 @@ void main() {
2933
expect(appConfig.defaultRequestTimeout, Duration(seconds: 120));
3034
});
3135

32-
test('BaseUrl not cached on App config', () {
33-
final newUrl = Uri.parse('https://dart.dev');
34-
35-
// Instantiate App with default BaseUrl
36-
final appConfig = AppConfiguration(APP_ID,
37-
defaultRequestTimeout: const Duration(seconds: 120)
38-
);
36+
test('Custom BaseUrl', () {
37+
// :snippet-start: custom-base-url
38+
// Specify a baseUrl to connect to a server other than the default
39+
final appConfig =
40+
AppConfiguration(APP_ID, baseUrl: Uri.parse('https://example.com'));
41+
3942
var app = App(appConfig);
40-
expect(app.baseUrl.toString(), 'https://realm.mongodb.com');
41-
42-
// Update with new BaseUrl
43-
final newConfig = AppConfiguration(APP_ID,
44-
defaultRequestTimeout: const Duration(seconds: 120),
45-
baseUrl:newUrl);
46-
app = App(newConfig);
47-
expect(app.baseUrl.toString(), 'https://dart.dev');
43+
// :snippet-end:
44+
expect(app.baseUrl.toString(), 'https://example.com');
4845
});
4946

47+
test('Change BaseUrl', () async {
48+
// :snippet-start: change-base-url
49+
// Specify a custom baseUrl to connect to.
50+
// In this case, an Edge Server instance running on the device.
51+
final appConfig = AppConfiguration(
52+
EDGE_SERVER_APP_ID,
53+
baseUrl: Uri.parse('http://localhost:80')
54+
);
55+
56+
var app = App(appConfig);
57+
58+
// ... log in a user and use the app ...
59+
// :remove-start:
60+
expect(app.baseUrl.toString(), baseUrl);
61+
await app.logIn(Credentials.anonymous());
62+
expect(app.currentUser != null, true);
63+
// :remove-end:
64+
65+
// Later, change the baseUrl to the default:
66+
// https://services.cloud.mongodb.com
67+
await app.updateBaseUrl(null);
68+
// :snippet-end:
69+
expect(app.baseUrl.toString(), newBaseUrl);
70+
},
71+
skip:
72+
"""Skipping until we get Edge Server running in a CI and we can write automated tests for full flow (this was tested locally and succeeded)""");
73+
5074
test('Access App on background isolate by id', () async {
5175
// :snippet-start: access-app-by-id
5276
// Create an App instance once on main isolate,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Specify a custom baseUrl to connect to.
2+
// In this case, an Edge Server instance running on the device.
3+
final appConfig = AppConfiguration(
4+
EDGE_SERVER_APP_ID,
5+
baseUrl: Uri.parse('http://localhost:80')
6+
);
7+
8+
var app = App(appConfig);
9+
10+
// ... log in a user and use the app ...
11+
12+
// Later, change the baseUrl to the default:
13+
// https://services.cloud.mongodb.com
14+
await app.updateBaseUrl(null);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Specify a baseUrl to connect to a server other than the default
2+
final appConfig =
3+
AppConfiguration(APP_ID, baseUrl: Uri.parse('https://example.com'));
4+
5+
var app = App(appConfig);

source/sdk/flutter/app-services/connect-to-app.txt

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,44 +36,44 @@ Access the App Client
3636
.. versionchanged:: 1.7.0
3737
``App`` must be created on the main isolate.
3838

39-
Create an ``App`` instance to access App Services features throughout your
40-
client application. We recommend that you create the ``App`` instance only
39+
Create an ``App`` instance to access App Services features throughout your
40+
client application. We recommend that you create the ``App`` instance only
4141
once on the main isolate, ideally as soon as the app starts.
4242

43-
#. Get your App Services App's ID from the App Services UI. To learn how,
43+
#. Get your App Services App's ID from the App Services UI. To learn how,
4444
refer to :ref:`Find your App ID <find-your-app-id>`.
4545
#. Create an :flutter-sdk:`AppConfiguration <realm/AppConfiguration-class.html>`
4646
object with your App's App ID as the argument.
4747
#. Create an :flutter-sdk:`App <realm/App-class.html>`
48-
with the ``AppConfiguration`` you just created. In Flutter v1.7.0 and later,
49-
this must be done on the main isolate, otherwise the SDK throws an error.
48+
with the ``AppConfiguration`` you just created. In Flutter v1.7.0 and later,
49+
this must be done on the main isolate, otherwise the SDK throws an error.
5050

51-
After you create the ``App``, you can access the constructed ``App`` instance
51+
After you create the ``App``, you can access the constructed ``App`` instance
5252
on a background isolate using ``App.getById``. Refer to the
53-
:ref:`Get App by ID <flutter-get-app-by-id>` section on this page for more
53+
:ref:`Get App by ID <flutter-get-app-by-id>` section on this page for more
5454
information.
5555

5656
.. literalinclude:: /examples/generated/flutter/app_services_test.snippet.access-app-client.dart
5757
:language: dart
5858

5959
You can create multiple App client instances to connect to multiple
60-
Apps. All App client instances that share the same App ID use the same
60+
Apps. All App client instances that share the same App ID use the same
6161
underlying connection.
6262

6363
.. important:: Changing an App Config After Initializing the App
6464

6565
.. versionchanged:: 1.8.0
6666
``baseUrl`` is not cached in the ``AppConfiguration``
67-
68-
When you initialize the App client, the configuration is cached internally.
67+
68+
When you initialize the App client, the configuration is cached internally.
6969
Attempting to close and then re-open an App with a changed configuration
70-
within the same process has no effect. The client continues to use the
71-
cached configuration.
70+
within the same process has no effect. The client continues to use the
71+
cached configuration.
7272

73-
In Flutter SDK version 1.8.0 and later, the :flutter-sdk:`baseUrl <realm/AppConfiguration/baseUrl.html>`
74-
is *no longer* cached in the App configuration. This means that you can change the
75-
``baseUrl``, and the App client will use the updated configuration. In
76-
earlier SDK versions, changes to the ``baseUrl`` in a cached App
73+
In Flutter SDK version 1.8.0 and later, the :flutter-sdk:`baseUrl <realm/AppConfiguration/baseUrl.html>`
74+
is *no longer* cached in the App configuration. This means that you can change the
75+
``baseUrl``, and the App client will use the updated configuration. In
76+
earlier SDK versions, changes to the ``baseUrl`` in a cached App
7777
configuration have no effect.
7878

7979
.. _flutter-app-client-configuration:
@@ -91,7 +91,7 @@ To learn about the available configuration options, refer to the
9191
:flutter-sdk:`AppConfiguration <realm/AppConfiguration-class.html>` reference documentation.
9292

9393
.. literalinclude:: /examples/generated/flutter/app_services_test.snippet.app-client-advanced-configuration.dart
94-
:language: dart
94+
:language: dart
9595

9696
.. note:: Connect Using Android 7 or Older
9797

@@ -107,11 +107,64 @@ Get App by ID
107107

108108
.. versionadded:: 1.7.0
109109

110-
After you have created an ``App`` instance on the main isolate, you can access
111-
the constructed instance on a background isolate by passing the App ID to the
110+
After you have created an ``App`` instance on the main isolate, you can access
111+
the constructed instance on a background isolate by passing the App ID to the
112112
:flutter-sdk:`App.getById() <realm/App/getById.html>` method. Then, you can use it to work with the ``App`` and users as needed.
113113

114114
.. literalinclude:: /examples/generated/flutter/app_services_test.snippet.access-app-by-id.dart
115115
:language: dart
116116
:emphasize-lines: 14
117117

118+
.. _flutter-connect-to-specific-server:
119+
120+
Connect to a Specific Server
121+
----------------------------
122+
123+
By default, Atlas Device SDK connects to Atlas using the global ``baseUrl``
124+
of ``https://services.cloud.mongodb.com``. In some cases, you may want to
125+
connect to a different server:
126+
127+
- Your App Services App uses local deployment, and you want to connect directly to a local ``baseUrl`` in your region. For more information, refer to the :ref:`<local-deployment>` App Services documentation.
128+
- You want to connect to an Edge Server instance. For more information, refer to
129+
the :ref:`edge-server-connect-from-client` App Services documentation.
130+
131+
You can specify a ``baseUrl`` in the
132+
:flutter-sdk:`AppConfiguration <realm/AppConfiguration-class.html>`:
133+
134+
.. literalinclude:: /examples/generated/flutter/app_services_test.snippet.custom-base-url.dart
135+
:language: dart
136+
137+
Connect to a Different Server During Runtime
138+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139+
140+
.. versionadded:: 1.8.0
141+
.. versionchanged:: 2.2.0
142+
``updateBaseUrl`` accepts ``null`` value
143+
144+
In some cases, you might want to change the ``baseUrl`` while the app is
145+
running. For example, you might want to roam between Edge Servers or
146+
move from an App Services connection to an Edge Server connection.
147+
148+
To change the ``baseUrl`` during runtime, call the experimental
149+
:flutter-sdk:`app.updateBaseUrl <realm/App/updateBaseUrl.html>` method. You can
150+
pass ``null`` to reset the ``baseUrl`` to the default value.
151+
152+
.. literalinclude:: /examples/generated/flutter/app_services_test.snippet.change-base-url.dart
153+
:language: dart
154+
155+
This API is experimental and may change in future releases.
156+
157+
If you want to change the ``baseUrl`` after you have logged in a user and
158+
have opened a synced database, the app must perform a client reset. For more
159+
information, refer to :ref:`flutter-client-reset`.
160+
161+
Perform the following in your code:
162+
163+
1. :ref:`Pause the Sync session <flutter-pause-resume-sync>`
164+
2. Update the ``baseUrl`` using the ``app.updateBaseUrl`` method
165+
3. Re-authenticate the user to log in using the new ``baseUrl``
166+
4. Open a synced database pulling data from the new server
167+
168+
Both the server and the client *must* be online for the user to authenticate and
169+
connect to the new server. If the server is not online or the client does not
170+
have a network connection, the user cannot authenticate and open the database.

0 commit comments

Comments
 (0)