Skip to content

Commit

Permalink
websocket separation
Browse files Browse the repository at this point in the history
  • Loading branch information
Manik2708 authored and Manik2708 committed Jun 13, 2024
1 parent 6f05adc commit a355fe0
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 29 deletions.
7 changes: 7 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ services:
timeout: 10s
retries: 10
start_period: 20s
io-server:
image: manik2708/hi-io-server:latest
environment:
REDIS_URL: redis://client:6379
container_name: hi-io-server
networks:
- hi-services
networks:
hi-services:
external: true
Expand Down
27 changes: 16 additions & 11 deletions src/Controllers/RetrieveData/Services/retrieve_data_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ export class RetrieveDataServices {
socket_id!,
ommited_message,
user_id,
this.client
this.client,
);
switch (message.message_type) {
case MessageType.CONFESSION_MESSAGE_TYPE:
await message_sender.sendMessage(EventNames.recieveConfession);
chnl.ack(msg);
break;
case MessageType.UPDATE_CONFESSION_STATUS:
await message_sender.sendMessage(EventNames.updateConfssionStatus);
await message_sender.sendMessage(
EventNames.updateConfssionStatus,
);
chnl.ack(msg);
break;
case MessageType.ACCEPT_CONFESSION_TYPE:
Expand Down Expand Up @@ -76,25 +78,28 @@ class SendMessageToWebsocketServices {
private redis: RedisClientType;
private socket_id: string;
private message: any;
private user_id: string
private user_id: string;

constructor(
socket_id: string,
message: any,
user_id: string,
redis: RedisClientType
redis: RedisClientType,
) {
this.socket_id = socket_id;
this.message = message;
this.redis = redis;
this.user_id = user_id
this.user_id = user_id;
}

sendMessage = async (name: string) => {
await this.redis.PUBLISH(this.user_id, JSON.stringify({
id: this.socket_id,
name: name,
data: this.message
}))
await this.redis.PUBLISH(
this.user_id,
JSON.stringify({
id: this.socket_id,
name: name,
data: this.message,
}),
);
};
}
}
13 changes: 8 additions & 5 deletions src/Services/send_message_to_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ export class SendMessageToUserService {
RedisNames.OnlineUserMap + userId,
RedisNames.SocketId,
);
await this.client.PUBLISH(userId, JSON.stringify({
id: socketid!,
name: userIsOnlineEvent,
data: messageForOnlineUser,
}))
await this.client.PUBLISH(
userId,
JSON.stringify({
id: socketid!,
name: userIsOnlineEvent,
data: messageForOnlineUser,
}),
);
if (afterAcknowledgement) {
await afterAcknowledgement();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
describe,
it,
expect,
beforeAll,
afterEach,
} from '@jest/globals';
import { describe, it, expect, beforeAll, afterEach } from '@jest/globals';
import { delay } from '../../../Helpers/get_testing_app';
import { SendMessageToUserService } from '../../../../src/Services/send_message_to_user';
import { CassandraDatabaseQueries } from '../../../../src/Database/Cassandra/queries';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Send confession tests', () => {
let socket: Socket;
let outputData: any;
let cassandraClient: Client;
let crushId = nanoid().toLowerCase()
let crushId = nanoid().toLowerCase();
beforeAll(async () => {
redisClient = await TestServiceContainers.getTestingRedisClient().connect();
const moduleRef = await getTestingGlobalServicesModule();
Expand Down
5 changes: 2 additions & 3 deletions test/Helpers/create_socket_client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { INestApplication } from '@nestjs/common';
import { io as IoClient, Socket as ClientSocket } from 'socket.io-client';

export const initClientSocket = async (
Expand All @@ -12,8 +11,8 @@ export const initClientSocket = async (

socket.on('connect', () => {
socket.emit('subscribe', {
userId: userId
})
userId: userId,
});
callback(socket);
resolve(socket);
});
Expand Down
24 changes: 22 additions & 2 deletions test/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ services:
retries: 3
start_period: 20s
container_name: client-testing
networks:
- hi-test-services
rabbitmq-testing:
image: rabbitmq
ports:
Expand All @@ -23,7 +25,9 @@ services:
interval: 10s
timeout: 3s
retries: 3
start_period: 20s
start_period: 20s
networks:
- hi-test-services
cassandra-testing:
image: cassandra:latest
ports:
Expand All @@ -35,6 +39,8 @@ services:
timeout: 10s
retries: 10
start_period: 20s
networks:
- hi-test-services
mongodb-testing:
image: mongo:latest
ports:
Expand All @@ -46,4 +52,18 @@ services:
timeout: 10s
retries: 10
start_period: 20s

networks:
- hi-test-services
io-server-testing:
image: manik2708/hi-io-server:latest
ports:
- "3500:3500"
environment:
REDIS_URL: redis://client-testing:6379
PORT: 3500
container_name: hi-io-server-testing
networks:
- hi-test-services
networks:
hi-test-services:
driver: bridge

0 comments on commit a355fe0

Please sign in to comment.