PromucFlow_constructor/app/client/packages/rts/src/sockets/events.ts

79 lines
2.6 KiB
TypeScript
Raw Normal View History

import type { Server, Socket } from "socket.io";
fix: revamped rts server architecture (#15870) ## Description RTS node server was being used for Availability feature. Now we will be enhancing the server to utilise client side logic on the server side side like construction of AST, Evaluations logic etc. So, since our codebase was not structured for implementing such use case, so we had to revamp the structure of the codebase. #### Architecture - **Routes :** To define different paths to consume apis - **Controllers :** To capture the routing and formulate the logic to provide response to a particular request - **Middleware :** action functions that happen to be stitched in between the routes and controllers, or can be used as a part of logic that needs to be injected in between. - **Constants :** To keep static variables as per the usage without declaring it in other parts of the codebase - **Sockets :** Any socket apis exists inside this folder to properly structure every socket implementations - **Utils :** Helper functions which can be used at any stage of the application Fixes #15645 ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. - Test A - Test B ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-08-11 09:36:02 +00:00
import log from "loglevel";
import {
APP_ROOM_PREFIX,
RELEASE_VERSION_EVENT_NAME,
LEAVE_EDIT_EVENT_NAME,
PAGE_EDIT_NAMESPACE,
PAGE_ROOM_PREFIX,
ROOT_NAMESPACE,
PAGE_VISIBILITY_EVENT_NAME,
} from "@constants/socket";
import { VERSION as buildVersion } from "../version";
fix: revamped rts server architecture (#15870) ## Description RTS node server was being used for Availability feature. Now we will be enhancing the server to utilise client side logic on the server side side like construction of AST, Evaluations logic etc. So, since our codebase was not structured for implementing such use case, so we had to revamp the structure of the codebase. #### Architecture - **Routes :** To define different paths to consume apis - **Controllers :** To capture the routing and formulate the logic to provide response to a particular request - **Middleware :** action functions that happen to be stitched in between the routes and controllers, or can be used as a part of logic that needs to be injected in between. - **Constants :** To keep static variables as per the usage without declaring it in other parts of the codebase - **Sockets :** Any socket apis exists inside this folder to properly structure every socket implementations - **Utils :** Helper functions which can be used at any stage of the application Fixes #15645 ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. - Test A - Test B ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-08-11 09:36:02 +00:00
import {
subscribeToEditEvents,
onAppSocketConnected,
onPageSocketConnected,
sendCurrentUsers,
} from "@controllers/socket";
fix: revamped rts server architecture (#15870) ## Description RTS node server was being used for Availability feature. Now we will be enhancing the server to utilise client side logic on the server side side like construction of AST, Evaluations logic etc. So, since our codebase was not structured for implementing such use case, so we had to revamp the structure of the codebase. #### Architecture - **Routes :** To define different paths to consume apis - **Controllers :** To capture the routing and formulate the logic to provide response to a particular request - **Middleware :** action functions that happen to be stitched in between the routes and controllers, or can be used as a part of logic that needs to be injected in between. - **Constants :** To keep static variables as per the usage without declaring it in other parts of the codebase - **Sockets :** Any socket apis exists inside this folder to properly structure every socket implementations - **Utils :** Helper functions which can be used at any stage of the application Fixes #15645 ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. - Test A - Test B ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-08-11 09:36:02 +00:00
export function watchEvents(io: Server) {
io.on("connection", (socket: Socket) => {
socket.emit(RELEASE_VERSION_EVENT_NAME, buildVersion);
subscribeToEditEvents(socket, APP_ROOM_PREFIX);
onAppSocketConnected(socket).catch((error) =>
log.error("Error in socket connected handler", error),
fix: revamped rts server architecture (#15870) ## Description RTS node server was being used for Availability feature. Now we will be enhancing the server to utilise client side logic on the server side side like construction of AST, Evaluations logic etc. So, since our codebase was not structured for implementing such use case, so we had to revamp the structure of the codebase. #### Architecture - **Routes :** To define different paths to consume apis - **Controllers :** To capture the routing and formulate the logic to provide response to a particular request - **Middleware :** action functions that happen to be stitched in between the routes and controllers, or can be used as a part of logic that needs to be injected in between. - **Constants :** To keep static variables as per the usage without declaring it in other parts of the codebase - **Sockets :** Any socket apis exists inside this folder to properly structure every socket implementations - **Utils :** Helper functions which can be used at any stage of the application Fixes #15645 ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. - Test A - Test B ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-08-11 09:36:02 +00:00
);
});
/** When we get the page visibility event, it means the page/tab
* is visible on the client after navigating away from it.
* We will respond back with the current version to
* so that the client can confirm if they are
* on the latest version of the client
*/
io.on(PAGE_VISIBILITY_EVENT_NAME, (socket: Socket) => {
socket.emit(RELEASE_VERSION_EVENT_NAME, buildVersion);
});
fix: revamped rts server architecture (#15870) ## Description RTS node server was being used for Availability feature. Now we will be enhancing the server to utilise client side logic on the server side side like construction of AST, Evaluations logic etc. So, since our codebase was not structured for implementing such use case, so we had to revamp the structure of the codebase. #### Architecture - **Routes :** To define different paths to consume apis - **Controllers :** To capture the routing and formulate the logic to provide response to a particular request - **Middleware :** action functions that happen to be stitched in between the routes and controllers, or can be used as a part of logic that needs to be injected in between. - **Constants :** To keep static variables as per the usage without declaring it in other parts of the codebase - **Sockets :** Any socket apis exists inside this folder to properly structure every socket implementations - **Utils :** Helper functions which can be used at any stage of the application Fixes #15645 ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. - Test A - Test B ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-08-11 09:36:02 +00:00
io.of(PAGE_EDIT_NAMESPACE).on("connection", (socket: Socket) => {
subscribeToEditEvents(socket, PAGE_ROOM_PREFIX);
onPageSocketConnected(socket, io).catch((error) =>
log.error("Error in socket connected handler", error),
fix: revamped rts server architecture (#15870) ## Description RTS node server was being used for Availability feature. Now we will be enhancing the server to utilise client side logic on the server side side like construction of AST, Evaluations logic etc. So, since our codebase was not structured for implementing such use case, so we had to revamp the structure of the codebase. #### Architecture - **Routes :** To define different paths to consume apis - **Controllers :** To capture the routing and formulate the logic to provide response to a particular request - **Middleware :** action functions that happen to be stitched in between the routes and controllers, or can be used as a part of logic that needs to be injected in between. - **Constants :** To keep static variables as per the usage without declaring it in other parts of the codebase - **Sockets :** Any socket apis exists inside this folder to properly structure every socket implementations - **Utils :** Helper functions which can be used at any stage of the application Fixes #15645 ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. - Test A - Test B ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-08-11 09:36:02 +00:00
);
});
io.of(ROOT_NAMESPACE).adapter.on("leave-room", (room, id) => {
if (room.startsWith(APP_ROOM_PREFIX)) {
log.debug(`ns:${ROOT_NAMESPACE}# socket ${id} left the room ${room}`);
}
sendCurrentUsers(io, room, APP_ROOM_PREFIX);
});
io.of(ROOT_NAMESPACE).adapter.on("join-room", (room, id) => {
if (room.startsWith(APP_ROOM_PREFIX)) {
log.debug(`ns:${ROOT_NAMESPACE}# socket ${id} joined the room ${room}`);
}
sendCurrentUsers(io, room, APP_ROOM_PREFIX);
});
io.of(PAGE_EDIT_NAMESPACE).adapter.on("leave-room", (room, id) => {
if (room.startsWith(PAGE_ROOM_PREFIX)) {
// someone left the page edit, notify others
log.debug(
`ns:${PAGE_EDIT_NAMESPACE} # socket ${id} left the room ${room}`,
fix: revamped rts server architecture (#15870) ## Description RTS node server was being used for Availability feature. Now we will be enhancing the server to utilise client side logic on the server side side like construction of AST, Evaluations logic etc. So, since our codebase was not structured for implementing such use case, so we had to revamp the structure of the codebase. #### Architecture - **Routes :** To define different paths to consume apis - **Controllers :** To capture the routing and formulate the logic to provide response to a particular request - **Middleware :** action functions that happen to be stitched in between the routes and controllers, or can be used as a part of logic that needs to be injected in between. - **Constants :** To keep static variables as per the usage without declaring it in other parts of the codebase - **Sockets :** Any socket apis exists inside this folder to properly structure every socket implementations - **Utils :** Helper functions which can be used at any stage of the application Fixes #15645 ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. - Test A - Test B ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-08-11 09:36:02 +00:00
);
io.of(PAGE_EDIT_NAMESPACE).to(room).emit(LEAVE_EDIT_EVENT_NAME, id);
fix: revamped rts server architecture (#15870) ## Description RTS node server was being used for Availability feature. Now we will be enhancing the server to utilise client side logic on the server side side like construction of AST, Evaluations logic etc. So, since our codebase was not structured for implementing such use case, so we had to revamp the structure of the codebase. #### Architecture - **Routes :** To define different paths to consume apis - **Controllers :** To capture the routing and formulate the logic to provide response to a particular request - **Middleware :** action functions that happen to be stitched in between the routes and controllers, or can be used as a part of logic that needs to be injected in between. - **Constants :** To keep static variables as per the usage without declaring it in other parts of the codebase - **Sockets :** Any socket apis exists inside this folder to properly structure every socket implementations - **Utils :** Helper functions which can be used at any stage of the application Fixes #15645 ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. - Test A - Test B ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-08-11 09:36:02 +00:00
}
sendCurrentUsers(io.of(PAGE_EDIT_NAMESPACE), room, PAGE_ROOM_PREFIX);
});
io.of(PAGE_EDIT_NAMESPACE).adapter.on("join-room", (room, id) => {
if (room.startsWith(PAGE_ROOM_PREFIX)) {
log.debug(
`ns:${PAGE_EDIT_NAMESPACE}# socket ${id} joined the room ${room}`,
fix: revamped rts server architecture (#15870) ## Description RTS node server was being used for Availability feature. Now we will be enhancing the server to utilise client side logic on the server side side like construction of AST, Evaluations logic etc. So, since our codebase was not structured for implementing such use case, so we had to revamp the structure of the codebase. #### Architecture - **Routes :** To define different paths to consume apis - **Controllers :** To capture the routing and formulate the logic to provide response to a particular request - **Middleware :** action functions that happen to be stitched in between the routes and controllers, or can be used as a part of logic that needs to be injected in between. - **Constants :** To keep static variables as per the usage without declaring it in other parts of the codebase - **Sockets :** Any socket apis exists inside this folder to properly structure every socket implementations - **Utils :** Helper functions which can be used at any stage of the application Fixes #15645 ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. - Test A - Test B ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-08-11 09:36:02 +00:00
);
}
sendCurrentUsers(io.of(PAGE_EDIT_NAMESPACE), room, PAGE_ROOM_PREFIX);
});
}