Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real-time/bi-directional communication (Wildcard callbacks) #61

Open
brillout opened this issue Oct 14, 2020 · 0 comments
Open

Real-time/bi-directional communication (Wildcard callbacks) #61

brillout opened this issue Oct 14, 2020 · 0 comments
Labels
enhancement New feature or request vision The future

Comments

@brillout
Copy link
Owner

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!
function callback(event) {
  console.log('Message from the server: '+event.message);
}

// We pass `callback` to the server.
server.subscribeEvents(callback);
// Server-side

server.subscribeEvents = async function(callback) {
  callback({message: 'Hello!'});
  await sleep({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-side

import { server } from '@wildcard-api/client';

main();

async function joinChat(roomId) {
  const { leave } = await server.joinChatRoom(
    roomId,
    // Our client-side defined `onNewMessage` is called by the server.
    onNewMessage,
  );

  print(`Joined chat room ${roomId}.`);

  // Leave chat room after 30 seconds
  setTimeout(
    () => {
      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');
}

async function main() {
  const roomId = 123;
  await joinChat(roomId);
};

async function onNewMessage(message) {
  print(message);
}

function print(text) {
   // (One should never do this; prone to JS injection.)
   document.body.innerHTML += `<div>${text}</div>`;
}
// Server-side

import { server } from '@wildcard-api/server';
const chatRooms = require('./path/to/chatRooms');

server.joinChatRoom = async function(roomId, onNewMessage) {
  const removeListener = chatRooms[roomId].addListener(message => {
    onNewMessage(message);
  });

  function leave() {
    removeListener();
  }

  this._onClose = function() {
    // The connection to the client is lost
    //  - The user closed his browser, or
    //  - There is a network connection problem
    leave();
  };

  // We return `leave` to the client so that
  // the frontend can leave the chat room.
  return { leave };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request vision The future
Projects
None yet
Development

No branches or pull requests

1 participant