Skip to content

Commit

Permalink
Socket.io Back-end v3.6.15; Better way to use the room models.
Browse files Browse the repository at this point in the history
  • Loading branch information
dBish6 committed Mar 31, 2024
1 parent be46218 commit fc120e4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
4 changes: 4 additions & 0 deletions Node+Socket.io+React/back-end/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ patch:
- The SocketUser type is change to UserDTO.
- The leave and join messages now doesn't send to the sender because of front-end changes.
- The usersJoined room object keys are now a Map instead of a array + refactor of the userList method.

v3.6.15
patch:
- Added a better way to use the room models and they're properly initialize to avoid overwrites.
3 changes: 2 additions & 1 deletion Node+Socket.io+React/back-end/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "back-end",
"version": "3.6.14",
"version": "3.6.15",
"author": "David Bishop",
"description": "Demo Node server for a chat app.",
"repository": {
Expand All @@ -16,6 +16,7 @@
"dev": "cross-env NODE_ENV=development nodemon",
"build": "rollup -c",
"serve": "cross-env NODE_ENV=production node ./build/bundle.cjs",
"deploy": "fly deploy",
"clean": "rm -rf node_modules"
},
"dependencies": {
Expand Down
13 changes: 3 additions & 10 deletions Node+Socket.io+React/back-end/src/api/services/chatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
import { RoomIds } from "../../typings/RoomIds";
import { MessageDTO } from "../../dtos/MessageDto";

import { model } from "mongoose";
import { ChatAlphaSchema, ChatBravoSchema } from "../../model/Chat";
import { roomModals } from "../../model/Chat";
import CustomError from "../utils/CustomError";

export const getChats = async (roomId: RoomIds) => {
try {
const roomModel = model(
"",
roomId === "alpha" ? ChatAlphaSchema : ChatBravoSchema
);
const roomModel = roomModals[roomId];

return roomModel.find().sort({ timestamp: -1 }).limit(32);
} catch (error: any) {
Expand All @@ -29,10 +25,7 @@ export const storeMessage = async ({
roomId,
}: MessageDTO) => {
try {
const roomModel = model(
"",
roomId === "alpha" ? ChatAlphaSchema : ChatBravoSchema
);
const roomModel = roomModals[roomId];

const chat = new roomModel({
_id: `${userId}__${Math.random().toString(36).substring(2, 6)}`,
Expand Down
9 changes: 6 additions & 3 deletions Node+Socket.io+React/back-end/src/model/Chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema } from "mongoose";
import { Schema, model } from "mongoose";

export const ChatAlphaSchema = new Schema(
{
Expand All @@ -9,7 +9,6 @@ export const ChatAlphaSchema = new Schema(
},
{ collection: "chat_alpha" }
);
// export const ChatAlpha = model("chat_alpha", ChatAlphaSchema);

export const ChatBravoSchema = new Schema(
{
Expand All @@ -20,4 +19,8 @@ export const ChatBravoSchema = new Schema(
},
{ collection: "chat_bravo" }
);
// export const ChatBravo = model("chat_bravo", ChatBravoSchema);

export const roomModals = {
alpha: model("chat_alpha", ChatAlphaSchema),
bravo: model("chat_bravo", ChatBravoSchema),
};
4 changes: 2 additions & 2 deletions Node+Socket.io+React/back-end/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* Chat App Demo (back-end)
* Version: 3.6.14
* Version: 3.6.15
*
* Author: David Bishop
* Creation Date: December 10, 2023
* Last Updated: March 28, 2024
* Last Updated: March 31, 2024
*
* Description:
* This application is a demo chat app that allows users to exchange messages in real-time.
Expand Down

0 comments on commit fc120e4

Please sign in to comment.