-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from infinitybase/FEAT/socket-server
Feat/socket server
- Loading branch information
Showing
5 changed files
with
155 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
|
||
NODE_ENV=test | ||
|
||
# DATABASE | ||
DATABASE_HOST = 127.0.0.1 | ||
DATABASE_PORT = 5432 | ||
DATABASE_USERNAME = postgres | ||
DATABASE_PASSWORD = postgres | ||
DATABASE_NAME = postgres | ||
DATABASE_PORT_TEST = 5432 | ||
# APP | ||
API_PORT=3333 | ||
APP_NAME=bsafe-api | ||
# ADMIN USER | ||
APP_ADMIN_EMAIL=admin_user_email | ||
APP_ADMIN_PASSWORD=admin_user_password | ||
# TOKENS | ||
ACCESS_TOKEN_SECRET=access_token_secret | ||
REFRESH_TOKEN_SECRET=refresh_token_secret | ||
# AWS | ||
UI_URL=https://app.bsafe.pro | ||
|
||
# App | ||
API_PORT = 3333 | ||
|
||
# Admin user | ||
APP_ADMIN_EMAIL = [email protected] | ||
APP_ADMIN_PASSWORD = admin_user_password | ||
|
||
# Tokens | ||
ACCESS_TOKEN_SECRET = access_token_secret | ||
REFRESH_TOKEN_SECRET = refresh_token_secret | ||
# Time should be in minutes | ||
TOKEN_EXPIRATION_TIME = 15 | ||
#UI_URL=http://localhost:5173 | ||
UI_URL=https://app.bsafe.pro | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
DATABASE_HOST=127.0.0.1 | ||
DATABASE_PORT=5432 | ||
DATABASE_USERNAME=postgres | ||
DATABASE_PASSWORD=postgres | ||
DATABASE_NAME=postgres | ||
NODE_ENV=development | ||
# Database | ||
DATABASE_HOST = 127.0.0.1 | ||
DATABASE_PORT = 5432 | ||
DATABASE_USERNAME = postgres | ||
DATABASE_PASSWORD = postgres | ||
DATABASE_NAME = postgres | ||
|
||
# App | ||
API_PORT = 3333 | ||
|
||
# Admin user | ||
APP_ADMIN_EMAIL = [email protected] | ||
APP_ADMIN_PASSWORD = admin_user_password | ||
|
||
# Tokens | ||
ACCESS_TOKEN_SECRET = access_token_secret | ||
REFRESH_TOKEN_SECRET = refresh_token_secret | ||
# Time should be in minutes | ||
TOKEN_EXPIRATION_TIME = 15 | ||
UI_URL=http://localhost:5173 | ||
# UI_URL=https://app.bsafe.pro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Server, ServerOptions } from 'socket.io'; | ||
|
||
export interface ISocketUser { | ||
userID: string; | ||
username: string; | ||
} | ||
|
||
export enum SocketChannels { | ||
WALLET = '[WALLET]', | ||
POPUP_AUTH = '[POPUP_AUTH]', | ||
POPUP_TRANSFER = '[POPUP_TRANSFER]', | ||
} | ||
|
||
export interface ISocketEvent { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
content: any; // todo: typing all events, and useing or | ||
to: string; | ||
} | ||
|
||
class SocketIOServer extends Server { | ||
//todo: upgrade this type | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
private io: any; | ||
|
||
constructor(httpServer: Express.Application, options?: Partial<ServerOptions>) { | ||
super(httpServer); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
this.io = new Server(httpServer, { | ||
cors: { | ||
origin: '*', | ||
methods: ['GET', 'POST'], | ||
}, | ||
...options, | ||
}); | ||
|
||
//middleware de conexao | ||
this.io.use((socket, next) => { | ||
const username = socket.handshake.auth.username; | ||
const hasValidEvent = | ||
username.includes(SocketChannels.WALLET) || | ||
username.includes(SocketChannels.POPUP_AUTH) || | ||
username.includes(SocketChannels.POPUP_TRANSFER); | ||
if (!username || !hasValidEvent) { | ||
return next(new Error('invalid username')); | ||
} | ||
|
||
console.log('[user_connectted]:', username); | ||
socket.username = username; | ||
next(); | ||
}); | ||
|
||
this.io.on('connection', socket => { | ||
//[to list all users] | ||
// const users: ISocketUser[] = []; | ||
// for (const [id, socket] of this.io.of('/').sockets) { | ||
// users.push({ userID: id, username: socket.username }); | ||
// } | ||
|
||
/* | ||
[WALLET] | ||
- complement this connection depends to event content | ||
- for exemple, complement payload to message to send to client | ||
*/ | ||
socket.on('[WALLET]', ({ content, to }: ISocketEvent) => { | ||
console.log('[WALLET]'); | ||
socket.to(to).emit('[WALLET]', { | ||
content, | ||
from: socket.id, | ||
}); | ||
}); | ||
|
||
/* | ||
[POPUP_TRANSFER] | ||
- complement this connection depends to event content | ||
- for exemple, complement payload to message to send to client | ||
*/ | ||
socket.on('[POPUP_TRANSFER]', ({ content, to }: ISocketEvent) => { | ||
console.log('[POPUP_TRANSFER]'); | ||
socket.to(to).emit('[POPUP_TRANSFER]', { | ||
content, | ||
from: socket.id, | ||
}); | ||
}); | ||
|
||
/* | ||
[POPUP_AUTH] | ||
- complement this connection depends to event content | ||
- for exemple, complement payload to message to send to client | ||
*/ | ||
socket.on('[POPUP_AUTH]', ({ content, to }: ISocketEvent) => { | ||
console.log('[POPUP_AUTH]'); | ||
socket.to(to).emit('[POPUP_AUTH]', { | ||
content, | ||
from: socket.id, | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default SocketIOServer; |