API for a simple real time message chat via websocket.
- NodeJs
- SocketIo
This Node application is a real time one to one chat via websockets that allow multiple channels conversation with another registered user.
To run this application Node must be installed. Just enter on the project folder via terminal and type the following comand:
npm run dev
or
yarn dev
You can also run the following commands to transpile the code to JavaScript version, that will be generated at the /dist
folder in the projet root.
npm run build
or
yarn build
After that, go to the browser and type or click in the link below:
http://localhost:3333/index.html
To test just open two pages and register the users. Then, placing the user in the To User
field the messages will arrive on the specified user chat.
The messages persist until restarting the application. If you refresh the page and give the same user, the chat that already exists will appear after the registration.
Chats are one to one, but not just two users. You can registry so many users (and open pages) you want. The messages will arive on the correct chat. An improvement possible is add group chats, what is easy to made just creating a custom room and joining the rescpetives scokets to them and use broadcast to a room. See the Socket.Io documentation
The messages are displayed in the same div, but this is just an example. If you want to use that on a best way you need to create chat tabs and handle the messages to show only the messages for that chat.
This is just a simple example for learning purpose. Feel free to use.