-
Notifications
You must be signed in to change notification settings - Fork 3
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 #4 from jovinjijo/feature-websockets
Realtime updates in UI
- Loading branch information
Showing
26 changed files
with
496 additions
and
73 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,7 +1,7 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
end_of_line = crlf | ||
insert_final_newline = true | ||
|
||
[*.{js,json,ts,tsx,.yml}] | ||
|
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,16 +1,21 @@ | ||
import app from './src/app'; | ||
import { SocketService } from './src/services/SocketService'; | ||
import { initMarket } from './src/util/Methods'; | ||
import { createServer } from 'http'; | ||
|
||
initMarket(); | ||
const server = createServer(app); | ||
const socketService = new SocketService(server); | ||
|
||
initMarket(socketService); | ||
|
||
/** | ||
* Start Express server. | ||
*/ | ||
const server = app.listen(app.get('port'), () => { | ||
server.listen(app.get('port'), () => { | ||
console.log(' App is running at http://localhost:%d in %s mode', app.get('port'), app.get('env')); | ||
console.log(' Press CTRL-C to stop\n'); | ||
}); | ||
|
||
export { server }; | ||
export { UserStoreItemDetails } from './src/models/User'; | ||
export { UserStoreItemDetails, UserDetails } from './src/models/User'; | ||
export { OrderStoreDetails } from './src/models/Order'; | ||
export { LtpUpdate, OrderUpdate } from './src/services/NotificationService'; |
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
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,36 @@ | ||
import { Amount, Notification, Order, Stock, User } from '@se/core'; | ||
import { OrderDetails, OrderRepository } from '../models/Order'; | ||
import { UserDetails, UserStore } from '../models/User'; | ||
import { SocketService } from './SocketService'; | ||
|
||
export interface LtpUpdate { | ||
stock: Stock; | ||
lastTradePrice: Amount; | ||
time: Date; | ||
} | ||
|
||
export interface OrderUpdate { | ||
user: UserDetails; | ||
order: OrderDetails; | ||
} | ||
|
||
export class NotificationService implements Notification { | ||
socketService: SocketService; | ||
constructor(socketService: SocketService) { | ||
this.socketService = socketService; | ||
} | ||
|
||
notifyLtpUpdate(stock: Stock, lastTradePrice: Amount, time: Date): void { | ||
this.socketService.broadcast('ltpUpdate', { stock, lastTradePrice, time }); | ||
} | ||
|
||
notifyOrderUpdate(user: User, order: Order): void { | ||
const socket = UserStore.findUserByUsername(user.name)?.socket; | ||
if (socket) { | ||
this.socketService.send(socket, 'orderUpdate', { | ||
user: UserStore.getUserDetails(user), | ||
order: OrderRepository.getOrderDetails(order), | ||
}); | ||
} | ||
} | ||
} |
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,27 @@ | ||
import { SocketServer } from '../util/SocketServer'; | ||
import { Server } from 'http'; | ||
import { Socket } from 'socket.io'; | ||
import { UserStore } from '../models/User'; | ||
import { Market } from '@se/core'; | ||
|
||
export class SocketService extends SocketServer { | ||
constructor(server: Server) { | ||
super(server, SocketService.onNewConnection); | ||
} | ||
|
||
static onNewConnection(socket: Socket): void { | ||
try { | ||
if (!socket.handshake.session) { | ||
throw new Error('User not logged in'); | ||
} else { | ||
const username = socket.handshake.session.userId; | ||
if (username) { | ||
UserStore.setSocketForUser(username, socket); | ||
socket.emit('ltpMap', Market.getInstance().getLtpForOrderStores()); | ||
} | ||
} | ||
} catch (ex) { | ||
socket.disconnect(); | ||
} | ||
} | ||
} |
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,9 +1,13 @@ | ||
import { Market, Stock } from '@se/core'; | ||
import { NotificationService } from '../services/NotificationService'; | ||
import { SocketService } from '../services/SocketService'; | ||
|
||
function initMarket(): void { | ||
function initMarket(socketService: SocketService): void { | ||
Market.getInstance().addOrderStore(Stock.TSLA, 500); | ||
Market.getInstance().addOrderStore(Stock.AMZN, 3000); | ||
Market.getInstance().addOrderStore(Stock.RIL, 2000); | ||
|
||
Market.getInstance().attachNotification(new NotificationService(socketService)); | ||
} | ||
|
||
export { initMarket }; |
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,24 @@ | ||
import { Server } from 'http'; | ||
import io, { Socket } from 'socket.io'; | ||
import sharedSession from 'express-socket.io-session'; | ||
import { session } from '../app'; | ||
|
||
export class SocketServer { | ||
private socket: SocketIO.Server; | ||
|
||
constructor(server: Server, onNewConnection?: (socket: Socket) => void) { | ||
this.socket = io(server); | ||
this.socket.use(sharedSession(session, { autoSave: true })); | ||
this.socket.on('connection', (socket: Socket) => { | ||
if (onNewConnection) onNewConnection(socket); | ||
}); | ||
} | ||
|
||
send(socket: Socket, type: string, data: unknown): void { | ||
socket.emit(type, data); | ||
} | ||
|
||
broadcast(type: string, data: unknown): void { | ||
this.socket.emit(type, data); | ||
} | ||
} |
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
Oops, something went wrong.