Sending data
Send data to a specific client
Send
You are able to send data to the client. The data can be any amount of variables of any type. When the data is sent, it will trigger the corresponding client-side MessageHandler.
Syntax
send(eventHandlerName: string, ...data: any[])
- Server Side
- Client Side
server.send(socket, "Example name", "myData", true);
OR
socket.send("Example name", "myData", true);
client.setMessageHandler("Example name", (data1: string, data2: boolean) => {
//Your code here...
});
Waiter
Waiters can be used to simply send data too, but it is not practical to do so.
Syntax
waiter<T>(socket: ZilaConnection, eventHandlerName: string, ...data: any[]) : Promise<T | undefined>
- Server Side
- Client Side
const response: string | undefined = await server.waiter<string>(socket, "Example name", "myData", true);
OR
const response: string | undefined = await socket.waiter<string>("Example name", "myData", true);
client.setMessageHandler("Example name", (data1: string, data2: boolean) => {
//Your code here...
return "This is message from the client side";
});
Broadcasting
Sometimes you need to send a message to all the clients. Don't worry, ZilaWS got you covered.
Send
Unlike waiters, this will return void and is not awaitable.
- Server Side
- Client Side
server.broadcastSend("Broadcast Example", "Broadcast data");
client.setMessageHandler("Broadcast Example", (data1: string) => {
//Your code here...
});
Waiter
You might use waiters, if you're expecting to receive back data or just want to make sure the client did what you asked it for.
- Server Side
- Client Side
const responses: Array<string | undefined> =
await server.broadcastWaiter<string>("Waiter Broadcast Example", "Broadcast data");
client.setMessageHandler("Waiter Broadcast Example", (data1: string) => {
//Your code here...
return "A message from the client side";
});