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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
config.yaml
config.yaml
.env
node_modules
2 changes: 2 additions & 0 deletions with-subscriptions/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
STEPZEN_ACCOUNT=
STEPZEN_API_KEY=
108 changes: 108 additions & 0 deletions with-subscriptions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# StepZen Example: `with-subscriptions`

## Introduction

This project shows a basic example of subscriptions in StepZen. Any query in StepZen can be converted into a subscription.

## Getting Started

You'll need to create a [StepZen account](https://stepzen.com/request-invite) first. Once you've got that set up, [git clone](https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-clone) this repository onto your machine and open the working directory:

```bash
git clone https://github.com/stepzen-dev/examples.git
cd examples/with-subscriptions
```

## Run the example

First, install StepZen CLI and log in using your StepZen account name and the Admin Key from [dashboard.stepzen.com/account](dashboard.stepzen.com/account).

```bash
npm i -g stepzen
stepzen login
```

Then, create a `.env` file to pass your StepZen credentials to the client app:

```bash
echo "STEPZEN_ACCOUNT=$(stepzen whoami --account)" >> .env.local
echo "STEPZEN_API_KEY=$(stepzen whoami --apikey)" >> .env.local
```

(or copy the `.env.sample` file to `.env` and add your credentials manually)

Finally, install dependencies and start the GraphQL server and client:

```bash
npm i
npm run start:all
```

This command deploys the GraphQL schema in the `stepzen` directory to the StepZen Cloud and the websockets endpoint is `wss://ACCOUNT.stepzen.net/stepzen-subscriptions/api/with-subscriptions/__graphql`, which is used by the client app.

> You can also start the GraphQL server and client separately by running `npm run stepzen` and `npm run start` in separate terminals.

## Alternative: using Docker

First, install StepZen CLI:

```bash
npm i -g stepzen
```

Start a local Docker container running the StepZen services:

```bash
stepzen service start && stepzen login --config ~/.stepzen/stepzen-config.local.yaml`
```

Deploy the GraphQL schema in the `stepzen` directory:

```bash
stepzen deploy
```

This deploys the GraphQL schema to StepZen (on your local machine) and the endpoint is `http://localhost:9000/api/with-subscriptions/__graphql`. However, the URL for web sockets is `ws://localhost:9000/stepzen-subscriptions/api/with-subscriptions/__graphql`.
Then, create a `.env` file to pass your StepZen credentials to the client app:

```bash
echo "STEPZEN_ACCOUNT=$(stepzen whoami --account)" >> .env.local
echo "STEPZEN_API_KEY=$(stepzen whoami --apikey)" >> .env.local
```

(or copy the `.env.sample` file to `.env` and add your credentials manually)

Finally, install dependencies and start the client:

```bash
npm i
npm start
```

You will see the subscription running in your terminal.

## Run a query

The subscription is against a mock REST backend and the Binance REST API (`https://api.binance.us/api/v3/ticker?symbol=ETHBTC`). StepZen does a long polling and pushes back to the client whenever a new value is found.

In the file `client/index.js` the subscription query is defined as:

```js
let query = 'subscription { random { number } }';
```

You can change this value to any query you want to run. For example, you can change it to:

```js
let query =
'subscription { binance(symbol: "ETHUSD", windowSize: "10m") { priceChange } }';
```

This will subscribe to the `binance` query and will return the `priceChange` field whenever a new value is found.

## Learn More

Because any `Query` field can be exposed as a `Subscription`, you can set up subscription against databases, GraphQL endpoints, or any combination thereof. Try them for yourself.

You can learn more in the [StepZen documentation](https://stepzen.com/docs). Questions? Head over to [Discord](https://discord.gg/9k2VdPn2FR) or [GitHub Discussions](https://git$
67 changes: 67 additions & 0 deletions with-subscriptions/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { createClient } = require('graphql-ws');
const { WebSocket } = require('ws');
require('dotenv').config();

const APIKEY = process.env.STEPZEN_API_KEY;
const ACCOUNT = process.env.STEPZEN_ACCOUNT;
const QUERY = process.env.GRAPHQL_QUERY;

let url =
'ws://localhost:9000/stepzen-subscriptions/api/with-subscriptions/__graphql';
// let query = 'subscription { binance(symbol: "ETHUSD", windowSize: "10m") { priceChange } }'
let query = 'subscription { random { number } }';

if (QUERY) {
query = QUERY;
}
console.log(`running: ${query}`);

if (!APIKEY) {
console.log(
process.env.STEPZEN_API_KEY,
'You must add your APIKEY to the .env file',
);
process.exit(1);
}

if (ACCOUNT && ACCOUNT != 'graphql') {
console.log(`wss "${ACCOUNT}" on stepzen service version`);
url = `wss://${ACCOUNT}.stepzen.net/stepzen-subscriptions/api/with-subscriptions/__graphql`;
} else {
console.log(`ws local version`);
}

const client = createClient({
webSocketImpl: WebSocket,
url: url,
connectionParams: () => {
return {
headers: {
Authorization: `apikey ${APIKEY}`,
},
};
},
});

// query
(async () => {
const result = await new Promise((resolve, reject) => {
let result;
client.subscribe(
{
query: query,
},
{
next: (data) => console.log(new Date(), data?.data),
error: (e) => {
console.log('ERROR', e);
reject();
},
complete: () => {
console.log('Complete');
resolve(result);
},
},
);
});
})();
Loading