Frameworks, tooling and other drugs (II)

I wrote a first post recently describing how I started with Phaser and Webpack. That would be the Client side in my game idea of a #browser #multiplayer #RPG. Now let's see the needed Server side:

Node & Websockets
I decided to start with Node.js as it offers speed, scalability, a non-blocking API and continue with the Javascript stack. It has an easy and fast learning curve for anyone familiar to Javascipt and it's a very good and simple start. There are a lot of simple HTTP server examples made in Node but what we need for our game are Websockets.

A game server needs rapid request/response communication. The fastest way would be using UDP network protocol, but as UDP doesn't guarantee an order in responses it is needed a TCP. Websockets are the fastest TCP approach. Looking for a good library to work with Node and Websockets I found ws and I liked it.

See a very basic server example:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 1234 });
wss.on('connection', function connection(ws) {
  console.log('new client connected');
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  ws.send('text from server');

And a very basic browser client to complete the comunication:

wsc = new WebSocket('ws://localhost:1234');
wsc.onopen = () => console.log('connected to server');
wsc.onmessage = (message) => console.log('received: %s', message);
wsc.send('text from client');

Now you can see how easy is to evolve these basic examples in a simple chat or just for sending/receiving players positions. A good pair of hints for the simple server example would be:
1) push and keep client connections (players) in array data
2) create a broadcast function in order to send data for other clients

Posted on 6th Nov, 2018 by applicant42

This article is my 3rd oldest. It is 284 words long