sono.land

emit

emit

What does sono.emit do?

When you need to send a message to all clients that are currently connected to the server, emit method is used to execute that task. It takes in one argument, a message, to send to all clients that are currently connected via websocket through the channels list.

Client Side

1// Listening for requests coming into server
2for await (const req of server) {
3 // Serving an html file when client first connects to server
4 if (req.method === "GET" && req.url === "/") {
5 const path = `${Deno.cwd()}/index.html`
6 const content = await serveFile(req, path);
7 req.respond(content)
8 }
9 /* An example of how we can use emit once the client side sends the
10 initiation of a WebSocket handshake and the process is complete */
11 else if (req.method === "GET" && req.url === "/ws") {
12 sono.connect(req, ()=> {
13 sono.emit('new client connected')
14 });
15 }
Edit this page on GitHub