https://github.com/gforge/subscription_example
https://github.com/gforge/subscription_example
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/gforge/subscription_example
- Owner: gforge
- Created: 2018-11-21T22:15:21.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-09-18T22:15:43.000Z (over 4 years ago)
- Last Synced: 2025-04-18T05:53:42.680Z (10 months ago)
- Language: TypeScript
- Size: 358 KB
- Stars: 7
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# An ApolloServer subscription example with passport
A simple example to run a ApolloServer with subscriptions and authorization. The logic in this example contains:
- login functionality using `graphql-passport`
- message publication using subscriptions
- filtering subscriptions
- checking subscriptions for valid credentials
## Direct run
For running in any NodeJS environment you have two options:
- `npm run start` - runs `ts-node` and starts the application
- `npm run dev` - runs `ts-node-dev` and allows you to do changes in the application with automated respawns
## Docker
### Using `docker-compose`
The simplest aproach is to just run:
```bash
docker-compose up
```
### Using plain `docker`
Package also contains a Dockerfile that can be used to isolate the app. The
code is inspired by: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/
Build using (use sudo if you haven't added your user to the Docker group - not
that adding may be a security issue):
```bash
docker build -t /subscription-app .
```
To run just write (the `--rm` makes it easier to clean up the container):
```bash
docker run --name subapp --rm -p 49160:4000 -d /subscription-app
```
### Extra stuff
Additional useful commands:
```bash
# Get container ID
$ docker ps
# Print app output
$ docker logs
# Enter the container
$ docker exec -it /bin/bash
```
## Testing
Then you can connect to it on [localhost:4000/graphql](http://localhost:4000/graphql) and run two simultaneous graphql queries in multiple tabs. **Note** Once you enter a subscription the context will never change, thus we need to first login:
```gql
mutation {
login(email: "john@doe.com", password: "pwd123") {
name
}
}
```
Then in a new tab, start a subscription:
```gql
subscription {
newMessage
}
```
The third tab should do a mutation for publishing to the subscription-tab:
```gql
mutation {
addMessage(message: "Testing to send a subscription message")
}
```
After running the mutation you should see "Testing to send a subscription message" in the subscription tab:
```json
{
"data": {
"newMessage": "Testing to send a subscription message"
}
}
```
### Tips
To test the user login functionality with multiple users you can start an _incognito_/_private_ window or an alternative browser, load up the playground and see how messages are sent between users.