Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple react frontend to manage sessions and add a websocket server #253

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Ignore build
build

# Ignore node_modules
node_modules

Expand Down Expand Up @@ -27,4 +30,4 @@ Dockerfile
docker-compose.yml
swagger.yml
.github
assets
assets
13 changes: 8 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
## Application ##
PORT=3000 # OPTIONAL, DEFAULT 3000
API_PORT=5000 # OPTIONAL, DEFAULT 5000
API_KEY=your_global_api_key_here # OPTIONAL, DEFAULT EMPTY
BASE_WEBHOOK_URL=http://localhost:3000/localCallbackExample # MANDATORY
BASE_WEBHOOK_URL=http://localhost:5000/localCallbackExample # MANDATORY
ENABLE_LOCAL_CALLBACK_EXAMPLE=TRUE # OPTIONAL, DISABLE FOR PRODUCTION
RATE_LIMIT_MAX=1000 # OPTIONAL, THE MAXIUM NUMBER OF CONNECTIONS TO ALLOW PER TIME FRAME
RATE_LIMIT_MAX=1000 # OPTIONAL, THE MAXIMUM NUMBER OF CONNECTIONS TO ALLOW PER TIME FRAME
RATE_LIMIT_WINDOW_MS=1000 # OPTIONAL, TIME FRAME FOR WHICH REQUESTS ARE CHECKED IN MS

USE_FRONTEND=TRUE # Use the frontend to help managing sessions (This will overwrite empty USE_WEB_SOCKET_PORT to 8000)
USE_WEB_SOCKET_PORT=8000 # Specify the websocket port if it is intended to be used

## Client ##
MAX_ATTACHMENT_SIZE=10000000 # IF REACHED, MEDIA ATTACHMENT BODY WILL BE NULL
SET_MESSAGES_AS_SEEN=TRUE # WILL MARK THE MESSAGES AS READ AUTOMATICALLY
SET_MESSAGES_AS_SEEN=FALSE # WILL MARK THE MESSAGES AS READ AUTOMATICALLY
# ALL CALLBACKS: auth_failure|authenticated|call|change_state|disconnected|group_join|group_leave|group_update|loading_screen|media_uploaded|message|message_ack|message_create|message_reaction|message_revoke_everyone|qr|ready|contact_changed|unread_count|message_edit|message_ciphertext
DISABLED_CALLBACKS=message_ack|message_reaction|unread_count|message_edit|message_ciphertext # PREVENT SENDING CERTAIN TYPES OF CALLBACKS BACK TO THE WEBHOOK
WEB_VERSION='2.2328.5' # OPTIONAL, THE VERSION OF WHATSAPP WEB TO USE
Expand All @@ -18,4 +21,4 @@ RECOVER_SESSIONS=TRUE # OPTIONAL, SHOULD WE RECOVER THE SESSION IN CASE OF PAGE
## Session File Storage ##
SESSIONS_PATH=./sessions # OPTIONAL

ENABLE_SWAGGER_ENDPOINT=TRUE # OPTIONAL
ENABLE_SWAGGER_ENDPOINT=TRUE # OPTIONAL
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
node_modules

# Ignore dotenv files
.env
/.env
# for cpanel or shared hosting purpose
.htaccess

Expand All @@ -20,3 +20,6 @@ coverage
# Ignore other unnecessary files
.DS_Store
.vscode

# Ignore idea settings
.idea
9 changes: 7 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ RUN npm ci --only=production --ignore-scripts
# Copy the rest of the source code to the working directory
COPY . .

WORKDIR /usr/src/app/frontend
RUN npm run build
WORKDIR /usr/src/app

# Expose the port the API will run on
EXPOSE 3000
EXPOSE 5000
EXPOSE 8000

# Start the API
CMD ["npm", "start"]
CMD ["node", "server.js"]
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ cd whatsapp-api
```bash
docker-compose pull && docker-compose up
```
4. Visit http://localhost:3000/session/start/ABCD
4. Visit http://localhost:5000/session/start/ABCD

5. Scan the QR on your console using WhatsApp mobile app -> Linked Device -> Link a Device (it may take time to setup the session)

6. Visit http://localhost:3000/client/getContacts/ABCD
6. Visit http://localhost:5000/client/getContacts/ABCD

7. EXTRA: Look at all the callbacks data in `./session/message_log.txt`

Expand Down Expand Up @@ -123,7 +123,32 @@ cp .env.example .env
npm run start
```

5. Access the API at `http://localhost:3000`
5. Access the API at `http://localhost:5000`

## Optional: Use WebSocket

You can set up a websocket server, by setting `USE_FRONTEND` to `TRUE` (default port is 8000) or specifying `USE_WEB_SOCKET_PORT`.

The websocket server will require authentication token if environment variable `API_KEY` is specified.

### Production
There is a pitfalls to serve isomorphic application if your infrastructure provider does not allow specifying multiple ports.
Consider using nginx and route `/ws` `/socket.io` requests to websocket ports.

## Optional: Use Session Management UI

If `USE_FRONTEND` is set to `TRUE`, node server will look for the frontend build (defaulted to `./frontend/build`) and try to serve the frontend.

Frontend UI will use websocket for event handling.

Therefore, it is needed to run the react build first by:
```bash
cd frontend
# npm install (if packages are not installed)
npm run build
```

By using the UI, all the requests not intended for api will be routed to the frontend index.

## Testing

Expand Down
26 changes: 6 additions & 20 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
version: '3.8'

services:
app:
container_name: whatsapp_web_api
# build: .
image: chrishubert/whatsapp-web-api:latest # Pull the image from Docker Hub
build: .
restart: always
ports:
- "3000:3000"
environment:
# - API_KEY=your_global_api_key_here # OPTIONAL
- BASE_WEBHOOK_URL=http://localhost:3000/localCallbackExample
- ENABLE_LOCAL_CALLBACK_EXAMPLE=TRUE # OPTIONAL, NOT RECOMMENDED FOR PRODUCTION
- MAX_ATTACHMENT_SIZE=5000000 # IN BYTES
- SET_MESSAGES_AS_SEEN=TRUE # WILL MARK THE MESSAGES AS READ AUTOMATICALLY
# ALL CALLBACKS: auth_failure|authenticated|call|change_state|disconnected|group_join|group_leave|group_update|loading_screen|media_uploaded|message|message_ack|message_create|message_reaction|message_revoke_everyone|qr|ready|contact_changed
- DISABLED_CALLBACKS=message_ack|message_reaction # PREVENT SENDING CERTAIN TYPES OF CALLBACKS BACK TO THE WEBHOOK
- ENABLE_SWAGGER_ENDPOINT=TRUE # OPTIONAL, ENABLES THE /api-docs ENDPOINT
# - RATE_LIMIT_MAX=1000 # OPTIONAL, THE MAXIUM NUMBER OF CONNECTIONS TO ALLOW PER TIME FRAME
# - RATE_LIMIT_WINDOW_MS=1000 # OPTIONAL, TIME FRAME FOR WHICH REQUESTS ARE CHECKED IN MS
# - WEB_VERSION='2.2328.5' # OPTIONAL, THE VERSION OF WHATSAPP WEB TO USE
# - WEB_VERSION_CACHE_TYPE=none # OPTIONAL, DETERMINES WHERE TO GET THE WHATSAPP WEB VERSION(local, remote or none), DEFAULT 'none'
# - RECOVER_SESSIONS=TRUE # OPTIONAL, SHOULD WE RECOVER THE SESSION IN CASE OF PAGE FAILURES
- "5000:5000"
- "8000:8000"
env_file:
- .env.example
volumes:
- ./sessions:/usr/src/app/sessions # Mount the local ./sessions/ folder to the container's /usr/src/app/sessions folder
- ./sessions:/usr/src/app/sessions
6 changes: 6 additions & 0 deletions frontend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Specify how the which port frontend websocket client should be connected to.
REACT_APP_USE_WEB_SOCKET_PORT=8000
# Specify how the which port frontend rest api client should be connected to.
REACT_APP_BACKEND_RESTAPI_PORT=5000
# The backend url for the rest client to connect to
REACT_APP_BACKEND_URL=http://localhost
23 changes: 23 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# keep production build
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
70 changes: 70 additions & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:5000) to view it in your browser.

The page will reload when you make changes.\
You may also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can't go back!**

If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
Loading