├── .editorconfig ├── .gitignore ├── README.md ├── angular.json ├── e2e ├── protractor.conf.js ├── src │ ├── app.e2e-spec.ts │ └── app.po.ts └── tsconfig.e2e.json ├── package.json ├── server └── index.js ├── src ├── app │ ├── app.component.css │ ├── app.component.html │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ └── websocket │ │ ├── index.ts │ │ ├── websocket.config.ts │ │ ├── websocket.events.ts │ │ ├── websocket.interfaces.ts │ │ ├── websocket.models.ts │ │ ├── websocket.module.spec.ts │ │ ├── websocket.module.ts │ │ ├── websocket.service.spec.ts │ │ └── websocket.service.ts ├── assets │ └── .gitkeep ├── browserslist ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── karma.conf.js ├── main.ts ├── polyfills.ts ├── styles.css ├── test.ts ├── tsconfig.app.json ├── tsconfig.spec.json └── tslint.json ├── tsconfig.json └── tslint.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 4 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | 8 | # dependencies 9 | /node_modules 10 | 11 | # IDEs and editors 12 | /.idea 13 | .project 14 | .classpath 15 | .c9/ 16 | *.launch 17 | .settings/ 18 | *.sublime-workspace 19 | 20 | # IDE - VSCode 21 | .vscode/* 22 | !.vscode/settings.json 23 | !.vscode/tasks.json 24 | !.vscode/launch.json 25 | !.vscode/extensions.json 26 | 27 | # misc 28 | /.sass-cache 29 | /connect.lock 30 | /coverage 31 | /libpeerconnection.log 32 | npm-debug.log 33 | yarn-error.log 34 | testem.log 35 | /typings 36 | 37 | # System Files 38 | .DS_Store 39 | Thumbs.db 40 | package-lock.json 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Websocket Example 2 | 3 | > Angular service for WebSocket with Rx WebSocketSubject 4 | 5 | ## Installation 6 | For angular 6: 7 | ```bash 8 | $ git clone https://github.com/Angular-RU/angular-websocket-starter.git 9 | $ cd angular-websocket-starter 10 | $ npm install 11 | $ npm run start 12 | $ npm run server 13 | ``` 14 | 15 | ## Example 16 | 17 | #### Add WebSockets to your project 18 | 19 | > in app module 20 | 21 | Config ReconnectingWebSocket: 22 | ```typescript 23 | Options = { 24 | WebSocket?: any; // WebSocket constructor, if none provided, defaults to global WebSocket 25 | maxReconnectionDelay?: number; // max delay in ms between reconnections 26 | minReconnectionDelay?: number; // min delay in ms between reconnections 27 | reconnectionDelayGrowFactor?: number; // how fast the reconnection delay grows 28 | minUptime?: number; // min time in ms do consider connection as stable 29 | connectionTimeout?: number; // retry connect if not connected after this time, in ms 30 | maxRetries?: number; // maximum number of retries 31 | debug?: boolean; // enables debug output 32 | }; 33 | ``` 34 | 35 | ```typescript 36 | import { BrowserModule } from '@angular/platform-browser'; 37 | import { NgModule } from '@angular/core'; 38 | 39 | import { AppComponent } from './app.component'; 40 | import { WebsocketModule } from './websocket'; 41 | 42 | 43 | @NgModule({ 44 | declarations: [ 45 | AppComponent 46 | ], 47 | imports: [ 48 | WebsocketModule.config({ 49 | url: 'http:localhost:8080', // websocket url 50 | ignore: [WS_API.EVENTS.MESSAGES], // ignore events 51 | garbageCollectInterval: 30000, // remove topics without subscribes. default 10000 52 | options: { // ReconnectingWebSocket 53 | connectionTimeout: 5000, 54 | maxRetries: 10 55 | } 56 | }) 57 | ], 58 | providers: [], 59 | bootstrap: [AppComponent] 60 | }) 61 | export class AppModule { 62 | } 63 | ``` 64 | 65 | 66 | > in components 67 | 68 | ```typescript 69 | import { Component, OnInit } from '@angular/core'; 70 | import { Observable } from 'rxjs'; 71 | 72 | import { IMessage, WebsocketService, WS_API } from './websocket/index'; 73 | 74 | 75 | 76 | @Component({ 77 | selector: 'app-root', 78 | templateUrl: './app.component.html', 79 | styleUrls: ['./app.component.css'] 80 | }) 81 | export class AppComponent implements OnInit { 82 | 83 | private messages$: Observable; 84 | 85 | constructor(private wsService: WebsocketService) { 86 | } 87 | 88 | ngOnInit() { 89 | // get messages 90 | this.messages$ = this.wsService.on(WS_API.EVENTS.MESSAGES); 91 | // or 92 | this.messages$ = this.wsService.on([WS_API.EVENTS.MESSAGES, WS_API.EVENTS.MESSAGES_NEW]); 93 | } 94 | 95 | public sendMessge(): void { 96 | this.wsService.send(WS_API.COMMANDS.SEND_TEXT, 'My Message Text'); 97 | } 98 | 99 | } 100 | ``` 101 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "angular-websocket-example": { 7 | "root": "", 8 | "sourceRoot": "src", 9 | "projectType": "application", 10 | "prefix": "app", 11 | "schematics": {}, 12 | "architect": { 13 | "build": { 14 | "builder": "@angular-devkit/build-angular:browser", 15 | "options": { 16 | "outputPath": "dist/angular-websocket-example", 17 | "index": "src/index.html", 18 | "main": "src/main.ts", 19 | "polyfills": "src/polyfills.ts", 20 | "tsConfig": "src/tsconfig.app.json", 21 | "assets": [ 22 | "src/favicon.ico", 23 | "src/assets" 24 | ], 25 | "styles": [ 26 | "src/styles.css" 27 | ], 28 | "scripts": [] 29 | }, 30 | "configurations": { 31 | "production": { 32 | "fileReplacements": [ 33 | { 34 | "replace": "src/environments/environment.ts", 35 | "with": "src/environments/environment.prod.ts" 36 | } 37 | ], 38 | "optimization": true, 39 | "outputHashing": "all", 40 | "sourceMap": false, 41 | "extractCss": true, 42 | "namedChunks": false, 43 | "aot": true, 44 | "extractLicenses": true, 45 | "vendorChunk": false, 46 | "buildOptimizer": true 47 | } 48 | } 49 | }, 50 | "serve": { 51 | "builder": "@angular-devkit/build-angular:dev-server", 52 | "options": { 53 | "browserTarget": "angular-websocket-example:build" 54 | }, 55 | "configurations": { 56 | "production": { 57 | "browserTarget": "angular-websocket-example:build:production" 58 | } 59 | } 60 | }, 61 | "extract-i18n": { 62 | "builder": "@angular-devkit/build-angular:extract-i18n", 63 | "options": { 64 | "browserTarget": "angular-websocket-example:build" 65 | } 66 | }, 67 | "test": { 68 | "builder": "@angular-devkit/build-angular:karma", 69 | "options": { 70 | "main": "src/test.ts", 71 | "polyfills": "src/polyfills.ts", 72 | "tsConfig": "src/tsconfig.spec.json", 73 | "karmaConfig": "src/karma.conf.js", 74 | "styles": [ 75 | "src/styles.css" 76 | ], 77 | "scripts": [], 78 | "assets": [ 79 | "src/favicon.ico", 80 | "src/assets" 81 | ] 82 | } 83 | }, 84 | "lint": { 85 | "builder": "@angular-devkit/build-angular:tslint", 86 | "options": { 87 | "tsConfig": [ 88 | "src/tsconfig.app.json", 89 | "src/tsconfig.spec.json" 90 | ], 91 | "exclude": [ 92 | "**/node_modules/**" 93 | ] 94 | } 95 | } 96 | } 97 | }, 98 | "angular-websocket-example-e2e": { 99 | "root": "e2e/", 100 | "projectType": "application", 101 | "architect": { 102 | "e2e": { 103 | "builder": "@angular-devkit/build-angular:protractor", 104 | "options": { 105 | "protractorConfig": "e2e/protractor.conf.js", 106 | "devServerTarget": "angular-websocket-example:serve" 107 | }, 108 | "configurations": { 109 | "production": { 110 | "devServerTarget": "angular-websocket-example:serve:production" 111 | } 112 | } 113 | }, 114 | "lint": { 115 | "builder": "@angular-devkit/build-angular:tslint", 116 | "options": { 117 | "tsConfig": "e2e/tsconfig.e2e.json", 118 | "exclude": [ 119 | "**/node_modules/**" 120 | ] 121 | } 122 | } 123 | } 124 | } 125 | }, 126 | "defaultProject": "angular-websocket-example" 127 | } -------------------------------------------------------------------------------- /e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const {SpecReporter} = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './src/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function () { 21 | } 22 | }, 23 | onPrepare() { 24 | require('ts-node').register({ 25 | project: require('path').join(__dirname, './tsconfig.e2e.json') 26 | }); 27 | jasmine.getEnv().addReporter(new SpecReporter({spec: {displayStacktrace: true}})); 28 | } 29 | }; -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import {AppPage} from './app.po'; 2 | 3 | describe('workspace-project App', () => { 4 | let page: AppPage; 5 | 6 | beforeEach(() => { 7 | page = new AppPage(); 8 | }); 9 | 10 | it('should display welcome message', () => { 11 | page.navigateTo(); 12 | expect(page.getParagraphText()).toEqual('Welcome to angular-websocket-example!'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import {browser, by, element} from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get('/'); 6 | } 7 | 8 | getParagraphText() { 9 | return element(by.css('app-root h1')).getText(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ] 12 | } 13 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-websocket-example", 3 | "version": "0.1.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e", 11 | "server": "node server" 12 | }, 13 | "private": true, 14 | "dependencies": { 15 | "@angular/animations": "^6.0.3", 16 | "@angular/common": "^6.0.3", 17 | "@angular/compiler": "^6.0.3", 18 | "@angular/core": "^6.0.3", 19 | "@angular/forms": "^6.0.3", 20 | "@angular/http": "^6.0.3", 21 | "@angular/platform-browser": "^6.0.3", 22 | "@angular/platform-browser-dynamic": "^6.0.3", 23 | "@angular/router": "^6.0.3", 24 | "core-js": "^2.5.4", 25 | "dexie": "^2.0.4", 26 | "js-sha256": "^0.9.0", 27 | "reconnecting-websocket": "^4.0.0-rc5", 28 | "rxjs": "^6.0.0", 29 | "ws": "^5.2.2", 30 | "zone.js": "^0.8.26" 31 | }, 32 | "devDependencies": { 33 | "@angular-devkit/build-angular": "~0.6.8", 34 | "@angular/cli": "~6.0.8", 35 | "@angular/compiler-cli": "^6.0.3", 36 | "@angular/language-service": "^6.0.3", 37 | "@types/dexie": "^1.3.1", 38 | "@types/jasmine": "~2.8.6", 39 | "@types/jasminewd2": "~2.0.3", 40 | "@types/node": "~8.9.4", 41 | "codelyzer": "~4.2.1", 42 | "jasmine-core": "~2.99.1", 43 | "jasmine-spec-reporter": "~4.2.1", 44 | "karma": "~1.7.1", 45 | "karma-chrome-launcher": "~2.2.0", 46 | "karma-coverage-istanbul-reporter": "~2.0.0", 47 | "karma-jasmine": "~1.1.1", 48 | "karma-jasmine-html-reporter": "^0.2.2", 49 | "protractor": "~5.3.0", 50 | "ts-node": "~5.0.1", 51 | "tslint": "~5.9.1", 52 | "typescript": "~2.7.2" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /server/index.js: -------------------------------------------------------------------------------- 1 | const WebSocketServer = require('ws').Server; 2 | const wss = new WebSocketServer({port: 6759}); 3 | 4 | const messages = [ 5 | { 6 | id: 1, 7 | text: 'Hello everyone!!!' 8 | }, 9 | { 10 | id: 2, 11 | text: 'I\'m here!!!' 12 | }, 13 | { 14 | id: 3, 15 | text: 'Who there???' 16 | }, 17 | { 18 | id: 4, 19 | text: 'Damn!' 20 | }, 21 | { 22 | id: 5, 23 | text: 'I\'m off' 24 | } 25 | ]; 26 | const texts = ['Text Data']; 27 | let counter = 0; 28 | 29 | 30 | wss.on('connection', (ws) => { 31 | ws.binaryType = 'arraybuffer'; 32 | 33 | console.log('WebSocket connection!'); 34 | 35 | ws.on('message', (event) => { 36 | const message = JSON.parse(event); 37 | 38 | switch (message.event) { 39 | case 'set-text': 40 | texts.unshift(message.data); 41 | 42 | break; 43 | case 'remove-text': 44 | texts.splice(message.data, 1); 45 | break; 46 | } 47 | 48 | ws.send(JSON.stringify({ 49 | event: 'update-texts', 50 | buffer: Buffer.from(JSON.stringify(texts)) 51 | })); 52 | 53 | console.log('message', message); 54 | }); 55 | 56 | ws.send(JSON.stringify({ 57 | event: 'messages', 58 | buffer: Buffer.from(JSON.stringify(messages)) 59 | })); 60 | 61 | ws.send(JSON.stringify({ 62 | event: 'update-texts', 63 | buffer: Buffer.from(JSON.stringify(texts)) 64 | })); 65 | 66 | const timer = () => { 67 | ws.send(JSON.stringify({ 68 | event: 'counter', 69 | buffer: Buffer.from((++counter).toString()) 70 | })); 71 | }; 72 | 73 | const interval = setInterval(timer, 1000); 74 | 75 | ws.on('close', () => { 76 | console.log('disconnected'); 77 | clearInterval(interval); 78 | }); 79 | 80 | }); 81 | -------------------------------------------------------------------------------- /src/app/app.component.css: -------------------------------------------------------------------------------- 1 | .container { 2 | max-width: 1200px; 3 | margin: 100px auto; 4 | } 5 | 6 | .row { 7 | display: grid; 8 | grid-template-columns: 1fr 1fr; 9 | grid-gap: 20px; 10 | margin-bottom: 2rem; 11 | align-items: center; 12 | } 13 | 14 | ul { 15 | margin: 0; 16 | padding: 0; 17 | list-style: none; 18 | } 19 | 20 | li { 21 | margin-bottom: .5rem; 22 | padding-bottom: .5rem; 23 | border-bottom: 1px #eee solid; 24 | } 25 | 26 | li:last-child { 27 | margin-bottom: 0; 28 | padding-bottom: 0; 29 | border-bottom: 0 none; 30 | overflow: hidden; 31 | } 32 | 33 | form, .texts { 34 | align-self: start; 35 | } 36 | 37 | textarea { 38 | width: 100%; 39 | min-height: 120px; 40 | padding: 10px; 41 | box-sizing: border-box; 42 | } 43 | 44 | button { 45 | padding: 10px 15px; 46 | float: right; 47 | cursor: pointer; 48 | } 49 | 50 | button:disabled { 51 | cursor: default; 52 | } 53 | 54 | .counter { 55 | text-align: center; 56 | font-size: 24px; 57 | vertical-align: middle; 58 | } 59 | 60 | .remove { 61 | display: block; 62 | height: 16px; 63 | width: 16px; 64 | cursor: pointer; 65 | float: right; 66 | line-height: 16px; 67 | text-align: center; 68 | color: red; 69 | font-size: 12px; 70 | } 71 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 |
2 |

Angular WebSocket Starter Demo

3 | 4 |
5 |
6 |
    7 |
  • 8 | №{{ message.id }}: {{ message.text }} 9 |
  • 10 |
11 |
12 | 13 |
14 | Update websocket counter: {{ counter }} 15 |
16 |
17 | 18 |
19 |
20 |
21 |
22 | 23 |
24 |
25 |
26 | 27 |
28 |
29 | 30 |
31 |
    32 |
  • 33 | {{ text }} 34 | x 35 |
  • 36 |
37 |
38 |
39 | 40 |
41 | -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, async } from '@angular/core/testing'; 2 | import { AppComponent } from './app.component'; 3 | 4 | describe('AppComponent', () => { 5 | beforeEach(async(() => { 6 | TestBed.configureTestingModule({ 7 | declarations: [ 8 | AppComponent 9 | ] 10 | }).compileComponents(); 11 | })); 12 | it('should create the app', async(() => { 13 | const fixture = TestBed.createComponent(AppComponent); 14 | const app = fixture.debugElement.componentInstance; 15 | expect(app).toBeTruthy(); 16 | })); 17 | }); 18 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit, OnDestroy } from '@angular/core'; 2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; 3 | import { Observable } from 'rxjs'; 4 | 5 | import { IMessage, WebsocketService, WS_API } from './websocket/index'; 6 | 7 | 8 | @Component({ 9 | selector: 'app-root', 10 | templateUrl: './app.component.html', 11 | styleUrls: ['./app.component.css'] 12 | }) 13 | export class AppComponent implements OnInit, OnDestroy { 14 | 15 | private messages$: Observable; 16 | private counter$: Observable; 17 | private texts$: Observable; 18 | 19 | public form: FormGroup; 20 | 21 | constructor(private fb: FormBuilder, private wsService: WebsocketService) { 22 | } 23 | 24 | ngOnInit() { 25 | this.form = this.fb.group({ 26 | text: [null, [ 27 | Validators.required 28 | ]] 29 | }); 30 | 31 | // get messages 32 | this.messages$ = this.wsService.addEventListener(WS_API.EVENTS.MESSAGES); 33 | 34 | // get counter 35 | this.counter$ = this.wsService.addEventListener(WS_API.EVENTS.COUNTER); 36 | 37 | // get texts 38 | this.texts$ = this.wsService.addEventListener(WS_API.EVENTS.UPDATE_TEXTS); 39 | } 40 | 41 | ngOnDestroy() { 42 | 43 | } 44 | 45 | public sendText(): void { 46 | if (this.form.valid) { 47 | this.wsService.sendMessage(WS_API.COMMANDS.SEND_TEXT, this.form.value.text); 48 | this.form.reset(); 49 | } 50 | } 51 | 52 | public removeText(index: number): void { 53 | this.wsService.sendMessage(WS_API.COMMANDS.REMOVE_TEXT, index); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { BrowserModule } from '@angular/platform-browser'; 2 | import { NgModule } from '@angular/core'; 3 | import { ReactiveFormsModule } from '@angular/forms'; 4 | 5 | import { AppComponent } from './app.component'; 6 | import { WebsocketModule } from './websocket/index'; 7 | import { environment } from '../environments/environment'; 8 | 9 | 10 | @NgModule({ 11 | declarations: [ 12 | AppComponent 13 | ], 14 | imports: [ 15 | BrowserModule, 16 | ReactiveFormsModule, 17 | WebsocketModule.config({ 18 | url: environment.ws 19 | }) 20 | ], 21 | providers: [], 22 | bootstrap: [AppComponent] 23 | }) 24 | export class AppModule { 25 | } 26 | -------------------------------------------------------------------------------- /src/app/websocket/index.ts: -------------------------------------------------------------------------------- 1 | export * from './websocket.module'; 2 | export * from './websocket.config'; 3 | export * from './websocket.interfaces'; 4 | export * from './websocket.models'; 5 | export * from './websocket.events'; 6 | export * from './websocket.service'; 7 | -------------------------------------------------------------------------------- /src/app/websocket/websocket.config.ts: -------------------------------------------------------------------------------- 1 | import { InjectionToken } from '@angular/core'; 2 | 3 | export const config: InjectionToken = new InjectionToken('websocket'); 4 | -------------------------------------------------------------------------------- /src/app/websocket/websocket.events.ts: -------------------------------------------------------------------------------- 1 | export const WS_API = { 2 | EVENTS: { 3 | MESSAGES: 'messages', 4 | COUNTER: 'counter', 5 | UPDATE_TEXTS: 'update-texts' 6 | }, 7 | COMMANDS: { 8 | SEND_TEXT: 'set-text', 9 | REMOVE_TEXT: 'remove-text' 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /src/app/websocket/websocket.interfaces.ts: -------------------------------------------------------------------------------- 1 | import { Options } from 'reconnecting-websocket'; 2 | import { Observable, Subject, ObjectUnsubscribedError } from 'rxjs'; 3 | 4 | export class MessageSubject extends Subject { 5 | 6 | constructor( 7 | private listeners: IListeners, 8 | private topic: string, 9 | private id: string 10 | ) { 11 | super(); 12 | } 13 | 14 | public next(value?: T): void { 15 | if (this.closed) { 16 | throw new ObjectUnsubscribedError(); 17 | } 18 | 19 | if (!this.isStopped) { 20 | const {observers} = this; 21 | const len = observers.length; 22 | const copy = observers.slice(); 23 | 24 | for (let i = 0; i < len; i++) { 25 | copy[i].next(value); 26 | } 27 | 28 | if (!len) { 29 | this.garbageCollect(); 30 | } 31 | } 32 | } 33 | 34 | /* 35 | * garbage collector 36 | * */ 37 | private garbageCollect(): void { 38 | delete this.listeners[this.topic][this.id]; 39 | 40 | if (!Object.keys(this.listeners[this.topic]).length) { // if not subjects 41 | delete this.listeners[this.topic]; 42 | } 43 | } 44 | 45 | } 46 | 47 | export interface IWebsocketService { 48 | addEventListener(topics: string[], id?: number): Observable; 49 | runtimeIgnore(topics: string[]): void; 50 | runtimeRemoveIgnore(topics: string[]): void; 51 | sendMessage(event: string, data: any): void; 52 | } 53 | 54 | export interface WebSocketConfig { 55 | url: string; 56 | ignore?: string[]; 57 | garbageCollectInterval?: number; 58 | options?: Options; 59 | } 60 | 61 | export interface ITopic { 62 | [hash: string]: MessageSubject; 63 | } 64 | 65 | export interface IListeners { 66 | [topic: string]: ITopic; 67 | } 68 | 69 | export interface IBuffer { 70 | type: string; 71 | data: number[]; 72 | } 73 | 74 | export interface IWsMessage { 75 | event: string; 76 | buffer: IBuffer; 77 | } 78 | 79 | export interface IMessage { 80 | id: number; 81 | text: string; 82 | } 83 | 84 | export type ITopicDataType = IMessage[] | number | string[]; 85 | -------------------------------------------------------------------------------- /src/app/websocket/websocket.models.ts: -------------------------------------------------------------------------------- 1 | import Dexie from 'dexie'; 2 | 3 | import { IMessage, IWsMessage } from './websocket.interfaces'; 4 | import { WS_API } from './websocket.events'; 5 | 6 | class MessagesDatabase extends Dexie { 7 | public messages!: Dexie.Table; // id is number in this case 8 | 9 | constructor() { 10 | super('MessagesDatabase'); 11 | 12 | this.version(1).stores({ 13 | messages: '++id,text' 14 | }); 15 | } 16 | } 17 | 18 | 19 | export const modelParser = (message: IWsMessage) => { 20 | if (message && message.buffer) { 21 | /* binary parse */ 22 | const encodeUint8Array = String.fromCharCode 23 | .apply(String, new Uint8Array(message.buffer.data)); 24 | 25 | const parseData = JSON.parse(encodeUint8Array); 26 | 27 | let MessagesDB: MessagesDatabase; 28 | 29 | if (message.event === WS_API.EVENTS.MESSAGES) { // if messages set IMessage[] 30 | if (!MessagesDB) { 31 | MessagesDB = new MessagesDatabase(); 32 | } 33 | 34 | parseData.forEach((messageData: IMessage) => { 35 | /* create transaction */ 36 | MessagesDB.transaction('rw', MessagesDB.messages, async () => { 37 | 38 | /* if not item */ 39 | if ((await MessagesDB.messages 40 | .where({id: messageData.id}).count()) === 0) { 41 | 42 | const id = await MessagesDB.messages 43 | .add({id: messageData.id, text: messageData.text}); 44 | 45 | console.log(`Addded message with id ${id}`); 46 | } 47 | 48 | }).catch(e => { 49 | console.error(e.stack || e); 50 | }); 51 | }); 52 | 53 | return MessagesDB.messages.toArray(); 54 | } 55 | 56 | if (message.event === WS_API.EVENTS.COUNTER) { // if counter set number 57 | return new Promise(r => r(parseData)); 58 | } 59 | 60 | if (message.event === WS_API.EVENTS.UPDATE_TEXTS) { // if text set string 61 | const texts = []; 62 | 63 | parseData.forEach((textData: string) => { 64 | texts.push(textData); 65 | }); 66 | 67 | return new Promise(r => r(texts)); 68 | } 69 | 70 | } else { 71 | console.log(`[${Date()}] Buffer is "undefined"`); 72 | } 73 | }; 74 | -------------------------------------------------------------------------------- /src/app/websocket/websocket.module.spec.ts: -------------------------------------------------------------------------------- 1 | import { WebsocketModule } from './websocket.module'; 2 | 3 | describe('WebsocketModule', () => { 4 | let websocketModule: WebsocketModule; 5 | 6 | beforeEach(() => { 7 | websocketModule = new WebsocketModule(); 8 | }); 9 | 10 | it('should create an instance', () => { 11 | expect(websocketModule).toBeTruthy(); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/app/websocket/websocket.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule, ModuleWithProviders } from '@angular/core'; 2 | import { CommonModule } from '@angular/common'; 3 | 4 | import { config } from './websocket.config'; 5 | import { WebSocketConfig } from './websocket.interfaces'; 6 | 7 | 8 | @NgModule({ 9 | imports: [ 10 | CommonModule 11 | ] 12 | }) 13 | export class WebsocketModule { 14 | public static config(wsConfig: WebSocketConfig): ModuleWithProviders { 15 | return { 16 | ngModule: WebsocketModule, 17 | providers: [{provide: config, useValue: wsConfig}] 18 | }; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/app/websocket/websocket.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, inject } from '@angular/core/testing'; 2 | 3 | import { WebsocketService } from './websocket.service'; 4 | 5 | describe('WebsocketService', () => { 6 | beforeEach(() => { 7 | TestBed.configureTestingModule({ 8 | providers: [WebsocketService] 9 | }); 10 | }); 11 | 12 | it('should be created', inject([WebsocketService], (service: WebsocketService) => { 13 | expect(service).toBeTruthy(); 14 | })); 15 | }); 16 | -------------------------------------------------------------------------------- /src/app/websocket/websocket.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, OnDestroy, Inject } from '@angular/core'; 2 | import { Observable } from 'rxjs'; 3 | import ReconnectingWebSocket from 'reconnecting-websocket'; 4 | import { sha256 } from 'js-sha256'; 5 | 6 | import { IListeners, ITopic, ITopicDataType, IWebsocketService, MessageSubject, WebSocketConfig } from './websocket.interfaces'; 7 | import { config } from './websocket.config'; 8 | import { WS_API } from './websocket.events'; 9 | import { modelParser } from './websocket.models'; 10 | 11 | 12 | @Injectable({ 13 | providedIn: 'root' 14 | }) 15 | export class WebsocketService implements IWebsocketService, OnDestroy { 16 | 17 | private listeners: IListeners; 18 | private uniqueId: number; 19 | private websocket: ReconnectingWebSocket; 20 | 21 | constructor(@Inject(config) private wsConfig: WebSocketConfig) { 22 | this.uniqueId = -1; 23 | this.listeners = {}; 24 | this.wsConfig.ignore = wsConfig.ignore ? wsConfig.ignore : []; 25 | 26 | // run connection 27 | this.connect(); 28 | } 29 | 30 | ngOnDestroy() { 31 | this.websocket.close(); 32 | } 33 | 34 | 35 | /* 36 | * connect to WebSocked 37 | * */ 38 | private connect(): void { 39 | // ReconnectingWebSocket config 40 | const options = { 41 | connectionTimeout: 1000, 42 | maxRetries: 10, 43 | ...this.wsConfig.options 44 | }; 45 | 46 | // connect to WebSocked 47 | this.websocket = new ReconnectingWebSocket(this.wsConfig.url, [], options); 48 | 49 | this.websocket.addEventListener('open', (event: Event) => { 50 | console.log(`[${Date()}] WebSocket connected!`); 51 | }); 52 | 53 | this.websocket.addEventListener('close', (event: CloseEvent) => { 54 | console.log(`[${Date()}] WebSocket close!`); 55 | }); 56 | 57 | this.websocket.addEventListener('error', (event: ErrorEvent) => { 58 | console.error(`[${Date()}] WebSocket error!`); 59 | }); 60 | 61 | this.websocket.addEventListener('message', (event: MessageEvent) => { 62 | // dispatch message to subscribers 63 | this.onMessage(event); 64 | }); 65 | 66 | setInterval(() => { 67 | this.garbageCollect(); // remove subjects without subscribe 68 | }, (this.wsConfig.garbageCollectInterval || 10000)); 69 | } 70 | 71 | 72 | /* 73 | * garbage collector 74 | * */ 75 | private garbageCollect(): void { 76 | for (const event in this.listeners) { 77 | if (this.listeners.hasOwnProperty(event)) { 78 | const topic = this.listeners[event]; 79 | 80 | for (const key in topic) { 81 | if (topic.hasOwnProperty(key)) { 82 | const subject = topic[key]; 83 | 84 | if (!subject.observers.length) { // if not subscribes 85 | delete topic[key]; 86 | } 87 | } 88 | } 89 | 90 | if (!Object.keys(topic).length) { // if not subjects 91 | delete this.listeners[event]; 92 | } 93 | } 94 | } 95 | } 96 | 97 | 98 | /* 99 | * call messages to Subject 100 | * */ 101 | private callMessage(topic: ITopic, data: T): void { 102 | for (const key in topic) { 103 | if (topic.hasOwnProperty(key)) { 104 | const subject = topic[key]; 105 | 106 | if (subject) { 107 | // dispatch message to subscriber 108 | subject.next(data); 109 | } else { 110 | console.log(`[${Date()}] Topic Subject is "undefined"`); 111 | } 112 | } 113 | } 114 | } 115 | 116 | 117 | /* 118 | * dispatch messages to subscribers 119 | * */ 120 | private onMessage(event: MessageEvent): void { 121 | const message = JSON.parse(event.data); 122 | 123 | for (const name in this.listeners) { 124 | if (this.listeners.hasOwnProperty(name) && !this.wsConfig.ignore.includes(name)) { 125 | 126 | const topic = this.listeners[name]; 127 | const keys = name.split('/'); // if multiple events 128 | const isMessage = keys.includes(message.event); 129 | const model = modelParser(message); // get model 130 | 131 | if (isMessage && typeof model !== 'undefined') { 132 | model.then((data: ITopicDataType) => { 133 | this.callMessage(topic, data); 134 | }); 135 | } 136 | } 137 | } 138 | } 139 | 140 | 141 | /* 142 | * add topic for subscribers 143 | * */ 144 | private addTopic(topic: string, id?: number): MessageSubject { 145 | const token = (++this.uniqueId).toString(); // token for personal subject 146 | const key = id ? token + id : token; // id for more personal subject 147 | const hash = sha256.hex(key); // set hash for personal 148 | 149 | if (!this.listeners[topic]) { 150 | this.listeners[topic] = {}; 151 | } 152 | 153 | return this.listeners[topic][hash] = new MessageSubject(this.listeners, topic, hash); 154 | } 155 | 156 | 157 | /* 158 | * subscribe method 159 | * */ 160 | public addEventListener(topics: string | string[], id?: number): Observable { 161 | if (topics) { 162 | const topicsKey = typeof topics === 'string' ? topics : topics.join('/'); // one or multiple 163 | 164 | return this.addTopic(topicsKey, id).asObservable(); 165 | } else { 166 | console.log(`[${Date()}] Can't add EventListener. Type of event is "undefined".`); 167 | } 168 | } 169 | 170 | 171 | /* 172 | * on message to server 173 | * */ 174 | public sendMessage(event: string, data: any = {}): void { 175 | if (event && this.websocket.readyState === 1) { 176 | this.websocket.send(JSON.stringify({event, data})); 177 | } else { 178 | console.log('Send error!'); 179 | } 180 | } 181 | 182 | 183 | /* 184 | * runtime add ignore list 185 | * */ 186 | public runtimeIgnore(topics: string[]): void { 187 | if (topics && topics.length) { 188 | this.wsConfig.ignore.push(...topics); 189 | } 190 | } 191 | 192 | 193 | /* 194 | * runtime remove from ignore list 195 | * */ 196 | public runtimeRemoveIgnore(topics: string[]): void { 197 | if (topics && topics.length) { 198 | topics.forEach((topic: string) => { 199 | const topicIndex = this.wsConfig.ignore.findIndex(t => t === topic); // find topic in ignore list 200 | 201 | if (topicIndex > -1) { 202 | this.wsConfig.ignore.splice(topicIndex, 1); 203 | } 204 | }); 205 | } 206 | } 207 | 208 | } 209 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexDaSoul/angular-websocket-example/4609992b0fc88b24c6d2ea65880a3dbc24019216/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/browserslist: -------------------------------------------------------------------------------- 1 | # This file is currently used by autoprefixer to adjust CSS to support the below specified browsers 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | # For IE 9-11 support, please uncomment the last line of the file and adjust as needed 5 | > 0.5% 6 | last 2 versions 7 | Firefox ESR 8 | not dead 9 | # IE 9-11 -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true, 3 | ws: 'ws://localhost:6759' 4 | }; 5 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build ---prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false, 7 | ws: 'ws://localhost:6759' 8 | }; 9 | 10 | /* 11 | * In development mode, to ignore zone related error stack frames such as 12 | * `zone.run`, `zoneDelegate.invokeTask` for easier debugging, you can 13 | * import the following file, but please comment it out in production mode 14 | * because it will have performance impact when throw error 15 | */ 16 | // import 'zone.js/dist/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexDaSoul/angular-websocket-example/4609992b0fc88b24c6d2ea65880a3dbc24019216/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | AngularWebsocketExample 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, '../coverage'), 20 | reports: ['html', 'lcovonly'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false 30 | }); 31 | }; -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.log(err)); 13 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE9, IE10 and IE11 requires all of the following polyfills. **/ 22 | // import 'core-js/es6/symbol'; 23 | // import 'core-js/es6/object'; 24 | // import 'core-js/es6/function'; 25 | // import 'core-js/es6/parse-int'; 26 | // import 'core-js/es6/parse-float'; 27 | // import 'core-js/es6/number'; 28 | // import 'core-js/es6/math'; 29 | // import 'core-js/es6/string'; 30 | // import 'core-js/es6/date'; 31 | // import 'core-js/es6/array'; 32 | // import 'core-js/es6/regexp'; 33 | // import 'core-js/es6/map'; 34 | // import 'core-js/es6/weak-map'; 35 | // import 'core-js/es6/set'; 36 | 37 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 38 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 39 | 40 | /** IE10 and IE11 requires the following for the Reflect API. */ 41 | // import 'core-js/es6/reflect'; 42 | 43 | 44 | /** Evergreen browsers require these. **/ 45 | // Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove. 46 | import 'core-js/es7/reflect'; 47 | 48 | 49 | /** 50 | * Web Animations `@angular/platform-browser/animations` 51 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 52 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 53 | **/ 54 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 55 | 56 | /** 57 | * By default, zone.js will patch all possible macroTask and DomEvents 58 | * user can disable parts of macroTask/DomEvents patch by setting following flags 59 | */ 60 | 61 | // (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 62 | // (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 63 | // (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 64 | 65 | /* 66 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 67 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 68 | */ 69 | // (window as any).__Zone_enable_cross_context_check = true; 70 | 71 | /*************************************************************************************************** 72 | * Zone JS is required by default for Angular itself. 73 | */ 74 | import 'zone.js/dist/zone'; // Included with Angular CLI. 75 | 76 | 77 | /*************************************************************************************************** 78 | * APPLICATION IMPORTS 79 | */ 80 | -------------------------------------------------------------------------------- /src/styles.css: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/dist/zone-testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: any; 11 | 12 | // First, initialize the Angular testing environment. 13 | getTestBed().initTestEnvironment( 14 | BrowserDynamicTestingModule, 15 | platformBrowserDynamicTesting() 16 | ); 17 | // Then we find all the tests. 18 | const context = require.context('./', true, /\.spec\.ts$/); 19 | // And load the modules. 20 | context.keys().map(context); 21 | -------------------------------------------------------------------------------- /src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "es2015", 6 | "types": [] 7 | }, 8 | "exclude": [ 9 | "src/test.ts", 10 | "**/*.spec.ts" 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /src/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/spec", 5 | "module": "commonjs", 6 | "types": [ 7 | "jasmine", 8 | "node" 9 | ] 10 | }, 11 | "files": [ 12 | "test.ts", 13 | "polyfills.ts" 14 | ], 15 | "include": [ 16 | "**/*.spec.ts", 17 | "**/*.d.ts" 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /src/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tslint.json", 3 | "rules": { 4 | "directive-selector": [ 5 | true, 6 | "attribute", 7 | "app", 8 | "camelCase" 9 | ], 10 | "component-selector": [ 11 | true, 12 | "element", 13 | "app", 14 | "kebab-case" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "moduleResolution": "node", 9 | "emitDecoratorMetadata": true, 10 | "experimentalDecorators": true, 11 | "target": "es5", 12 | "typeRoots": [ 13 | "node_modules/@types" 14 | ], 15 | "lib": [ 16 | "es2017", 17 | "dom" 18 | ] 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesDirectory": [ 3 | "node_modules/codelyzer" 4 | ], 5 | "rules": { 6 | "arrow-return-shorthand": true, 7 | "callable-types": true, 8 | "class-name": true, 9 | "comment-format": [ 10 | true, 11 | "check-space" 12 | ], 13 | "curly": true, 14 | "deprecation": { 15 | "severity": "warn" 16 | }, 17 | "eofline": true, 18 | "forin": true, 19 | "import-blacklist": [ 20 | true, 21 | "rxjs/Rx" 22 | ], 23 | "import-spacing": true, 24 | "indent": [ 25 | true, 26 | "spaces" 27 | ], 28 | "interface-over-type-literal": true, 29 | "label-position": true, 30 | "max-line-length": [ 31 | true, 32 | 140 33 | ], 34 | "member-access": false, 35 | "member-ordering": [ 36 | true, 37 | { 38 | "order": [ 39 | "static-field", 40 | "instance-field", 41 | "static-method", 42 | "instance-method" 43 | ] 44 | } 45 | ], 46 | "no-arg": true, 47 | "no-bitwise": true, 48 | "no-console": [ 49 | true, 50 | "debug", 51 | "info", 52 | "time", 53 | "timeEnd", 54 | "trace" 55 | ], 56 | "no-construct": true, 57 | "no-debugger": true, 58 | "no-duplicate-super": true, 59 | "no-empty": false, 60 | "no-empty-interface": true, 61 | "no-eval": true, 62 | "no-inferrable-types": [ 63 | true, 64 | "ignore-params" 65 | ], 66 | "no-misused-new": true, 67 | "no-non-null-assertion": true, 68 | "no-shadowed-variable": true, 69 | "no-string-literal": false, 70 | "no-string-throw": true, 71 | "no-switch-case-fall-through": true, 72 | "no-trailing-whitespace": true, 73 | "no-unnecessary-initializer": true, 74 | "no-unused-expression": true, 75 | "no-use-before-declare": true, 76 | "no-var-keyword": true, 77 | "object-literal-sort-keys": false, 78 | "one-line": [ 79 | true, 80 | "check-open-brace", 81 | "check-catch", 82 | "check-else", 83 | "check-whitespace" 84 | ], 85 | "prefer-const": true, 86 | "quotemark": [ 87 | true, 88 | "single" 89 | ], 90 | "radix": true, 91 | "semicolon": [ 92 | true, 93 | "always" 94 | ], 95 | "triple-equals": [ 96 | true, 97 | "allow-null-check" 98 | ], 99 | "typedef-whitespace": [ 100 | true, 101 | { 102 | "call-signature": "nospace", 103 | "index-signature": "nospace", 104 | "parameter": "nospace", 105 | "property-declaration": "nospace", 106 | "variable-declaration": "nospace" 107 | } 108 | ], 109 | "unified-signatures": true, 110 | "variable-name": false, 111 | "whitespace": [ 112 | true, 113 | "check-branch", 114 | "check-decl", 115 | "check-operator", 116 | "check-separator", 117 | "check-type" 118 | ], 119 | "no-output-on-prefix": true, 120 | "use-input-property-decorator": true, 121 | "use-output-property-decorator": true, 122 | "use-host-property-decorator": true, 123 | "no-input-rename": true, 124 | "no-output-rename": true, 125 | "use-life-cycle-interface": true, 126 | "use-pipe-transform-interface": true, 127 | "component-class-suffix": true, 128 | "directive-class-suffix": true 129 | } 130 | } 131 | --------------------------------------------------------------------------------