Refactoring the app to be more extensible
For the refactoring, we will start by defining service functions for all chat functionality that our server provides.
Defining service functions
Follow these steps to get started defining service functions for the chat functionality:
- Create a new
backend/src/services/chat.js
file. - Inside it, import the service functions related to messages:
import { createMessage, getMessagesByRoom } from './messages.js'
- Define a new function to send a private message directly to a user:
export function sendPrivateMessage( socket, { username, room, message, replayed }, ) { socket.emit('chat.message', { username, message, room, replayed }) }
Private messages will be used to, for example, replay messages to a specific user, and are not stored in the database.
- Also, define a function to send a system message:
export function sendSystemMessage(io, { room, message }) { io.to...