├── .gitignore
├── .prettierrc
├── README.md
├── nest-cli.json
├── nodemon-debug.json
├── nodemon.json
├── package.json
├── src
├── alert
│ ├── alert.controller.ts
│ └── alert.gateway.ts
├── app.module.ts
├── chat
│ └── chat.gateway.ts
└── main.ts
├── static
├── index.html
└── styles.css
├── test
├── app.e2e-spec.ts
└── jest-e2e.json
├── tsconfig.build.json
├── tsconfig.json
├── tslint.json
└── yarn.lock
/.gitignore:
--------------------------------------------------------------------------------
1 | # NestJS Ignores
2 | /dist
3 | /tmp
4 | /app/**/*.js
5 | /app/**/*.js.map
6 |
7 | # dependencies
8 | /node_modules
9 | /bower_components
10 |
11 | # IDEs and editors
12 | /.idea
13 | /.vscode
14 | **/.history/**
15 |
16 | # misc
17 | /.sass-cache
18 | /connect.lock
19 | /coverage/*
20 | /libpeerconnection.log
21 | npm-debug.log
22 | testem.log
23 | /typings
24 |
25 | # e2e
26 | /e2e/*.js
27 | /e2e/*.map
28 |
29 | #System Files
30 | .DS_Store
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "trailingComma": "all"
4 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
6 | [travis-url]: https://travis-ci.org/nestjs/nest
7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
8 | [linux-url]: https://travis-ci.org/nestjs/nest
9 |
10 | A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
26 |
27 | ## Description
28 |
29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
30 |
31 | ## Installation
32 |
33 | ```bash
34 | $ npm install
35 | ```
36 |
37 | ## Running the app
38 |
39 | ```bash
40 | # development
41 | $ npm run start
42 |
43 | # watch mode
44 | $ npm run start:dev
45 |
46 | # production mode
47 | $ npm run start:prod
48 | ```
49 |
50 | ## Test
51 |
52 | ```bash
53 | # unit tests
54 | $ npm run test
55 |
56 | # e2e tests
57 | $ npm run test:e2e
58 |
59 | # test coverage
60 | $ npm run test:cov
61 | ```
62 |
63 | ## Support
64 |
65 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
66 |
67 | ## Stay in touch
68 |
69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
70 | - Website - [https://nestjs.com](https://nestjs.com/)
71 | - Twitter - [@nestframework](https://twitter.com/nestframework)
72 |
73 | ## License
74 |
75 | Nest is [MIT licensed](LICENSE).
76 |
--------------------------------------------------------------------------------
/nest-cli.json:
--------------------------------------------------------------------------------
1 | {
2 | "language": "ts",
3 | "collection": "@nestjs/schematics",
4 | "sourceRoot": "src"
5 | }
6 |
--------------------------------------------------------------------------------
/nodemon-debug.json:
--------------------------------------------------------------------------------
1 | {
2 | "watch": ["src"],
3 | "ext": "ts",
4 | "ignore": ["src/**/*.spec.ts"],
5 | "exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts"
6 | }
7 |
--------------------------------------------------------------------------------
/nodemon.json:
--------------------------------------------------------------------------------
1 | {
2 | "watch": ["src"],
3 | "ext": "ts",
4 | "ignore": ["src/**/*.spec.ts"],
5 | "exec": "ts-node -r tsconfig-paths/register src/main.ts"
6 | }
7 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "nest.ws.tutorial",
3 | "version": "0.0.1",
4 | "description": "",
5 | "author": "",
6 | "license": "MIT",
7 | "scripts": {
8 | "build": "tsc -p tsconfig.build.json",
9 | "format": "prettier --write \"src/**/*.ts\"",
10 | "start": "ts-node -r tsconfig-paths/register src/main.ts",
11 | "start:dev": "nodemon",
12 | "start:debug": "nodemon --config nodemon-debug.json",
13 | "prestart:prod": "rimraf dist && npm run build",
14 | "start:prod": "node dist/main.js",
15 | "lint": "tslint -p tsconfig.json -c tslint.json",
16 | "test": "jest",
17 | "test:watch": "jest --watch",
18 | "test:cov": "jest --coverage",
19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
20 | "test:e2e": "jest --config ./test/jest-e2e.json"
21 | },
22 | "dependencies": {
23 | "@nestjs/common": "^6.0.0",
24 | "@nestjs/core": "^6.0.0",
25 | "@nestjs/platform-express": "^6.0.0",
26 | "@nestjs/platform-socket.io": "^6.1.0",
27 | "@nestjs/websockets": "^6.1.0",
28 | "reflect-metadata": "^0.1.12",
29 | "rimraf": "^2.6.2",
30 | "rxjs": "^6.3.3"
31 | },
32 | "devDependencies": {
33 | "@nestjs/testing": "^6.0.0",
34 | "@types/express": "^4.16.0",
35 | "@types/jest": "^23.3.13",
36 | "@types/node": "^10.12.18",
37 | "@types/socket.io": "^2.1.2",
38 | "@types/supertest": "^2.0.7",
39 | "jest": "^23.6.0",
40 | "nodemon": "^1.18.9",
41 | "prettier": "^1.15.3",
42 | "supertest": "^3.4.1",
43 | "ts-jest": "^23.10.5",
44 | "ts-node": "^7.0.1",
45 | "tsconfig-paths": "^3.7.0",
46 | "tslint": "5.12.1",
47 | "typescript": "^3.2.4"
48 | },
49 | "jest": {
50 | "moduleFileExtensions": [
51 | "js",
52 | "json",
53 | "ts"
54 | ],
55 | "rootDir": "src",
56 | "testRegex": ".spec.ts$",
57 | "transform": {
58 | "^.+\\.(t|j)s$": "ts-jest"
59 | },
60 | "coverageDirectory": "../coverage",
61 | "testEnvironment": "node"
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/alert/alert.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Post, HttpCode, Body } from '@nestjs/common';
2 | import { AlertGateway } from './alert.gateway';
3 |
4 | @Controller('alert')
5 | export class AlertController {
6 |
7 | constructor(private alertGateway: AlertGateway) {}
8 |
9 | @Post()
10 | @HttpCode(200)
11 | sendAlertToAll(@Body() dto: { message: string }) {
12 | this.alertGateway.sendToAll(dto.message);
13 | return dto;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/alert/alert.gateway.ts:
--------------------------------------------------------------------------------
1 | import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
2 | import { Server } from 'socket.io';
3 |
4 | @WebSocketGateway({ namespace: '/alert' })
5 | export class AlertGateway {
6 |
7 | @WebSocketServer() wss: Server;
8 |
9 | sendToAll(msg: string) {
10 | this.wss.emit('alertToClient', { type: 'Alert', message: msg });
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { ChatGateway } from './chat/chat.gateway';
3 | import { AlertGateway } from './alert/alert.gateway';
4 | import { AlertController } from './alert/alert.controller';
5 |
6 | @Module({
7 | imports: [],
8 | controllers: [AlertController],
9 | providers: [ChatGateway, AlertGateway],
10 | })
11 | export class AppModule {}
12 |
--------------------------------------------------------------------------------
/src/chat/chat.gateway.ts:
--------------------------------------------------------------------------------
1 | import { SubscribeMessage, WebSocketGateway, OnGatewayInit, WebSocketServer, OnGatewayConnection } from '@nestjs/websockets';
2 | import { Socket, Server } from 'socket.io';
3 | import { Logger } from '@nestjs/common';
4 |
5 | @WebSocketGateway({ namespace: '/chat' })
6 | export class ChatGateway implements OnGatewayInit {
7 |
8 | @WebSocketServer() wss: Server;
9 |
10 | private logger: Logger = new Logger('ChatGateway');
11 |
12 | afterInit(server: any) {
13 | this.logger.log('Initialized!');
14 | }
15 |
16 | @SubscribeMessage('chatToServer')
17 | handleMessage(client: Socket, message: { sender: string, room: string, message: string }) {
18 | this.wss.to(message.room).emit('chatToClient', message);
19 | }
20 |
21 | @SubscribeMessage('joinRoom')
22 | handleRoomJoin(client: Socket, room: string ) {
23 | client.join(room);
24 | client.emit('joinedRoom', room);
25 | }
26 |
27 | @SubscribeMessage('leaveRoom')
28 | handleRoomLeave(client: Socket, room: string ) {
29 | client.leave(room);
30 | client.emit('leftRoom', room);
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { AppModule } from './app.module';
3 | import { NestExpressApplication } from '@nestjs/platform-express';
4 | import { join } from 'path';
5 |
6 | async function bootstrap() {
7 | const app = await NestFactory.create(AppModule);
8 | app.useStaticAssets(join(__dirname, '..', 'static'));
9 | await app.listen(3000); // Start on port 3000
10 | }
11 | bootstrap();
12 |
--------------------------------------------------------------------------------
/static/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Websockets Client
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
19 |
20 | Websockets Tester
21 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Status: {{ isMemberOfActiveRoom ? 'Joined' : 'NOT Joined' }}
33 |
34 |
35 |
36 | -
37 | {{ msg.sender }}: {{ msg.message }}
38 |
39 |
40 |
41 |
42 |
43 |
44 |
118 |
119 |
--------------------------------------------------------------------------------
/static/styles.css:
--------------------------------------------------------------------------------
1 | main {
2 | display: grid;
3 | grid-template-columns: 1fr 1fr;
4 | grid-template-areas: "chat alerts";
5 | }
6 |
7 | section.chat {
8 | grid-area: chat;
9 | }
10 |
11 | section.alerts {
12 | grid-area: alerts;
13 | }
14 |
15 | .tab-row {
16 | padding-top: 20px;
17 | }
18 |
19 | .tab-btn.active {
20 | background: blue;
21 | color: white;
22 | }
--------------------------------------------------------------------------------
/test/app.e2e-spec.ts:
--------------------------------------------------------------------------------
1 | import { Test, TestingModule } from '@nestjs/testing';
2 | import * as request from 'supertest';
3 | import { AppModule } from './../src/app.module';
4 |
5 | describe('AppController (e2e)', () => {
6 | let app;
7 |
8 | beforeEach(async () => {
9 | const moduleFixture: TestingModule = await Test.createTestingModule({
10 | imports: [AppModule],
11 | }).compile();
12 |
13 | app = moduleFixture.createNestApplication();
14 | await app.init();
15 | });
16 |
17 | it('/ (GET)', () => {
18 | return request(app.getHttpServer())
19 | .get('/')
20 | .expect(200)
21 | .expect('Hello World!');
22 | });
23 | });
24 |
--------------------------------------------------------------------------------
/test/jest-e2e.json:
--------------------------------------------------------------------------------
1 | {
2 | "moduleFileExtensions": ["js", "json", "ts"],
3 | "rootDir": ".",
4 | "testEnvironment": "node",
5 | "testRegex": ".e2e-spec.ts$",
6 | "transform": {
7 | "^.+\\.(t|j)s$": "ts-jest"
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/tsconfig.build.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "./tsconfig.json",
3 | "exclude": ["node_modules", "test", "**/*spec.ts"]
4 | }
5 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": true,
5 | "removeComments": true,
6 | "emitDecoratorMetadata": true,
7 | "experimentalDecorators": true,
8 | "target": "es6",
9 | "sourceMap": true,
10 | "outDir": "./dist",
11 | "baseUrl": "./"
12 | },
13 | "exclude": ["node_modules"]
14 | }
15 |
--------------------------------------------------------------------------------
/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": ["tslint:recommended"],
4 | "jsRules": {
5 | "no-unused-expression": true
6 | },
7 | "rules": {
8 | "quotemark": [true, "single"],
9 | "member-access": [false],
10 | "ordered-imports": [false],
11 | "max-line-length": [true, 150],
12 | "member-ordering": [false],
13 | "interface-name": [false],
14 | "arrow-parens": false,
15 | "object-literal-sort-keys": false
16 | },
17 | "rulesDirectory": []
18 | }
19 |
--------------------------------------------------------------------------------