Custom generate PlayerID

How can i create a Unique four digit player-ID for a player connected to the server, i want to use this id to connect to the player from other device to control him.For now I’m using the socketID to connect but it’s a mess to enter the whole 20 digit i want to make this short so the user won’t get frustrated by typing the 20 digit ID.

4 digits mean that you can only have 10000 users on the server. The server should be responsible for giving the player the id as it would know which what IDs have been used already.

That’s fine cause this is a demo project , but is it possible to overwrite the socket ID that was given by the server, or is it completly a nightmare.

Yeah, changing the dictionary or method for socket ID generation would be troublesome. However, you don’t want to do that. They are generated using unique UUID and you want to keep them that way.

Since it is not related to Playcanvas, you would get better support on this question on Stackoverflow. Nevertheless, you have a couple of options.

If I understood your post correctly, you want a player to send some message to another player via a socket connection (presumably websocket). I assume you already have a server that facilitates those connections.

Option 1. That server, upon opening a new socket connection, can also generate a short id. For example, shortid if you use node. Then save both short id and the socket UUID as a pair in memory. The player then sends a message to a short id, the server receives the message, finds that short id and the socket UUID linked to it and sends the message to the socket id. This way a player can send a message to another player by using a custom id.

Option 2. Assuming you use websockets, then you have an option to use its rooms feature. When server sends a message to a room, every socket connected to it will receive the message. You can open a new room for each new socket, connect the socket to it and name the room with a custom short id. The player sends a message to some room with a short id, the server receives the message and broadcasts it to the specified room id. Since you have only one player connected per room, only the target player in that room will receive the message. This way a player can send a message to another player using a custom id.

1 Like

Okay thanks I’ll go with Option1 ,well i need to go through some documentation for short-id to know how it works,

  • How can i pair short-id with socket UUID.

Pairing is simple. Just keep them in a Map as key:value pairs, for example, where key is short id and value is the socket id.

// on init
var cache = new Map();

// on new socket
var idShort = 'generated-short-id';
var idSocket = 'read-from-new-socket-its-unique-long-uuid';
cache.set(idShort, idSocket);

// some player sends message to `generated-short-id`
var pairedSocket = cache.get('generated-short-id'); 
// pairedSocket  === 'read-from-new-socket-its-unique-long-uuid'

Is the short-id customizable or it’s just a short form of socket-id.

You can read the description in the package homepage. It has nothing to do with sockets or their ids. It simply generates random strings, that can be used as ids. And yes, the dictionary can be customized, as well as the length.

It worked but for every new connection the data in the map is overriding and when i try to connect a player through another tab it returns undefined

  • How can i store each player data in a map like an array without overriding the last connected player data.
    Example :i want all the connected player data like this,suppose three players are connected
    var playerIds = [

So how can i achieve this kind of format with maps

What data overrides map? It shouldn’t unless you have multiple server instances, or you create a new map on every new connection. Make sure the map is created on server initialization, not on a new socket connection.

Okay Let me try it

Oh yeah it worked, Thank’s man

Are u on Discord??

On what Game engine are u worked on except playcanvas.

What types are player1shortid and If both are strings, then it should work as I showed earlier:


Refer to Map documentation. It also has examples.

There is un-official discord server:

This is how my code looks like

var map = new Map();
io.sockets.on("connection", function(socket) {
   var mShortId;
  function randomString(length, chars) {
    var mask = "";
    if (chars.indexOf("a") > -1) mask += "abcdefghijklmnopqrstuvwxyz";
    if (chars.indexOf("A") > -1) mask += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if (chars.indexOf("#") > -1) mask += "0123456789";
    if (chars.indexOf("!") > -1) mask += "~`!@#$%^&*()_+-={}[]:\";'<>?,./|\\";
    var result = "";
    for (var i = length; i > 0; --i)
      result += mask[Math.floor(Math.random() * mask.length)];
    return result;
 mShortId = randomString(4, "aA#");   
  //Generating a Random String
   var mapData = map.set(mShortId,;
   console.log (mapData); 
  socket.emit("test", mShortId);  
  //This shortId will be displayed on when player chooses game view
  var movement;     
  socket.on("updateMove", function(data) {    
    movement = data;
  //decides which side the player is going to move 1 to right,-1 to left
  //Listen for the key from client
  socket.on("ListenforshortId", function(data) {
     console.log("shortID", data);  
    var clientID= map.get(data);
    console.log('connectionReqfromClient',clientID);"playerMoved", movement);

1 Like