Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 106 additions & 55 deletions packages/browser/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

# MongoDB Stitch Browser SDK

The official [MongoDB Stitch](https://stitch.mongodb.com/) Browser SDK for JavaScript/TypeScript.
The official [MongoDB Stitch](https://www.mongodb.com/cloud/stitch) Browser SDK for JavaScript/TypeScript.

### Index
- [Documentation](#documentation)
- [Discussion](#discussion)
- [Installation](#installation)
- [Example Usage](#example-usage)
- [Getting Started](#getting-started)

## Documentation
* [API/Typedoc Documentation](https://docs.mongodb.com/stitch-sdks/js/4/index.html)
* [MongoDB Stitch Documentation](https://docs.mongodb.com/stitch/)
* [API Reference Manual](https://docs.mongodb.com/stitch-sdks/js/4/index.html)

## Discussion
* [MongoDB Stitch Users - Google Group](https://groups.google.com/d/forum/mongodb-stitch-users)
Expand All @@ -28,17 +28,9 @@ Run the following in the root directory of your NPM project.
npm install mongodb-stitch-browser-sdk
```

This will start you off with the core SDK functionality as well as the remote MongoDB service.
This will start you off with the [core SDK functionality](classes/stitch.html) as well as the [remote MongoDB service](modules/remotemongoclient.html).

For customized dependencies use the following:

```bash
npm install mongodb-stitch-browser-core
npm install mongodb-stitch-browser-services-aws
npm install mongodb-stitch-browser-services-http
npm install mongodb-stitch-browser-services-mongodb-remote
npm install mongodb-stitch-browser-services-twilio
```
See [Customized Dependencies (Advanced)](#customized-dependencies) below for customizing dependencies.

### HTML Script Tags

Expand All @@ -48,40 +40,57 @@ You can also include the SDK directly in your HTML code using script tags. For c
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch.js"></script>
```

For customized dependencies use the following:
```html
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch-core.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch-services-aws.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch-services-http.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch-services-mongodb-remote.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch-services-twilio.js"></script>
```
See [Customized Dependencies (Advanced)](#customized-dependencies) below for customizing dependencies.

## Example Usage
## Getting Started

### Creating a new app with the SDK (NPM)

#### Set up an application on Stitch

First, you need to create the server-side Stitch app, and (for the purpose of this quick start) enable anonymous authentication:

1. Go to [https://stitch.mongodb.com/](https://stitch.mongodb.com/) and log in to MongoDB Atlas.
2. Create a new app in your project with your desired name.
3. Go to your app in Stitch via Atlas by clicking Stitch Apps in the left side pane and clicking your app.
3. Copy your app's client app id by going to Clients on the left side pane and clicking copy on the App ID section.
4. Go to Providers from Users in the left side pane and edit and enable "Allow users to log in anonymously".

For detailed instructions, see [Create a Stitch App](https://docs.mongodb.com/stitch/procedures/create-stitch-app/).

#### Set up an NPM project

Next, you create the source for your client app.

1. Ensure that you have `npm` installed. See [npmjs.com](https://www.npmjs.com).
2. Initialize a new NPM project with `npm init`.
3. Add the MongoDB Stitch Browser SDK by running `npm install mongodb-stitch-browser-sdk`.
4. Install `webpack.js` by running `npm install --save-dev webpack webpack-cli`.
5. Add the following field to the `"scripts"` field of your `package.json`:
2. Initialize a new NPM project:

```bash
mkdir StitchProject && cd StitchProject
npm init
```

3. Add the MongoDB Stitch Browser SDK:

```bash
npm install mongodb-stitch-browser-sdk
```

4. Install [webpack.js](https://webpack.js.org):

```bash
npm install --save-dev webpack webpack-cli
```

5. Add the following field to the `"scripts"` field of the `package.json` file that was generated by `npm init`:

```json
"scripts": {
"pack": "webpack"
}
```

6. Create directories for your source files, and your distributed files:
6. Create directories for your source files and your distributed files:

```bash
mkdir src dist
Expand Down Expand Up @@ -118,8 +127,17 @@ window.onload = initializeAndLogin;
</html>
```

9. Run the webpack bundler by running `npm run pack`.
10. Open `dist/index.html` in your web browser. If everything was configured correctly, you should see a message in the browser window that you are logged in as an anonymous user.
#### Build and run the app

Finally, you can build and run the app:

1. Run the webpack bundler:

```bash
npm run pack
```

2. Open `dist/index.html` in your web browser. If everything was configured correctly, you should see a message in the browser window that you are logged in as an anonymous user.

See the [Getting Started](https://webpack.js.org/guides/getting-started/) guide on `webpack`'s website for more information on how to use webpack to bundle your JavaScript or TypeScript code that uses the Stitch SDK.

Expand All @@ -128,22 +146,20 @@ Additionally, the JavaScript code above utilizes ES6 features. If you'd like you
### Using the SDK

#### Initialize the SDK
1. When your app or webpage is initialized, run the following code to initialize the Stitch SDK, replacing `<your-client-app-id>` with your Stitch application's client app ID:

When your app or webpage is initialized, use [Stitch.initializeDefaultAppClient](classes/stitch.html#initializedefaultappclient) to initialize the Stitch SDK. Replace `<your-client-app-id>` with your Stitch application's client app ID:

```javascript
import { Stitch, AnonymousCredential } from 'mongodb-stitch-browser-sdk'

Stitch.initializeDefaultAppClient('<your-client-app-id>');
```

2. To get a client to use for logging in and communicating with Stitch, use `Stitch.defaultAppClient`

```javascript
const stitchClient = Stitch.defaultAppClient;
```
Your client app ID can be found in the [Stitch UI](https://stitch.mongodb.com).

#### Logging In
1. We enabled anonymous log in, so let's log in with it; add the following anywhere in your code:

1. We enabled [anonymous authentication](https://docs.mongodb.com/stitch/authentication/anonymous/) in the steps above, so let's log in with it! Add the following anywhere in your code:

```javascript
const client = Stitch.defaultAppClient;
Expand All @@ -154,18 +170,44 @@ client.auth.loginWithCredential(new AnonymousCredential()).then(user => {
});
```

2. When running this code, you should see the following in your browser's debug console:
When running this code, you should see the following in your browser's debug console:

```
logging in anonymously
logged in anonymously as user 58c5d6ebb9ede022a3d75050
```

#### Using BSON and Extended JSON
See [StitchAuth](interfaces/stitchauth.html) for more information.


#### Executing a Stitch Function

One of Stitch's powerful features is serverless [Functions](https://docs.mongodb.com/stitch/functions/). Once logged in, the Stitch client can execute remote Stitch Functions using the [StitchAppClient.callFunction](interfaces/stitchappclient.html#callfunction) method:

```javascript
client.callFunction("echoArg", ["Hello world!"]).then(echoedResult => {
console.log(`Echoed result: ${echoedResult}`);
})
```

This library depends on [js-bson](https://www.npmjs.com/package/js-bson).
Assuming you've configured your Stitch application to have a function named "echoArg" that returns its argument, you should see a message like:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth showing that function here, too?


```
Echoed result: Hello world!
```

The `echoArg` Function in Stitch would look something like:

```javascript
// echoArg Function in the Stitch UI
exports = function(arg) {
return {arg: arg};
};
```

#### Using BSON and Extended JSON

As a convenience, the SDK includes the `BSON` library, and you can import it as you would import other classes and values from the SDK.
As a convenience, the SDK includes the [bson](https://www.npmjs.com/package/bson) library. You can import it as you would import other classes and values from the SDK.

Here is an example of importing BSON to generate a BSON `ObjectID` using ES6:

Expand All @@ -189,33 +231,42 @@ And here is an example of importing BSON to generate an `ObjectId` using an HTML
<script>
function generateObjectId() {
const newObjectId = new stitch.BSON.ObjectId()
document.getElementById('obj-id-display').innerHTML =
`Generated ObjectId: ${newObjectId}`;
}
document.getElementById('obj-id-display').innerHTML =
`Generated ObjectId: ${newObjectId}`;
}

window.onload = generateObjectId;
window.onload = generateObjectId;
</script>
<div id="obj-id-display">Generated ObjectId: None</div>
</body>
</html>
```

#### Executing a function
1. Once logged in, executing a function happens via the `StitchAppClient`'s `callFunction()` method
## Advanced Topics

```javascript
client.callFunction("echoArg", ["Hello world!"]).then(echoedResult => {
console.log(`Echoed result: ${echoedResult}`);
})
```
#### Customized Dependencies

2. If you've configured your Stitch application to have a function named "echoArg" that returns its argument, you should see a message like:
For customized dependencies in NPM use the following:

```bash
npm install mongodb-stitch-browser-core
npm install mongodb-stitch-browser-services-aws
npm install mongodb-stitch-browser-services-http
npm install mongodb-stitch-browser-services-mongodb-remote
npm install mongodb-stitch-browser-services-twilio
```
Echoed result: Hello world!

For customized dependencies with HTML script tags use the following:

```html
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch-core.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch-services-aws.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch-services-http.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch-services-mongodb-remote.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.1.3/stitch-services-twilio.js"></script>
```
#### Getting a StitchAppClient without Stitch.getDefaultAppClient

#### Getting a StitchAppClient without Stitch.getDefaultAppClient (Advanced)

In the case that you don't want a single default initialized `StitchAppClient`, you can use the following with as many client app IDs as you'd like to initialize clients for multiple app IDs:

Expand Down
Loading