Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Postgres adapter for socket communication #449

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export class ConnectSocketIoAttachmentsDto {
id: number;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@






export class CreateSocketIoAttachmentsDto {
payload: Buffer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export * from './connect-socketIoAttachments.dto';
export * from './create-socketIoAttachments.dto';
export * from './update-socketIoAttachments.dto';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@






export class UpdateSocketIoAttachmentsDto {
payload?: Buffer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export * from './socketIoAttachments.entity';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@




export class SocketIoAttachments {
id: number ;
createdAt: Date ;
payload: Buffer ;
}
5 changes: 5 additions & 0 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { setupSwagger } from './config/swagger.config'
import { setupExceptions } from './config/exceptions.config'
import { setupValidation } from './config/validation.config'
import { setupShutdownHooks } from './config/shutdown.config'
import { PostgresIoAdapter } from './sockets/notifications/adapter'

const globalPrefix = process.env.GLOBAL_PREFIX ?? 'api/v1'
const logLevels: LogLevel[] = ['error', 'warn']
Expand All @@ -27,6 +28,10 @@ async function bootstrap() {
app.setGlobalPrefix(globalPrefix)
app.enableVersioning({ type: VersioningType.URI })

const postgresIoAdapter = new PostgresIoAdapter(app)
await postgresIoAdapter.connectToPostgres()
app.useWebSocketAdapter(postgresIoAdapter)

const appVersion = process.env.APP_VERSION || 'unknown'
setupHelmet(app)
setupCors(app)
Expand Down
29 changes: 29 additions & 0 deletions apps/api/src/sockets/notifications/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Logger } from '@nestjs/common'
import { IoAdapter } from '@nestjs/platform-socket.io'
import { createAdapter } from '@socket.io/postgres-adapter'
import { Pool } from 'pg'
import { ServerOptions } from 'socket.io'

const { DB_PORT, DB_USER, DB_PASS, DB_HOST } = process.env

export class PostgresIoAdapter extends IoAdapter {
private adapterConstructor: ReturnType<typeof createAdapter>

async connectToPostgres(): Promise<void> {
const pool = new Pool({
user: DB_USER,
host: DB_HOST,
database: 'postgres',
password: DB_PASS,
port: Number(DB_PORT) || 5432,
})

this.adapterConstructor = createAdapter(pool)
}

createIOServer(port: number, options?: ServerOptions): any {
const server = super.createIOServer(port, options)
server.adapter(this.adapterConstructor)
return server
}
}
3 changes: 2 additions & 1 deletion apps/api/src/sockets/notifications/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {
WebSocketServer,
} from '@nestjs/websockets'
import { Logger } from '@nestjs/common'
import { Server } from 'socket.io'

@WebSocketGateway({ namespace: '/api/v1', transport: 'websocket' })
export class NotificationGateway
implements OnGatewayConnection, OnGatewayInit, OnGatewayDisconnect
{
constructor() {}
@WebSocketServer() server
@WebSocketServer() server: Server

afterInit() {
Logger.log('Websocket server initiated and ready to receive connections')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- CreateTable
CREATE TABLE "socket-io-attachments" (
"id" SERIAL NOT NULL,
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"payload" BYTEA NOT NULL,

CONSTRAINT "socket-io-attachments_pkey" PRIMARY KEY ("id")
);
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"@sendgrid/client": "7.6.2",
"@sendgrid/mail": "7.6.0",
"@sentry/node": "^7.46.0",
"@socket.io/postgres-adapter": "^0.3.1",
"@socket.io/postgres-emitter": "^0.1.0",
"@types/pg": "^8.6.6",
"@types/cron": "^2.0.0",
"aws-sdk": "2.1199.0",
"class-transformer": "0.5.1",
Expand All @@ -60,6 +63,7 @@
"nest-keycloak-connect": "^1.9.1",
"reflect-metadata": "0.1.13",
"rxjs": "7.5.5",
"socket.io": "latest",
"stripe": "11.14.0",
"swagger-ui-express": "4.5.0",
"tslib": "2.4.0"
Expand Down Expand Up @@ -99,6 +103,7 @@
"prettier": "2.6.0",
"prisma": "4.9.0",
"prisma-dbml-generator": "^0.10.0",
"socket.io-client": "^4.6.0",
"supertest": "^6.3.3",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
Expand Down
6 changes: 6 additions & 0 deletions podkrepi.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ Table bank_transactions_files {
personId String [not null]
}

Table SocketIoAttachments {
id Int [pk, increment]
createdAt DateTime [default: `now()`, not null]
payload Bytes [not null]
}

Enum BeneficiaryType {
individual
company
Expand Down
10 changes: 8 additions & 2 deletions schema.prisma
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generator client {
provider = "prisma-client-js"
binaryTargets = ["native"]
provider = "prisma-client-js"
binaryTargets = ["native"]
previewFeatures = ["orderByNulls"]
}

Expand Down Expand Up @@ -573,6 +573,12 @@ model BankTransactionsFile {
@@map("bank_transactions_files")
}

model SocketIoAttachments {
id Int @id @default(autoincrement())
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
payload Bytes
}

enum BeneficiaryType {
individual
company
Expand Down
Loading