method

ServerWebSocket.cork

cork<R = unknown>(
callback: (ws: ServerWebSocket<T>) => R
): R;

Batches send() and publish() operations, which makes it faster to send data.

The message, open, and drain callbacks are automatically corked, so you only need to call this if you are sending messages outside of those callbacks or in async functions.

@param callback

The callback to run. It runs even if the socket is closed.

@returns

The return value of callback.

ws.cork((ctx) => {
  ctx.send("These messages");
  ctx.sendText("are sent");
  ctx.sendBinary(new TextEncoder().encode("together!"));
});