You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The idea is to allow the client to pass a callback to the server:
// Browser-side// We define a callback which will be called by the server!functioncallback(event){console.log('Message from the server: '+event.message);}// We pass `callback` to the server.server.subscribeEvents(callback);
// Server-sideserver.subscribeEvents=asyncfunction(callback){callback({message: 'Hello!'});awaitsleep({seconds: 3});callback({message: 'This is another message from the server.'});};
This enables all kinds of bi-directional communications, for example real-time chat, real-time gaming, live subscriptions, etc.
Proposed name: Wildcard callbacks.
The lifecycle can be managed thanks to two _onClose functions provided by Wildcard, as described in the following example.
Example - Live chat
// Browser-sideimport{server}from'@wildcard-api/client';main();asyncfunctionjoinChat(roomId){const{ leave }=awaitserver.joinChatRoom(roomId,// Our client-side defined `onNewMessage` is called by the server.onNewMessage,);print(`Joined chat room ${roomId}.`);// Leave chat room after 30 secondssetTimeout(()=>{leave();print(`Left chat room ${roomId}.`);},30*1000);}server._onClose=function(){// Browser is offline, or server is down.// All callbacks are now closed.print('Lost connection to server');}asyncfunctionmain(){constroomId=123;awaitjoinChat(roomId);};asyncfunctiononNewMessage(message){print(message);}functionprint(text){// (One should never do this; prone to JS injection.)document.body.innerHTML+=`<div>${text}</div>`;}
// Server-sideimport{server}from'@wildcard-api/server';constchatRooms=require('./path/to/chatRooms');server.joinChatRoom=asyncfunction(roomId,onNewMessage){constremoveListener=chatRooms[roomId].addListener(message=>{onNewMessage(message);});functionleave(){removeListener();}this._onClose=function(){// The connection to the client is lost// - The user closed his browser, or// - There is a network connection problemleave();};// We return `leave` to the client so that// the frontend can leave the chat room.return{ leave };};
The text was updated successfully, but these errors were encountered:
The idea is to allow the client to pass a callback to the server:
This enables all kinds of bi-directional communications, for example real-time chat, real-time gaming, live subscriptions, etc.
Proposed name: Wildcard callbacks.
The lifecycle can be managed thanks to two
_onClose
functions provided by Wildcard, as described in the following example.Example - Live chat
The text was updated successfully, but these errors were encountered: