Skip to main content
Version: Next

Recieving data

There are two ways to reciveve data from the server. With ZilaWS data can be asked and waited for.

Asking for data

To ask the server for data, the waiter function can be used. This will trigger a server side MessageHandler and get its return value.

const client = new ZilaConnection();

await client.connectTo("wss://yourhost.com");

console.log(await client.waiter("GetValueOfPI") as number); // --> 3.141592653589793

Waiting for data

MessageHandlers can be registered on client side too. They run when the client recives a message from the server with the event's identifier. The params contain the data. There can be any number of params with any types.

const client = new ZilaConnection();

client.setMessageHandler("SomeIdentifier", (param1: string, param2: number, param3: object /*, ...*/) => {
//Your code
});

await client.connectTo("wss://yourhost.com");

OR

You can use a onceMessageHandler to register a MessageHandler that only gets executed once.

const client = new ZilaConnection();

client.onceMessageHandler("SomeIdentifier", (param1: string, param2: number, param3: object /*, ...*/) => {
//Your code
});

await client.connectTo("wss://yourhost.com");

If the server uses the waiter function to send the data down, you can even make the server wait until it is done.

const client = new ZilaConnection();

client.setMessageHandler("SomeIdentifier", async (param1: string, param2: number, param3: object /*, ...*/) => {
await new Promise<void>((resolve) =>{
//Your Code is represented by this timeout
setTimeout(() => {
resolve();
}, 3000);
});
});

await client.connectTo("wss://yourhost.com");