• 30 favourites


Attached Files

The following files have been attached to this tutorial:



Download now 78.09 KB

Download now 355.61 KB


11,829 visits, 29,689 views



This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial. coding

        // This is required to run the server
        // here we define the port the messages are sended though.
        // in this case, it is port 8080, you can define it to every port you want.
        //but take note, some ports are already in use, then it will throw in an error message.
        var io = require("").listen(8080);
        //a variable for our "automatch making"
        //if a client connects to this, it performs this whole, big function.
        io.sockets.on("connection", function (socket) {
        	// when the client emits 'adduser', this listens and executes
        	socket.on("joint", function(data){
        		// checks, if no other waiting player exists,
        			//if yes, then it perform this
        			// it sais TmpRoom = data, that is the username that is data
        			/*That means, TmpRoom is not anymore = wait! so the next one that
        			wants to join here, go into the next loop, the "else" loop */
        			TmpRoom = data;
        			//then it creates, and joins a room, that is called after the users username
        			//Now it sends the message, you,playersname
        			socket.send("you," + TmpRoom);
        			// and now, it ends the loops.
        			//Joins the Temporary Room, to the enemy player
        			//Then it sends, the name of the room.
        			socket.send("Room," + TmpRoom);
        			//now, the interesting part. It not just sends a data to ONE client,
        			//but to all clients in the room. To all clients in the TmpRoom.
        			// the data that it sends, are the currently room name.
 "enemy," + TmpRoom);
        			//and the username from the other player.
 "enemy," + data);
        			//now it again sets the TmpRoom = wait, so the next that tries to join,
        			//again, is going to the upper loop, and creating its own room.
        			//Rooms are automatically "destroyed" when the Host, leaves the room.
        			TmpRoom = "wait";
        	//if the socket, gets a custom function from C2, called Message with some data(the room name),
        	//then it performs this:
        	socket.on("message", function(data){
        		//now it is in the loop, and if the server gets a event called Chatmessage,
        		//probably with a string message, then it performs this:
        		socket.on("chatmessage", function(data2){
        			//Sends a chat message in the room.
        			//it sends data to all clients in the room. that
        			//the sending client defined in his "message" function.
 "chatmessage," + data2);
        		//that is pretty much the same and don´t require any explanation
        		// i hope.
        		socket.on("GameSettings", function(data2){
        			//Sends the Game settings, x,y coordinate, String, everything!
 "GameSettings," + data2);
        	// when the user disconnects. perform this:
        	socket.on("disconnect", function(data){
        		//on disconnecting, it leaves the room.

Much code right?

The // are comments, and are used to set notes, like the / write it here/

I think this don´t require any further explanation. You can download the file on the left.

But the more javascript you know, the more possibilities you have,

This is only an example, and should not be used as a finished server.

But you could use it as an template how it maybe could be

Starting Server

Wondered how to start the server?

there exists multiple ways for it, im going to cover two of them

1. CMD

Open up CMD, type in:

cd C:\Your\Save\Directory\of\the\js\file

node yourappname.js

Now you started your server, a command window should show up now.

2. Batch

First you open up editor, and in the file you write:

node yourserverappname.js


yourserverappname needs to be your server app name, that you used to save the file as.

Then press Save As -> Filename: Start.bat -> Filetype: All files -> Save.

Thats it, put the .bat file in to your directory where you have your .js file and start the .bat .

Now a command windows should show up, and you started your server.

If you want to write specific things into this window, use

console.log("string" + "or" + avariable);


  • Order by
Want to leave a comment? Login or Register an account!