- Notifications
You must be signed in to change notification settings - Fork 18
move subscription example #70
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits Select commit Hold shift + click to select a range
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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| .DS_Store | ||
| config.yaml | ||
| config.yaml | ||
| .env | ||
| node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| STEPZEN_ACCOUNT= | ||
| STEPZEN_API_KEY= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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$ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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); | ||
| }, | ||
| }, | ||
| ); | ||
| }); | ||
| })(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.