Messaging within an ERP business systemHaving an internal messaging system could be an advantage to ensure all information is kept within the company. As explained last week, until now, we had a separate WebSocket chat server, which we have now replaced with a WebSocket server within the Moqui back-end. This reduces the number of components in the GrowERP system. By following the tactics below, messaging can be added to any business system with ease, eliminating the need for external chat SaaS offerings. OverviewThe Moqui back-end provides WebSocket notification services, which we used as the basis for the new back-end chat server since both use the WebSocket protocol. On the front-end, we moved the chat functionality into its own package to keep things organized. We also modified the automated tests and Docker generation scripts in the Flutter directory by removing the external chat server image. Messaging operates in two parallel ways: via WebSocket for a "live" connection, where chat message data updates without refreshing the screen, and via a REST interface to store messages in the database for history purposes and when the other user is not logged in. The Moqui Back-endFor the WebSocket chat server within the back-end, we created a new web app with the path 'chat' in the GrowERP Moqui configuration file. This is merged with the default configuration file and a Groovy file in the Moqui webapp directory. The notification service, which this chat endpoint is based on, requires a user to be logged in via a web browser using an HTTP session. However, GrowERP does not use HTTP sessions because even the browser implementation of our front-end utilizes the REST interface. We solved this issue by implementing a REST call within the chat server that includes an API key as part of the WebSocket connection function. This function fails if the API key is invalid. Since the user ID is unavailable in this context, it is provided in the WebSocket connection function by the front-end. The Flutter Front-endThe Chat Client Setup FileThe chat client provides basic functions such as setting up the URL for the connection service, sending messages, closing the connection, and providing a stream of incoming data. The Chat Message Bloc (Business Logic)The chat message bloc subscribes to the stream function of the client setup file during initialization. It ensures that initial unread chats are requested from the database and that sent messages are saved in the database. Incoming data from the stream is added to the list by the ChatMessageReceiveWs bloc function. The Chat Message DialogFinally, the chat message dialog displays the data sent by the message bloc and refreshes the screen when new data is added. This concludes the chat implementation in the back-end. Next week, we will demonstrate how you can use the Moqui notification services not just for sending messages but also for automatically updating the order list screen when new orders arrive. If you found this article valuable, don't keep it to yourself—spread the word! Share it with your friends, family, and colleagues, and let's inspire more people to create business applications! If you were forwarded this email, subscribe here. Thanks for reading! Any comments? Send them to support@growerp.com. |