Preparing the environment for WebSocket Server in Node.js

At Edge Experience Hackathon, I had to write client and server-side code in Node.js for our game. I wasn’t pleased with the quality of code there, so I decided to rewrite it and show you how to implement WebSocket communication between server and Unity client.

A little disclaimer. I’m working on Mac, and I have a Terminal, so if you are on a Windows machine, you might have to use CMD, but I’m not promising that Terminal commands will stay the same for you.

This post also starts the series on Client-Server communication based on WebSockets. If you want to follow this series, make sure to join my newsletter!

Preparing your local environment. 👨‍🏭

The first things first, If you don’t have a Node.js installed on your machine, we will have to do it now! Otherwise, our JS code won’t work.

To download Node.js, you have to go to the Node.js homepage. At the time of writing this post, I had two options: 10.16.3 LTS (long term support) and 12.11.1 (latest version).

I decided to go with the 10.12.3 version, but it’s your choice.

After you install it you should be able to use node and npm commands in your Terminal.

We will be running them constantly, so let me explain what you need to know about them.

Terminal: node

This command is responsible for the runtime environment for our JavaScript code. We will use this command with scripts that we will create.

You can also check what version of the node you have installed by opening the Terminal and running this command.

$ node -v
The result of the command on my machine.

To run code, you will use another command.

$ node <scriptname.js>

This will open a script and execute the code within it.

You can find more details on that command in the Node.js documentation.

Terminal: npm

Another command is npm. This is package manager created for the Node.js developers, but currently it is used everywhere… 😅

It’s pretty useful as there are many packages that you can download and use in your project.

To check what version of npm you have, simply run this command:

$ npm -v
The result of the command on my machine.

You can learn more about it on the npm homepage.

Terminal: websocat

There is also another very helpful command that we will use a little later, which is websocat.

With this command, you can connect to the WebSocket server by using commands in Terminal. This will significantly speed up the process of developing your server-side.

To install that command, I would refer you to the installation section in the websocat repository.

As I’m on a Mac, I just had to run a single command to install it.

$ brew install websocat

If you are on a different system, it will be something else.

When you install it, you should be able to connect to the echo server using the command below:

$ websocat ws://echo.websocket.org

The echo server will return what you will send to it. Go ahead and check if it is working!

Communicating with the echo server.

Code editor 👨‍💻

The last thing that we need is a code editor.

There are many different lightweight code editors like Visual Studio Code, Atom, or Sublime.

All depends on your preferences but the editor of my choice is Atom.

What’s next? 🤔

In the next post, we will create a project for our server, and we will establish some basis connection thanks to the websocat.

If you got lost somewhere, let me know in the comment section below!

If you want to get notified on future content, sign up for the newsletter!

And I hope to see you next time! 🤓

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments