Socket connect giving error 'WebSocket is not defined'


I am trying to build my Bot logic in Javascript using nakama-js.

While trying to connect socket to my Nakama server, i am getting error as ‘WebSocket is not defined’.

await nSocket.connect(nSession, appearOnline);   //Getting error here

My testing code is

const authenticate = async(deviceId) => {
    var client = new nakamaSDK.Client("defaultkey", IPAddress, port, false);
    client.timeout = 10000;
    var nSession = null;
    var nSocket = null;
    try {                
        nSession = await client.authenticateDevice(deviceId);
        nSocket = client.createSocket();
        var appearOnline = false;
        await nSocket.connect(nSession, appearOnline);        
    catch (err) {
        console.log("Error authenticating device: %o:%o", err.statusCode, err.message);

    return {
        'client': client,
        'session': nSession,
        'socket': nSocket


Do i need to import something else ?

Hey @HailMaverick the nakama-js library is intended for use in the browser.

For bot logic, you should use consider using our Typescript or Go server frameworks. You can find more information on our Typescript framework here:

This will give you better control of Nakama’s functionality by using before and after hooks and a match handler. This lends itself better to implementing bot logic.

Hey @lugehorsam , I am using Go runtime for my game server logic. My game is kind of party game where i need 100 people in the match. I want to create bots who can act like clients and play the game in the same manner as the real user. They should also do random chats in the game room.

Should we write the bots then along with Server module ?

Hey @HailMaverick yes I would use the Go match handler API in our server framework to handle bots rather than creating Nakama users for them, which would lead to an excessive amount of users in your database.

Using the match handler API, you can broadcast match state to real players with messages representing the bot’s actions. Each bot will implement the user presence interface and join/leave the match from within your Go match handler. You’ll implement a match loop function that handles the actual broadcasting of messages from the AI.

You can see our reference documentation for the match handler in Go here:

You can also find an example here of using an AI (in a 1v1 context) to perform bot behaviour. In particular, see match_handler.go and ai.go: