# Node-RpcDream **Repository Path**: dream-lab/Node-RpcDream ## Basic Information - **Project Name**: Node-RpcDream - **Description**: No description available - **Primary Language**: TypeScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-01-11 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Rpc dream realtime communication framework Rpc dream is a RPC (remote procedure call) framework written with TypeScript it is designing and developing for bi-directional realtime communication requirements by our software company. #### advantages" - **Easy of use** and very simple, we can call a remote rpc function as a local function - **no more complex dependency** - **fully bi-directional communication support** - **no callback, no string style events**, all rpc call is asynchronous style with Promise + async/await keyword ,so we can handle errors using standard try-catch statements. - **extensible design** - **platform independent**, support multiple environments, compatible with standard JavaScript(and TypeScript like Deno) runtime and other javascript environments (node.js, all the browsers that supported ES6 features, wechat mini program/mini game) - **good idea** of architecture design, programmers friendly, easy of understand. #### disadvantages: - **Not stable**, in development progress, not ready for production at this moment (but this framework is internally using to our company projects) --- ## Basic concepts this framework consists these basic concepts: - Transport layer (Abstraction layer) - Server implementation - Client implementation - Rpc definitions - Rpc server - Rpc client session - server side rpc implementation object - state - bridge (bridge object between server and client) - Rpc client - client side rpc implementation object - bridge (bridge object between server and client) --- **1. Transport layer (abstraction layer)** rpc server and rpc client itself has no any communication feature, they are created top of the transport layer, rpc client and rpc server are highly dependent on the transport layer. transport layer is the real communication layer that implemented communication features using sockets like tcp socket, web socket etc. transport layers are provide real communication(messaging) feature to rpc servers and rpc clients. Transport layer split into 2 types: server implementation and client implementation (we are not discuss this at this moment.) we can create our own transport layer using another messaging mechanism (e.g. udp, tcp ...) or using an already existing transport layer, for example, we need to encrypt all the data using some methods while sending and decrypt these data while receiving in another side, these case we not need to encode data before sending and decode data after receiving manually, this case, we can create our custom secure transport layer, implement our encrypt/decrypt process and pass encrypt/decrypt results to an existing transport layer. **2. Rpc definition** First of all, we need to define two interfaces used by Rpc server and Rpc client, these interface describes rpc methods in the server side and client side, actually these interface files not exists at the runtime, but it is very useful when during development, code editors/ide like vs code, web storm is can be help us to provide auto completion features using these interface. for example, a simple interface of a chatting app are looks like this: ``` export interface IServerInterface { join(person: Person): Promise leave(id: number): Promise sendMessage(content: {personId: number, content: string}): Promise } export interface IClientInterface { onJoinPerson(details: {person: Person, time: Date}): Promise onLeavePerson(details: {person: Person, time: Date}): Promise onMessage(details: {senderId: number, content: string}): Promise } ``` above is a simple chatting app rpc definition, in above code we used a class called "Person" for describe person of the chatting app ``` export class Person { constructor(public id: number, public name: string, public email: string, public address: string) { } } ``` above code are very simple, "IClientInterface" and "IServerInterface" are the **"Rpc definitions"** we are mentioned. **3. Rpc Server** like http server, this framework has rpc server concept that responsible to handle rpc client request, manage clients, handle clients rpc call or call rpc methods in any of client. rpc server itself has no any communication feature, it is created top of transport layer, a rpc server has one or more rpc client connections, rpc server can be invoke any rpc method of any clients at any time(requires this rpc client is connected to this rpc server), rpc server saves all rpc client list itself. when a rpc client connect to rpc server then rpc server accept client request and will create a client session object, client state object and server side rpc implementation object. - **rpc client session** is represents the rpc client in the server, rpc client session is created by rpc server, any client session has it's own state managed by rpc server. - **rpc client session state**, this is created by rpc client session object, this is a simple object that managed by rpc server and used to save client session state when the rpc client is disconnect and restores the saved state if the client is reconnect after some time, for example: this is very useful when we make a game server, when a gamer loss it's connection then server save the gamer's score and restores the score when the gamer recovers it's connection. - **rpc implementation object**, this object is implement server side **"Rpc Definition"** interface, this object is created by rpc client session, this object is responsible for handle rpc client's call request and return responds, this is corresponds **"Controller"** of MVC architecture on web development. this object can be complex tree like hirerchial sub object. - **rpc bridge** is the core object while communicating with remote side, this is automatically created by inner of rpc server. **4. Rpc client** also Rpc client has no any communication feature, it's required a transport layer, if rpc client needs sending or receiving message between itself and server, it's invokes it's transport layer specific methods, and rpc client has client side rpc implementation object that responsible to handle server side rpc request and return response, this is correspond **"Controller"** in the http server MVC architectures, (client side controller or call request handler.)**(Remember this, this framework is full support bidirectional rpc call)**