-
Notifications
You must be signed in to change notification settings - Fork 49
Websockets #1528
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
Merged
Merged
Websockets #1528
Changes from 7 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
5239843
add websockets for blocks and txs
stefangutica 6be90b6
add support for subscribe to stats
stefangutica 1442509
use websockets rooms
stefangutica 7d322ee
remove logs
stefangutica 62974c7
add lock on crons
stefangutica 8006d5e
check stats room exists
stefangutica d09f688
fix indent spaces
stefangutica 62fa109
add validation pipes + filters
stefangutica a7e71ea
add try catch + class validator fixes
stefangutica 58be392
fix linter
stefangutica cb92d2b
add pool subscription + reduce filters combinations for subscriptions
stefangutica 344330e
lint
stefangutica 404b18f
add support for events subscription
stefangutica d12208d
lint
stefangutica c0833a7
separate subscription websocket into separate app
stefangutica 4f9741d
add config
stefangutica 889a75b
add path
stefangutica cdfc711
fix
stefangutica 788284b
add path for events + config default settings
stefangutica 207ef83
temp logs
bogdan-rosianu 524fd9e
temp logs 2
bogdan-rosianu e644794
added missing configs + remove temp logs
bogdan-rosianu c5dc598
enable andromeda in config
stefangutica cad49bc
add metrics on subscription
stefangutica 8e92d63
remove async + reschedule
stefangutica 578303a
refresh metrics every second
stefangutica 6679b1f
set max listeners to 12
stefangutica 703a19e
Merge branch 'development' into websockets-poc
cfaur09 f6f747b
add EOL
cfaur09 d86907f
add count on update + parallel broadcast to rooms
stefangutica 5ceec0e
Merge branch 'development' into websockets-poc
bogdan-rosianu bdb82de
lower ttl for blocks count cache
stefangutica 341cbe3
remove comments
stefangutica e0c2295
remove comments
stefangutica a4dfb21
renaming
stefangutica File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,32 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { Cron } from '@nestjs/schedule'; | ||
| import { TransactionsGateway } from '../../endpoints/transactions/transaction.gateway'; | ||
| import { BlocksGateway } from 'src/endpoints/blocks/blocks.gateway'; | ||
| import { NetworkGateway } from 'src/endpoints/network/network.gateway'; | ||
| import { Lock } from "@multiversx/sdk-nestjs-common"; | ||
| @Injectable() | ||
| export class WebsocketCronService { | ||
| constructor( | ||
| private readonly transactionsGateway: TransactionsGateway, | ||
| private readonly blocksGateway: BlocksGateway, | ||
| private readonly networkGateway: NetworkGateway, | ||
| ) { } | ||
|
|
||
| @Cron('*/6 * * * * *') | ||
| @Lock({ name: 'Push transactions to subscribers', verbose: true }) | ||
| async handleTransactionsUpdate() { | ||
| await this.transactionsGateway.pushTransactions(); | ||
| } | ||
|
|
||
| @Cron('*/6 * * * * *') | ||
| @Lock({ name: 'Push blocks to subscribers', verbose: true }) | ||
| async handleBlocksUpdate() { | ||
| await this.blocksGateway.pushBlocks(); | ||
| } | ||
|
|
||
| @Cron('*/6 * * * * *') | ||
| @Lock({ name: 'Push stats to subscribers', verbose: true }) | ||
| async handleStatsUpdate() { | ||
| await this.networkGateway.pushStats(); | ||
| } | ||
| } |
This file contains hidden or 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,19 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { ScheduleModule } from '@nestjs/schedule'; | ||
| import { TransactionModule } from 'src/endpoints/transactions/transaction.module'; | ||
| import { WebsocketCronService } from './websocket.cron.service'; | ||
| import { BlockModule } from 'src/endpoints/blocks/block.module'; | ||
| import { NetworkModule } from 'src/endpoints/network/network.module'; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| ScheduleModule.forRoot(), | ||
| TransactionModule, | ||
| BlockModule, | ||
| NetworkModule, | ||
| ], | ||
| providers: [ | ||
| WebsocketCronService, | ||
| ], | ||
| }) | ||
| export class WebSocketCronModule { } |
This file contains hidden or 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 hidden or 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,48 @@ | ||
| import { WebSocketGateway, WebSocketServer, SubscribeMessage, OnGatewayDisconnect } from '@nestjs/websockets'; | ||
| import { Server, Socket } from 'socket.io'; | ||
| import { BlockService } from './block.service'; | ||
| import { BlockFilter } from './entities/block.filter'; | ||
| import { QueryPagination } from 'src/common/entities/query.pagination'; | ||
|
|
||
| @WebSocketGateway({ cors: { origin: '*' } }) | ||
| export class BlocksGateway implements OnGatewayDisconnect { | ||
| @WebSocketServer() | ||
| server!: Server; | ||
|
|
||
| constructor(private readonly blockService: BlockService) { } | ||
|
|
||
| @SubscribeMessage('subscribeBlocks') | ||
| async handleSubscription(client: Socket, payload: any) { | ||
| const filterHash = JSON.stringify(payload); | ||
| await client.join(`block-${filterHash}`); | ||
| } | ||
|
|
||
| async pushBlocks() { | ||
| for (const [roomName] of this.server.sockets.adapter.rooms) { | ||
| if (!roomName.startsWith("block-")) continue; | ||
|
|
||
| const filterHash = roomName.replace("block-", ""); | ||
| const filter = JSON.parse(filterHash); | ||
|
|
||
| const blockFilter = new BlockFilter({ | ||
| shard: filter.shard, | ||
| proposer: filter.proposer, | ||
| validator: filter.validator, | ||
| epoch: filter.epoch, | ||
| nonce: filter.nonce, | ||
| hashes: filter.hashes, | ||
| order: filter.order, | ||
| }); | ||
|
|
||
| const blocks = await this.blockService.getBlocks( | ||
| blockFilter, | ||
| new QueryPagination({ from: filter.from || 0, size: filter.size || 25 }), | ||
| filter.withProposerIdentity, | ||
| ); | ||
|
|
||
| this.server.to(roomName).emit('blocksUpdate', blocks); | ||
| } | ||
| } | ||
|
|
||
| handleDisconnect(_client: Socket) { } | ||
| } | ||
This file contains hidden or 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,25 @@ | ||
| import { WebSocketGateway, WebSocketServer, SubscribeMessage, OnGatewayDisconnect } from '@nestjs/websockets'; | ||
| import { Server, Socket } from 'socket.io'; | ||
| import { NetworkService } from './network.service'; | ||
|
|
||
| @WebSocketGateway({ cors: { origin: '*' } }) | ||
| export class NetworkGateway implements OnGatewayDisconnect { | ||
| @WebSocketServer() | ||
| server!: Server; | ||
|
|
||
| constructor(private readonly networkService: NetworkService) { } | ||
|
|
||
| @SubscribeMessage('subscribeStats') | ||
| async handleSubscription(client: Socket) { | ||
| await client.join('statsRoom'); | ||
| } | ||
|
|
||
| async pushStats() { | ||
| if (this.server.sockets.adapter.rooms.has('statsRoom')) { | ||
|
cfaur09 marked this conversation as resolved.
Outdated
|
||
| const stats = await this.networkService.getStats(); | ||
| this.server.to('statsRoom').emit('statsUpdate', stats); | ||
| } | ||
| } | ||
|
|
||
| handleDisconnect(_client: Socket) { } | ||
| } | ||
This file contains hidden or 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 hidden or 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,74 @@ | ||
| import { WebSocketGateway, WebSocketServer, SubscribeMessage, OnGatewayDisconnect } from '@nestjs/websockets'; | ||
| import { Server, Socket } from 'socket.io'; | ||
| import { TransactionService } from './transaction.service'; | ||
| import { TransactionFilter } from './entities/transaction.filter'; | ||
| import { QueryPagination } from 'src/common/entities/query.pagination'; | ||
| import { TransactionQueryOptions } from './entities/transactions.query.options'; | ||
|
|
||
| @WebSocketGateway({ cors: { origin: '*' } }) | ||
| export class TransactionsGateway implements OnGatewayDisconnect { | ||
| @WebSocketServer() | ||
| server!: Server; | ||
|
|
||
| constructor(private readonly transactionService: TransactionService) { } | ||
|
|
||
| @SubscribeMessage('subscribeTransactions') | ||
| async handleSubscription(client: Socket, payload: any) { | ||
| const filterHash = JSON.stringify(payload); | ||
| await client.join(`tx-${filterHash}`); | ||
| } | ||
|
|
||
| async pushTransactions() { | ||
| for (const [roomName] of this.server.sockets.adapter.rooms) { | ||
| if (!roomName.startsWith("tx-")) continue; | ||
|
|
||
| const filterHash = roomName.replace("tx-", ""); | ||
| const filter = JSON.parse(filterHash); | ||
|
|
||
| const options = TransactionQueryOptions.applyDefaultOptions(filter.size || 25, { | ||
| withScResults: filter.withScResults, | ||
| withOperations: filter.withOperations, | ||
| withLogs: filter.withLogs, | ||
| withScamInfo: filter.withScamInfo, | ||
| withUsername: filter.withUsername, | ||
| withBlockInfo: filter.withBlockInfo, | ||
| withActionTransferValue: filter.withActionTransferValue, | ||
| }); | ||
|
|
||
| const transactionFilter = new TransactionFilter({ | ||
| sender: filter.sender, | ||
| receivers: filter.receiver, | ||
| token: filter.token, | ||
| functions: filter.functions, | ||
| senderShard: filter.senderShard, | ||
| receiverShard: filter.receiverShard, | ||
| miniBlockHash: filter.miniBlockHash, | ||
| hashes: filter.hashes, | ||
| status: filter.status, | ||
| before: filter.before, | ||
| after: filter.after, | ||
| condition: filter.condition, | ||
| order: filter.order, | ||
| relayer: filter.relayer, | ||
| isRelayed: filter.isRelayed, | ||
| isScCall: filter.isScCall, | ||
| round: filter.round, | ||
| withRelayedScresults: filter.withRelayedScresults, | ||
| }); | ||
|
|
||
| TransactionFilter.validate(transactionFilter, filter.size || 25); | ||
|
|
||
| const txs = await this.transactionService.getTransactions( | ||
| transactionFilter, | ||
| new QueryPagination({ from: filter.from || 0, size: filter.size || 25 }), | ||
| options, | ||
| undefined, | ||
| filter.fields || [], | ||
| ); | ||
|
|
||
| this.server.to(roomName).emit('transactionUpdate', txs); | ||
| } | ||
| } | ||
|
|
||
| handleDisconnect(_client: Socket) { } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.