I have written a tutorial about how I did this. You can find it here or here, or here
A React single page chat application (SPA)Ρ built with the following technologies:
- React with TypeScript (
create-react-app my-app --scripts-version=react-scripts-ts
) - Redux
- Express.js with Socket.io
- styled-components (SASS)
- Jest/Enzyme/Sinon
-
Header
- Chat tab - blinking when new message is received until is read, i.e. when the user is on Settings page
- Settings tab
- Unread messages counter
- Font Awesome icons
-
Chat page
- Chat area (includes left aligned and right aligned messages)
- Message (text, datetime, left or right depending on if it's received or sent)
- Showing nickname only for received message
- Message sender - input and button. Input is cleared and focused when button is clicked
- Send messages with Ctrl+Enter
- Auto scroll to bottom when the chat area is not enough to show all messages
-
Settings page
- UserProfile component - possibility to change user name
- Interface color component - change the color theme of the app
- ClockDisplay component - change the time mode 12h or 24h, shown with each message
- Send messages with Ctrl+Enter - On/Off
- LanguageSwitcher - dropdown menu allowing changing the language of the app (currently English and Deutsch are supported)
- Reset button - resets all settings stored to local storage
- Add video chat feature.
- π Added AM/PM time formatting for when 12h mode is selected.
- π Added possibility to send message via ENTER by default. If the setting to send messages with CTRL+ENTER is ON, then this is going to be the only way (except via mouse/touch of course).
- π Optimized for iDevices (media queries).
- π Fix blinking/active class for the Chat tab issue - related to React Router not able to properly re-render connected components https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md
- π Clear input field when new message is sent.
- π Auto scroll to bottom main chat area when new messages exceed available space.
- π Prevent 'doubling messages' (or multiple messages duplicates when more clients are connected).
- π Add unit tests for the react components.
- Add unit tests for redux stuff - reducers, store, action creators.
- π Add media queries for responsiveness - test and adjust on more devices.
- π Add demo to heroku.
- π Add nice how-to in README.
- Add animations for the messages (animejs).
- Add sounds (with options to turn on/off in settings).
- Add more color themes.
- Add welcome message (broadcasts on user connected).
- π Add icons ( use font awesome).
- History of all the conversations.
- Handle case when socket's connection state change (visually).
- Handle case when there has been a socket error.
- Handle case when a very long word (without) spaces is entered and it goes beyond the message background color.
- π Emoticons support - such as :D, :P, :), ;), π, β€οΈ, etc.
- π Link Parser - youtube link (embedded video should appear), link to an image (embedded image should appear), all other links should appear as anchor.
β mihails-chat.herokuapp.com β
To run the app, follow these steps.
- Ensure that NodeJS is installed.
- Install npm.
- From the project folder, execute the following commands:
To install dependencies:
npm i
To run the client app:
npm start
To run the server app:
npm run server:watch
To run the tests:
npm t
Browse to http://localhost:3000 and see the running app. In order to test, open it in two different computers, or computer and a phone, or two browsers on the same computer (one of it in incognito mode). If you are running it locally, make sure to change the host variable in Socket.ts (line:28) and its value to match your local network address - the one of the computer you are running the server app.
This step assumes you have already a Heroku account, installed heroku-cli and did login.
npm build
to build the project tobuild
folder.- Add
build
folder to be committed to Git - Make sure that express server loads static resources from it.
- Commit all:
git commit -m 'Deploy to Heroky'
. - Run
git push heroku master
. - Open the app from the given URL (in my case: mihails-chat.herokuapp.com).
- https://socket.io/get-started/chat
- https://www.typescriptlang.org/docs/handbook/decorators.html
- http://www.typescriptlang.org/
- http://www.typescriptlang.org/docs/handbook/compiler-options.html
- http://www.typescriptlang.org/docs/handbook/tsconfig-json.html
- https://github.com/Microsoft/TypeScript-React-Starter
- https://animejs.com/
- https://css-tricks.com/snippets/css/media-queries-for-standard-devices/
- https://www.styled-components.com/
- https://airbnb.io/enzyme/docs/guides/jest.html
- https://redux.js.org/api/store
- https://www.npmjs.com/package/linkifyjs
- https://docs.microlink.io/sdk/getting-started/react/
- https://www.npmjs.com/package/react-emojione
- https://www.npmjs.com/package/get-urls
MIT License
Copyright (c) 2019 Mihail Gaberov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.