Skip to content
Snippets Groups Projects
Unverified Commit 4fe64503 authored by Zdravko Iliev's avatar Zdravko Iliev
Browse files

fix: replace socket.io with web socket

parent 63ca360e
Branches
Tags
1 merge request!25feat: replace socket.io with websocket
Pipeline #64501 passed
...@@ -21,7 +21,6 @@ NATS_SUBJECTS="connections.*,proofs.*,credentials.*,credentials.definition.*,cre ...@@ -21,7 +21,6 @@ NATS_SUBJECTS="connections.*,proofs.*,credentials.*,credentials.definition.*,cre
GATEWAY_HTTP_PORT=8081 GATEWAY_HTTP_PORT=8081
GATEWAY_TCP_PORT=8881 GATEWAY_TCP_PORT=8881
GATEWAY_SOCKET_EVENT_NAME=message
GATEWAY_MESSAGE_PATTERN=webhook GATEWAY_MESSAGE_PATTERN=webhook
GATEWAY_HOST=0.0.0.0 GATEWAY_HOST=0.0.0.0
......
...@@ -126,9 +126,9 @@ Example: ...@@ -126,9 +126,9 @@ Example:
## Usage via Postman ## Usage via Postman
1. Import postman collection from the repo 1. Import postman collection from the repo
2. Connect to Socket.io on the gateway address, to listen for responses with [postman](https://learning.postman.com/docs/sending-requests/websocket/websocket) event is "message" 2. Connect to Web Socket on the gateway address, to listen for responses
3. Make a request 3. Make a request
4. Response will be sent to the Socket.io 4. Response will be sent to the Web Socket
## Send Didcomm messages between two OCMs ## Send Didcomm messages between two OCMs
......
...@@ -26,7 +26,8 @@ export class AppController { ...@@ -26,7 +26,8 @@ export class AppController {
2, 2,
)}`, )}`,
); );
this.eventsGateway.server.emit(this.gatewayConfig.socketEventName, dto);
this.eventsGateway.sentEvent(dto);
return "ok"; return "ok";
} }
} }
import { WebSocketGateway, WebSocketServer } from "@nestjs/websockets"; import { WebSocketGateway, WebSocketServer } from "@nestjs/websockets";
import { Server } from "socket.io"; import { Server, WebSocket } from "ws";
import { CloudEventDto } from "@ocm-engine/dtos";
@WebSocketGateway() @WebSocketGateway()
export class EventsGateway { export class EventsGateway {
@WebSocketServer() @WebSocketServer()
server: Server; server: Server;
sentEvent<T>(dto: CloudEventDto<T>) {
this.server.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(dto));
}
});
}
} }
...@@ -12,10 +12,12 @@ import { MicroserviceOptions, Transport } from "@nestjs/microservices"; ...@@ -12,10 +12,12 @@ import { MicroserviceOptions, Transport } from "@nestjs/microservices";
import { ConfigService } from "@nestjs/config"; import { ConfigService } from "@nestjs/config";
import { IGateway } from "@ocm-engine/config"; import { IGateway } from "@ocm-engine/config";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger"; import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { WsAdapter } from "@nestjs/platform-ws";
import * as fs from "fs"; import * as fs from "fs";
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
app.useWebSocketAdapter(new WsAdapter(app));
const configService = app.get(ConfigService); const configService = app.get(ConfigService);
const gatewayConfig = configService.get<IGateway>("gateway")!; const gatewayConfig = configService.get<IGateway>("gateway")!;
...@@ -24,7 +26,7 @@ async function bootstrap() { ...@@ -24,7 +26,7 @@ async function bootstrap() {
app.setGlobalPrefix(globalPrefix); app.setGlobalPrefix(globalPrefix);
app.enableShutdownHooks(); app.enableShutdownHooks();
const microservice = app.connectMicroservice<MicroserviceOptions>({ app.connectMicroservice<MicroserviceOptions>({
transport: Transport.TCP, transport: Transport.TCP,
options: { options: {
host: gatewayConfig.host, host: gatewayConfig.host,
......
...@@ -20,7 +20,6 @@ NATS_SUBJECTS="connections.*,proofs.*,credentials.*,schemas.*,messages.*" ...@@ -20,7 +20,6 @@ NATS_SUBJECTS="connections.*,proofs.*,credentials.*,schemas.*,messages.*"
GATEWAY_HTTP_PORT=8081 GATEWAY_HTTP_PORT=8081
GATEWAY_TCP_PORT=8881 GATEWAY_TCP_PORT=8881
GATEWAY_SOCKET_EVENT_NAME=message
GATEWAY_MESSAGE_PATTERN=webhook GATEWAY_MESSAGE_PATTERN=webhook
GATEWAY_HOST=gateway-holder GATEWAY_HOST=gateway-holder
......
...@@ -20,7 +20,6 @@ NATS_SUBJECTS="connections.*,proofs.*,credentials.*,schemas.*" ...@@ -20,7 +20,6 @@ NATS_SUBJECTS="connections.*,proofs.*,credentials.*,schemas.*"
GATEWAY_HTTP_PORT=8081 GATEWAY_HTTP_PORT=8081
GATEWAY_TCP_PORT=8881 GATEWAY_TCP_PORT=8881
GATEWAY_SOCKET_EVENT_NAME=message
GATEWAY_MESSAGE_PATTERN=webhook GATEWAY_MESSAGE_PATTERN=webhook
GATEWAY_HOST=gateway-holder GATEWAY_HOST=gateway-holder
......
...@@ -20,7 +20,6 @@ NATS_SUBJECTS="connections.*,proofs.*,credentials.*,credentials.definition.*,cre ...@@ -20,7 +20,6 @@ NATS_SUBJECTS="connections.*,proofs.*,credentials.*,credentials.definition.*,cre
GATEWAY_HTTP_PORT=8081 GATEWAY_HTTP_PORT=8081
GATEWAY_TCP_PORT=8881 GATEWAY_TCP_PORT=8881
GATEWAY_SOCKET_EVENT_NAME=message
GATEWAY_MESSAGE_PATTERN=webhook GATEWAY_MESSAGE_PATTERN=webhook
GATEWAY_HOST=gateway-issuer GATEWAY_HOST=gateway-issuer
......
...@@ -20,7 +20,6 @@ NATS_SUBJECTS="connections.*,proofs.*,credentials.*,schemas.*" ...@@ -20,7 +20,6 @@ NATS_SUBJECTS="connections.*,proofs.*,credentials.*,schemas.*"
GATEWAY_HTTP_PORT=8081 GATEWAY_HTTP_PORT=8081
GATEWAY_TCP_PORT=8881 GATEWAY_TCP_PORT=8881
GATEWAY_SOCKET_EVENT_NAME=message
GATEWAY_MESSAGE_PATTERN=webhook GATEWAY_MESSAGE_PATTERN=webhook
GATEWAY_HOST=gateway-issuer GATEWAY_HOST=gateway-issuer
......
...@@ -7,7 +7,6 @@ export const gatewayConfig = registerAs( ...@@ -7,7 +7,6 @@ export const gatewayConfig = registerAs(
(): IGateway => ({ (): IGateway => ({
httpPort: parseInt(process.env["GATEWAY_HTTP_PORT"]!), httpPort: parseInt(process.env["GATEWAY_HTTP_PORT"]!),
tcpPort: parseInt(process.env["GATEWAY_TCP_PORT"]!), tcpPort: parseInt(process.env["GATEWAY_TCP_PORT"]!),
socketEventName: process.env["GATEWAY_SOCKET_EVENT_NAME"]!,
messagePattern: process.env["GATEWAY_MESSAGE_PATTERN"]!, messagePattern: process.env["GATEWAY_MESSAGE_PATTERN"]!,
host: process.env["GATEWAY_HOST"]!, host: process.env["GATEWAY_HOST"]!,
}), }),
......
export interface IGateway { export interface IGateway {
httpPort: number; httpPort: number;
tcpPort: number; tcpPort: number;
socketEventName: string;
messagePattern: string; messagePattern: string;
host: string; host: string;
} }
...@@ -3,7 +3,6 @@ import Joi from "joi"; ...@@ -3,7 +3,6 @@ import Joi from "joi";
export const gatewaySchema = Joi.object({ export const gatewaySchema = Joi.object({
GATEWAY_HTTP_PORT: Joi.string().required(), GATEWAY_HTTP_PORT: Joi.string().required(),
GATEWAY_TCP_PORT: Joi.string().required(), GATEWAY_TCP_PORT: Joi.string().required(),
GATEWAY_SOCKET_EVENT_NAME: Joi.string().required(),
GATEWAY_MESSAGE_PATTERN: Joi.string().required(), GATEWAY_MESSAGE_PATTERN: Joi.string().required(),
GATEWAY_HOST: Joi.string().required(), GATEWAY_HOST: Joi.string().required(),
}); });
...@@ -14,24 +14,21 @@ ...@@ -14,24 +14,21 @@
"build:pm:production": "nx run proof-manager:build:production", "build:pm:production": "nx run proof-manager:build:production",
"build:gw": "nx run gateway:build:development --parallel=3", "build:gw": "nx run gateway:build:development --parallel=3",
"build:gw:production": "nx run gateway:build:production", "build:gw:production": "nx run gateway:build:production",
"serve:all": "concurrently \"yarn serve:agent\" \"yarn serve:cm\" \"yarn serve:am\" \"yarn serve:pm\" \"yarn serve:gw\"", "serve:all": "concurrently \"yarn serve:agent\" \"yarn serve:cm\" \"yarn serve:am\" \"yarn serve:pm\" \"yarn serve:gw\"",
"serve:agent": "nx run agent:serve:development", "serve:agent": "nx run agent:serve:development",
"serve:cm": "nx run connection-manager:serve:development", "serve:cm": "nx run connection-manager:serve:development",
"serve:am": "nx run attestation-manager:serve:development", "serve:am": "nx run attestation-manager:serve:development",
"serve:pm": "nx run proof-manager:serve:development", "serve:pm": "nx run proof-manager:serve:development",
"serve:gw": "nx run gateway:serve:development", "serve:gw": "nx run gateway:serve:development",
"infra": "cd compose && docker-compose --profile issuer --profile holder up -d --build", "infra": "cd compose && docker-compose --profile issuer --profile holder up -d --build",
"infra:down": "cd compose && docker-compose --profile issuer --profile holder down", "infra:down": "cd compose && docker-compose --profile issuer --profile holder down",
"infra:status" : "cd compose && docker-compose ps -a", "infra:status": "cd compose && docker-compose ps -a",
"infra:issuer": "cd compose && docker-compose --profile issuer up -d --build", "infra:issuer": "cd compose && docker-compose --profile issuer up -d --build",
"infra:holder": "cd compose && docker-compose --profile holder up -d --build", "infra:holder": "cd compose && docker-compose --profile holder up -d --build",
"infra:issuer:stop": "cd compose && docker-compose --profile issuer stop", "infra:issuer:stop": "cd compose && docker-compose --profile issuer stop",
"infra:holder:stop": "cd compose && docker-compose --profile holder stop", "infra:holder:stop": "cd compose && docker-compose --profile holder stop",
"infra:local": "cd compose && docker-compose -f=docker-compose.infra.yml up -d", "infra:local": "cd compose && docker-compose -f=docker-compose.infra.yml up -d",
"infra:local:stop": "cd compose && docker-compose -f=docker-compose.infra.yml stop", "infra:local:stop": "cd compose && docker-compose -f=docker-compose.infra.yml stop",
"infra:simple": "cd compose && docker-compose -f docker-compose.simple.yml up -d" "infra:simple": "cd compose && docker-compose -f docker-compose.simple.yml up -d"
}, },
"private": true, "private": true,
...@@ -45,7 +42,7 @@ ...@@ -45,7 +42,7 @@
"@nestjs/core": "^9.0.0", "@nestjs/core": "^9.0.0",
"@nestjs/microservices": "^9.4.2", "@nestjs/microservices": "^9.4.2",
"@nestjs/platform-express": "^9.0.0", "@nestjs/platform-express": "^9.0.0",
"@nestjs/platform-socket.io": "^9.4.2", "@nestjs/platform-ws": "^10.1.3",
"@nestjs/websockets": "^9.4.2", "@nestjs/websockets": "^9.4.2",
"async-retry": "^1.3.3", "async-retry": "^1.3.3",
"axios": "^1.0.0", "axios": "^1.0.0",
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment