Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thangtran3112/chatter-backend
Modern NestJs BE with both REST and GraphQL, Apollo Server with PubSub, Passport Auth, MongoDB, Redis. Deployment with Cloudfront, AWS CodePipeline and EC2
https://github.com/thangtran3112/chatter-backend
apollo-server apollographql aws-cloudfront mongodb-atlas mongoose nest-passport nestjs redis-pubsub
Last synced: about 1 month ago
JSON representation
Modern NestJs BE with both REST and GraphQL, Apollo Server with PubSub, Passport Auth, MongoDB, Redis. Deployment with Cloudfront, AWS CodePipeline and EC2
- Host: GitHub
- URL: https://github.com/thangtran3112/chatter-backend
- Owner: thangtran3112
- Created: 2024-03-23T17:55:34.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-07-26T12:58:41.000Z (5 months ago)
- Last Synced: 2024-07-26T14:29:26.833Z (5 months ago)
- Topics: apollo-server, apollographql, aws-cloudfront, mongodb-atlas, mongoose, nest-passport, nestjs, redis-pubsub
- Language: TypeScript
- Homepage:
- Size: 306 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Chatter backend with Apollo PubSub and Redis PubSub
- For Production solution, i used Redis, please see the [list event-publishing system](https://www.apollographql.com/docs/apollo-server/data/subscriptions/#production-pubsub-libraries)
- In this solution, we are using [Redis PubSub](https://github.com/davidyaha/graphql-redis-subscriptions) for AWS Production server
- On Local server, we are still using the normal Apollo PubSub comes with NESTJS. See `pubsub.module.ts`
- By default, Javascript objects are serialized using the JSON.stringify and JSON.parse methods. This means that not all objects - such as Date or Regexp objects - will deserialize correctly without a custom reviver, that work out of the box with the default in-memory implementation. In our case, ObjectId, Date in `MessageDocument` will need a custom reviver.
- Notes: The data we receive from Redis PubSub will need to be deserialized to JSON format in `message.service.ts`, before the resolvers can handle it. For this we are using [custom-reviver](https://github.com/davidyaha/graphql-redis-subscriptions?tab=readme-ov-file#using-a-custom-reviver)## Install nest config and Mongo DB connection
NodeJS v20X
`npm i @nestjs/config @nestjs/mongoose mongoose`
`npm i joi`### Installing local Mongo DB
`brew tap mongodb/brew`
`brew update`
`brew install [email protected]`### Using Mongo DB
- kill MongoDB process:
`mkdir -p ~/data/db`
`chmod 777 ~/data/db`
`mongod --dbpath ~/data/db`- Normally MongoDb will be started on 127.0.0.1:27017
- Connection string should be `mongodb://127.0.0.1:27017/chatter`- How to use [MongoDB Model](https://mongoosejs.com/docs/api/model.html)
- How to use [MongoDb method](https://www.mongodb.com/docs/manual/reference/method/)
- [MongoDB aggregate commands](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort/)## NestJs GraphQL
- Installing: `npm i @nestjs/graphql @nestjs/apollo @apollo/server graphql`
- Playground GraphQL server, after starting backend: `http://localhost:3001/graphql`
- Understand NestJS GraphQL:
### Running MongoDB Compass to view the MongoDB
### Using `migrate-mongo` package to manage DB migration
`npm i migrate-mongo`
`npm i --save-dev @types/migrate-mongo`## Install auth module with @nest/passport
`nest g module auth`
`nest g service auth`## Decoding the retrieved Authentication cookie from jwt.io
## Install common practices Field Validation and annotate email and password
`npm i --save class-validator class-transformer`
[ValidationPipe](https://docs.nestjs.com/techniques/validation)## AWS Backend deployment with self-signed SSL certificate
- [Using self-signed SSL certificate (not recommended in production environment)](https://www.udemy.com/course/build-a-real-time-chat-app-with-react-nestjs-graphql/learn/lecture/41850348#overview)
```
openssl version
openssl genrsa 2048 > privatekey.pem
stat privatekey.pem
````openssl req -new -key privatekey.pem -out csr.pem`
```
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:Washington
Locality Name (eg, city) []:Seattle
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Example Corporation
Organizational Unit Name (eg, section) []:Marketing
Common Name (e.g. server FQDN or YOUR name) []:prod.eba-cnqkbmvr.us-west-2.elasticbeanstalk.com
Email Address []:[email protected]Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:sometestpassword
An optional company name []:
````openssl x509 -req -days 365 -in csr.pem -signkey privatekey.pem -out public.crt`
- Upload public.crt to AWS Beanstalk with [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
`brew install awscli`
`aws --version`
`aws configure````
AWS Access Key ID [None]: ************************
AWS Secret Access Key [None]: *************************************
Default region name [None]: us-west-2
Default output format [None]:
````aws iam upload-server-certificate --server-certificate-name elastic-beanstalk-x509 --certificate-body file://public.crt --private-key file://privatekey.pem`
## Enable CORS on NESTJS backend
- Enable CORS in `main.ts` through `app.enableCors();`
- Enable CORS for GraphQLModule inside `app.module.ts` through `cors: true`## Backend Setting Cookie issue
- By default, AWS does not allow setting cookie with different CORS origin