Skip to content

Commit fbab228

Browse files
committed
Create websocket demo project
1 parent b4fdd80 commit fbab228

38 files changed

+8122
-1
lines changed

articles/sse-js-crypto-feed/docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: '3.8'
21

32
services:
43
sse-app:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
npm-debug.log
3+
Dockerfile
4+
docker-compose.yml
5+
.git
6+
.env
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copy this file to .env and set your ngrok auth token
2+
NGROK_AUTHTOKEN=
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Node Modules
2+
node_modules
3+
4+
# Environment variables
5+
.env
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM node:18-alpine
2+
WORKDIR /app
3+
COPY package*.json ./
4+
RUN npm ci --omit=dev
5+
COPY . .
6+
EXPOSE 3000
7+
CMD ["npm", "start"]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Chatbot WebSocket Demo
2+
3+
This project provides a minimal WebSocket server and a React + Tailwind CSS client. It can be used to experiment with WebSocket connections for a chatbot-style application or to run load tests with Gatling.
4+
5+
## Features
6+
7+
- Node.js server with a handcrafted WebSocket implementation.
8+
- React frontend styled with Tailwind CSS and a simple SaaS look for **Acme Healthcare**.
9+
- Gatling load test examples in JavaScript and TypeScript.
10+
- Docker and ngrok setup for exposing the server to the internet, for remote load testing with Gatling Enterprise Edition.
11+
12+
## Running the Server
13+
14+
```bash
15+
node server.js
16+
```
17+
18+
Open your browser to `http://localhost:3000` and chat with the bot. Messages are echoed back with an "AI" prefix.
19+
20+
To gracefully end a session, send the message `close` and the server will close the WebSocket connection.
21+
22+
## Running a load test locally
23+
24+
To simulate thousands of concurrent connections using [Gatling](https://gatling.io/). Point the WebSocket scenario at `ws://localhost:3000` and configure the desired number of virtual users. There is a companion sample load test available [WebSockets JS load test](https://github.com/stb13579/WebSocketTestJS).
25+
26+
27+
## Running a load test remotely
28+
29+
To test the WebSocket server from a remote location, you can use the included Docker and ngrok setup to expose the server to the internet. This is useful for load testing with Gatling Enterprise Edition. First, expose the server using Docker and ngrok as described below. Then, configure your Gatling scenario to point to the ngrok URL.
30+
31+
### Application setup with Docker and ngrok
32+
33+
This repository includes a `Dockerfile` and `docker-compose.yml` to expose the server through [ngrok](https://ngrok.com) for remote load testing.
34+
35+
1. Create an ngrok account and obtain an auth token.
36+
2. Copy `.env.example` to `.env` and add your token: `NGROK_AUTHTOKEN=<token>`.
37+
3. Start the stack:
38+
39+
```bash
40+
docker compose up --build
41+
```
42+
43+
The ngrok service will print a forwarding URL such as `https://XXXX.ngrok-free.app`. Use this URL in your remote load testing tool to target the WebSocket server.
44+
45+
### Gatling Enterprise Edition setup
46+
1. Log in to your Gatling Enterprise Edition account.
47+
2. Create a new simulation and configure it to use the ngrok URL as the WebSocket endpoint.
48+
3. Run your load test and analyze the results in the Gatling dashboard.
49+
4. Monitor the server logs in the Docker container to see incoming connections and messages.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: "3.9"
2+
3+
services:
4+
app:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
ports:
9+
- "3000:3000"
10+
11+
ngrok:
12+
image: ngrok/ngrok:latest
13+
command:
14+
- http
15+
- app:3000
16+
environment:
17+
NGROK_AUTHTOKEN: ${NGROK_AUTHTOKEN:?NGROK_AUTHTOKEN must be set}
18+
depends_on:
19+
- app
20+
ports:
21+
- "4040:4040"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
target/
3+
4+
# Gatling run results
5+
results/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Gatling JS - JavaScript and TypeScript demo projects
2+
3+
A simple showcase of JavaScript and TypeScript NPM projects using Gatling JS. Please also check out the [introduction to JavaScript scripting](https://docs.gatling.io/tutorials/scripting-intro-js/) in the Gatling documentation.
4+
5+
## Prerequisites
6+
7+
You need [Node.js](https://nodejs.org/en/download) v18 or later (LTS versions only) and npm v8 or later (included with Node.js).
8+
9+
## Use demo project
10+
11+
Run the typeScript sample:
12+
13+
```shell
14+
cd typescript
15+
npm install
16+
npx gatling run --typescript --simulation basicSimulation # automatically download Gatling runtime, build the project, and run the basicSimulation simulation
17+
```
18+
19+
Or the JavaScript sample:
20+
21+
```shell
22+
cd javascript
23+
npm install
24+
npx gatling run --simulation basicSimulation # automatically download Gatling runtime, build the project, and run the basicSimulation simulation
25+
```
26+
27+
You can also launch the [Gatling Recorder](https://docs.gatling.io/tutorials/recorder/) and use it to capture browser-based actions and help create a realistic user scenario:
28+
29+
```shell
30+
npx gatling recorder
31+
```
32+
33+
The `gatling` command-line tool has a built-in help function:
34+
35+
```shell
36+
npx gatling --help # List all available commands
37+
npx gatling run --help # List options for the "run" command (--help also works for all other available commands)
38+
```
39+
40+
## Included helper scripts
41+
42+
Note that both sample projects include a few aliases in the `package.json`'s `scripts` section, which you can use for convenience or refer to as examples:
43+
44+
```shell
45+
npm run clean # Delete Gatling bundled code and generated reports
46+
npm run format # Format code with prettier
47+
npm run check # TypeScript project only, type check but don't build or run
48+
npm run build # Build project but don't run
49+
npm run basicSimulation # Run the included basicSimulation simulation
50+
npm run recorder # Starts the Gatling Recorder
51+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Learn more about Configuration as Code: https://docs.gatling.io/reference/execute/cloud/user/configuration-as-code
2+
# This file is full deployment configuration file example.
3+
# Please note all these configuration fields, or even this file, are optional.
4+
5+
gatling.enterprise.package {
6+
# Consistent deployment using an existing package ID
7+
# https://docs.gatling.io/reference/execute/cloud/user/configuration-as-code/#consistent-deployment-with-id
8+
# id = "00000000-0000-0000-0000-000000000000"
9+
name = "My package name"
10+
team = "Default team" # or ID with team = "00000000-0000-0000-0000-000000000000"
11+
default {
12+
simulation {
13+
locations = [
14+
{
15+
name: "Europe - Paris",
16+
size: 2,
17+
weight: 30
18+
},
19+
{
20+
name: "AP Pacific - Mumbai",
21+
size: 2,
22+
weight: 70
23+
}
24+
]
25+
parameters {
26+
ignoreDefaults = false
27+
systemProperties {
28+
# System properties names should be surrounded by quotes
29+
# Otherwise, it would be interpreted as HOCON keys
30+
"com.example.prop.1" = "default value from system prop 1"
31+
"com.example.prop.2" = "default value from system prop 2"
32+
}
33+
environmentVariables {
34+
MY_SIMULATION_ENV_VAR_1 = "default value from environment 1"
35+
MY_SIMULATION_ENV_VAR_2 = "default value from environment 2"
36+
}
37+
}
38+
timeWindow {
39+
rampUp = 10 # in seconds
40+
rampDown = 10 # in seconds
41+
}
42+
}
43+
}
44+
simulations = [
45+
{
46+
# Consistent deployment using an existing simulation ID
47+
# https://docs.gatling.io/reference/execute/cloud/user/configuration-as-code/#consistent-deployment-with-id
48+
# id = "00000000-0000-0000-0000-000000000001"
49+
name = "My simulation name"
50+
# Name of the simulation (i.e.: prefix of the simulation file src/*.gatling.js)
51+
simulation = "basicSimulation"
52+
}
53+
]
54+
}

0 commit comments

Comments
 (0)