Skip to content

Commit

Permalink
feat(websocket-sample): create websocket-sample
Browse files Browse the repository at this point in the history
* feat(websocket): initialize websocket sample

* feat(websocket): implement a simple websocket server with tests

A very simple websocket server (single enpoint). Client and server code written and ready to play
with

fix #14
  • Loading branch information
jmcdo29 authored Nov 26, 2019
1 parent 5e59bed commit b7fc7cd
Show file tree
Hide file tree
Showing 14 changed files with 1,021 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/cron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
node-version: [10.x, 12.x]
app-name: [simple-sample, complex-sample, mongo-sample, typeorm-sample, typeorm-graphql-sample,]
app-name: [simple-sample, complex-sample, mongo-sample, typeorm-sample, typeorm-graphql-sample, websocket-sample]

steps:
- uses: actions/checkout@v1
Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/websocket.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: WebSocket Sample

on:
pull_request:
branches:
- 'master'
push:
branches:
- 'websocket-sample'

jobs:
test:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x]

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install
run: npm ci
- name: npm test
run: npm test -- websocket-sample
env:
CI: true
36 changes: 36 additions & 0 deletions apps/websocket-sample/client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>

<head>
<title>WebSocket NestJS</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.dev.js"></script>
<script>
const socket = io('http://localhost:3000');
setInterval(() => {
if (socket.connected) {
socket.emit('message', { name: 'Test User' }, (data) => {
document.getElementById('socket-data').innerHTML +=
'<li>' + data.trim() + '<li/>';
});
}
}, 1000);
setInterval(() => socket.disconnect(), 30 * 1000);
</script>
<style>
.header {
padding: 2em;
}

.bordered {
border: 2px solid black;
}
</style>
</head>

<body>
<div class="header">
<h1>Websocket Testing</h1>
</div>
<div class="bordered">
<ul id="socket-data"></ul>
</div>
</body>
23 changes: 23 additions & 0 deletions apps/websocket-sample/src/app.gateway.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppGateway } from './app.gateway';

describe('AppGateway', () => {
let gateway: AppGateway;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AppGateway],
}).compile();

gateway = module.get<AppGateway>(AppGateway);
});

it('should be defined', () => {
expect(gateway).toBeDefined();
});

it('should be able to run gateway.handleMessage', () => {
expect(gateway.handleMessage({}, { name: 'Test' })).toBe('Hello, Test!');
expect(gateway.handleMessage({}, {})).toBe('Hello, World!');
});
});
9 changes: 9 additions & 0 deletions apps/websocket-sample/src/app.gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';

@WebSocketGateway()
export class AppGateway {
@SubscribeMessage('message')
handleMessage(client: any, payload: any): string {
return `Hello, ${payload.name || 'World'}!`;
}
}
14 changes: 14 additions & 0 deletions apps/websocket-sample/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { ServeStaticModule } from '@nestjs/serve-static';
import { AppGateway } from './app.gateway';
import { join } from 'path';

@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: join(process.cwd(), 'apps', 'websocket-sample', 'client'),
}),
],
providers: [AppGateway],
})
export class AppModule {}
8 changes: 8 additions & 0 deletions apps/websocket-sample/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
29 changes: 29 additions & 0 deletions apps/websocket-sample/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as io from 'socket.io-client';
import { AppModule } from './../src/app.module';

describe('AppController (e2e)', () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

afterEach(async () => {
await app.close();
});

it('should call message', async () => {
const socket = io.connect();
socket.emit('message', { name: 'Test' }, (data) => {
expect(data).toBe('Hello, Test!');
});
return socket.disconnect();
});
});
9 changes: 9 additions & 0 deletions apps/websocket-sample/test/jest-e2e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}
9 changes: 9 additions & 0 deletions apps/websocket-sample/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": false,
"outDir": "../../dist/apps/websocket-sample"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
3 changes: 3 additions & 0 deletions apps/websocket-sample/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tslint.json"
}
9 changes: 9 additions & 0 deletions nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
"compilerOptions": {
"tsConfigPath": "apps/typeorm-sample/tsconfig.app.json"
}
},
"websocket-sample": {
"type": "application",
"root": "apps/websocket-sample",
"entryFile": "main",
"sourceRoot": "apps/websocket-sample/src",
"compilerOptions": {
"tsConfigPath": "apps/websocket-sample/tsconfig.app.json"
}
}
}
}
Loading

0 comments on commit b7fc7cd

Please sign in to comment.