Make a multiplayer card game - Episode 1 | Create a simple server and client from scratch

In the coming weeks, I'll be updating a series of tutorials on making a multiplayer online card games base on the rule of the most popular Chinese card game “Doudizhu”, which means battle with landlord. I divide this series in many small part to make it easy for most friends. If you know a little javascript, all the better.

Parts including:

  • create a simple server and client
  • play cards in terminal with bots
  • communicate with server by protobuf
  • play cards in terminal with other players
  • create 2D graphical interface with Cocos Creator
  • create 2D graphical interface with React
  • create 3D graphical interface with Three.js
  • add physical lib ammo.js to add a tiny game
  • connect to Web3 world

open your terminal, input node -v, if something go wrong, please install node.js first

directory structure : demo0-document-tree.jpg

create a server, server/index.js:

 const net = require('net');

const server = net.createServer((socket) => {
    socket.on('data', (data) => {
        handlerData(socket, data)

function handlerData(socket, data) {
    send(socket, {'msg':'Welcome, friend.'});

function send(socket, data) {
    let bufferData = Buffer.from(JSON.stringify(data));

server.listen(8080, () => {
    console.log("server listening on")

create client, client/index.js:

const net = require('net');
const socket = new net.Socket();

    host: '',
    port: 8080
}, onConnected);

socket.on('data', (buffer) => {
socket.on('error', (buffer) => {

function onConnected() {

function decode(data) {

function request(data) {
    const bufferData = Buffer.from(JSON.stringify({ 'msg': data }));

function startGame() {
    request('Hello, world!')

run :

cd server

node index.js

cd client

node index.js



This episode is end here, the complete demo can be found on

Any problems, discussion is welcome.

In the next section, we will add the logic of Doudizhu game, and you will have completed a playable game in your terminal.

Thanks for your reading.

Did you find this article valuable?

Support Lizhiyu by becoming a sponsor. Any amount is appreciated!