-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Distributed Chat
Pomelo is a game framework, why the tutorial starts from chat?
Pomelo is really a game framework, but it is essentially a high real-time, scalable, multi-process application framework. In addition to the special part of the game library in the library section, the rest of the frame can be completely used for the development of real-time web application. And compared with some node.js high real-time application framework such as derby, socketstream, meteor etc, it has a better scalability.
Because of the complexity of the game in scene management, client Animation, they are not suitable entry level application for the pomelo. Chat application is usually the first application which developers contact with node.js, and therefore more suitable for the tutorial.
Generally the entry application of node.js is based on socket.io development of ordinary chat rooms. Because it is based on single-process node.js development, it hit a discount in scalability. For example, if you want to improve it to a multi-channels chat room like irc, the increase number of channels will inevitably lead the single-process node.js overloaded.
A native chat room application based socket.io, take [uberchat] (http://github.com/joshmarshall/uberchat ) for example.
Its application architecture diagram is as below:
The server which only contains a single node.js process receives requests from websocket.
It has following disadvantages:
-
Poor scalability: only support single process node.js, can not distribute according to room/channel, and can not separate broadcast pressure from logic business processing either.
-
Code redundancy: make simple encapsulation of socket.io, and only the server side contains 430 lines of code.
Using pomelo to write this framework can be completely overcome these shortcomings.
The distributed chat application architecture which we want to build is as follow:
In this architecture, the front-end servers named connector is responsible for holding connections, the chat server is in charge of processing business logic.
Such scale-up architecture has following advantages:
-
Load separation: The architecture totally separates the connection code from the business logic code, and this is really necessary especially in broadcast-intensive application like game and chat.Intensive broadcast and network communication consume large amount of resource, however, the business logic processing ability will not be influenced by broadcasting because of the separated architecture.
-
Simple switch: Users can switch channels or rooms that do not need to reconnect websocket because of this separated architecture.
-
Good scalability: We can launch more connector processes to deal with the increase of users, and use hash algorithm to map channels to different servers.
Below, we will start to build this application with pomelo, and we will find that it only needs less than 100 lines of code to build such a complex architecture.
Application of the code structure can be initialized by using the following command:
$:pomelo init chatofpomelo
The code structure is shown below:
Description:
-
game-server: all the game business logic code is in this directory.The file app.js is the entrance of the server, and all the game logic and functions start from here.
-
web-server: web server which used by game server(including login logic), the client js, css and static resources.
-
config: Generally, a project needs a lot of configurations and you can finish them by JSON files here. In game project, some configurations have be created such as log, master-server and other servers at initialization. Also, you can add database, map and numerical tabular configuration etc.
-
logs: Logs are essential for the project and contain a number of infomations which you can get the project runing state from.
-
shared: Both some configurations and code resources can be shared between front end and back end if you choose javascript to run client.
Initialization && Test:
install npm package
$:sh npm-install.sh
start game server
$:pomelo start
start web server
$:cd web-server && node app.js
The web server contains the pomelo client, when the web server is started, the client is automatically loaded into the browser. Clients send requests to the game server via websocket, the server can push messages to the client after connected by pomelo.
test
Enter http://localhost:3001, if the web server is running successfully, the following page will appear in the browser:
Click the Test Code button, the pop of 'hello, pomelo' prove the success of the client and server communication.
The logic of chat includes the following sections:
-
Entering: this part of the logic is responsible for registering user information to session, and add user into the channel of chat room.
-
Chatting: this section includes sending requests from the client, and receiving requests by the server.
-
Broadcasting: all clients in the same chat room receive and show messages.
-
Leaving: this section needs to do some clean-up work, including cleaning up the session and channel information.