Skip to main content

Setup WebsocketServer Script

WebsocketServer Scripts are 24/7 running servers that receive events from the server in real-time. They can be used to create more robust logic, such as anti-cheat measures, and provide a more reliable experience for users. These scripts are ideal for developers who need more advanced functionality and want to build custom experiences that are not possible with Client Scripts or WebServer Scripts.

See the different types of scripts here.

Requirements

Setup WebsocketServer Script

1. Install SDK

Install the Verza SDK in your script:

npm install @verza/sdk

2. Obtain your Access Token

In order to connect your WebsocketServer Script, you need to obtain your server's Access Token.

To obtain your server's Access Token, go to its Server Settings and click on the Access Token tab. Then click the Generate Access Token button to generate a new token and copy it.

3. Create a server script:

Create a file called server.js.

server.js
import {initEngine} from '@verza/sdk';

const engine = await initEngine(
{
accessToken: 'ACCESS_TOKEN',
},
true, // set to true to enable websocket mode
);

// add custom event
engine.network.onPlayerEvent('onRequestWelcome', () => {
engine.localPlayer.sendMessage(`Message received! Welcome to the server!`);
});

console.log('Server started');

4. Start the server:

node server.js

Keep the server running

Sometimes, the server may crash or stop running, this can be due to a variety of reasons, such as a bug in the script or a server restart, to prevent this from happening, you can use a process manager to keep the server running when it crashes or stops.

You can use a process manager like Forever or PM2.

Using Forever

npm install -g forever

# Start the server
forever start server.js

# Stop the server
forever stop server.js

Using PM2

npm install -g pm2

# Start the server
pm2 start server.js

# Stop the server
pm2 stop server.js

With these steps, you have set up a basic WebsocketServer Script using the Verza SDK.

Host your Script

To host your WebsocketServer Script, you need to deploy it to a server to keep it running 24/7. You can use any server provider you want, such as DigitalOcean, AWS, Heroku, etc.