@cryb/portals — VM microservice
@cryb/portals is the microservice used to handle requests @cryb/api to create and destroy 'Portals', which is the term we use for VM instances.
@cryb/portal instances also connect to @cryb/portals over WS to send and recieve updates like controller events and health updates.
@cryb/portals has been actively developed internally since September 2019, and is now open source as of October 2019.
The codebase for @cryb/portals is written in JavaScript, utilising TypeScript and Node.js. Express.js is used for our REST API, while the WebSocket API uses the ws module.
MongoDB is used as the primary database, while Redis is used for cache and PUB/SUB.
We ask that you follow our code style guidelines when contributing to this repository.
We use TSLint in order to lint our code. Run yarn lint before committing any code to ensure it's clean.
Note: While we have most rules covered in our tslint.json config, it's good practice to familarise yourself with our code style guidelines.
cryb/portals/
└──┐ src # The core source code
├──┐ config # Config files for Redis, Passport, etc
│ └── providers # Config files for the provider APIs, such as Google Cloud, Kubernetes, etc.
├── controllers # Our REST route controller files
├── drivers # Methods used to deploy Portal instances
├── models # Models for our a data types, such as portals and requests
├── schemas # Mongoose schema files
├── server # Our Express.js setup and WebSocket setup
├── services # Services such as queue management, etc
└── utils # Helper methods
First, clone the @cryb/portals repository locally:
git clone https://github.com/crybapp/portals.git
The following services need to be installed for @cryb/portals to function:
- MongoDB
- Redis
We recommend that you run the following services alongside @cryb/portals, but they're not required.
@cryb/api@cryb/web@cryb/aperture
You also need to install the required dependencies by running yarn.
Ensure that .env.example is either copied and renamed to .env, or is simply renamed to .env.
In this file, you'll need some values. Documentation is available in the .env.example file.
Make sure that you have installed MongoDB and Redis, and they are both running locally on port 27017 and 6379 respectively.
The command to start MongoDB is mongod, and the command to start Redis is redis-server.
If you're developing a feature that requires the VM infrastructure, then make sure @cryb/aperture is running.
To run @cryb/portals in development mode, run yarn dev.
It is recommended that in production you run yarn build, then yarn start.
@cryb/portals makes it easy to add a custom cloud provider to deploy Portal instances onto.
- First, make a config file under
src/config/providers. You want to call thisfoo.config.ts. This file should export a method that returns the API of the provider you want to use. Seeexample.config.tsorgcloud.config.tsfor an example of how Google Cloud intergration is handled. - Next, make a file under
src/drivers. You want to call thisfoo.driver.ts. You can copy the code inexample.driver.tsas a starting point. - Import your
foo.config.tsfile then create an instance of the client using thecreateClientmethod exported in the config file. - Then add the code to create a cloud deployment with the desired config under the
try {clause in theopenPortalInstancemethod. - Optional, but recommended Add the method under the
try {clause inclosePortalInstanceto destroy the VM instance. This will be called when a Room no longer needs a portal, such as when all members have gone offline. - Now, under
src/drivers/router.ts, import your driver and rename its methods so they don't conflict when any other drivers. See below:
import {
openPortalInstance as openFooPortalInstance,
closePortalInstance as closeFooPortalInstance
} from './foo.driver'- If you're not using TypeScript, skip this step Make sure you have added the name of your driver to the
Drivertype. See below:
type Driver = 'gcloud' | 'kubernetes' | 'foo'- Add a
caseto theswitchstatement underopenPortalInstancewith the name of your driver methods. See below:
switch(driver) {
...
case 'foo':
openFooPortalInstance(portal)
break
}- Optional, but recommended If you added a
closePortalInstancehandler in your driver, add acaseto theswitchstatement underclosePortalInstancewith the name of your driver methods. See below:
switch(driver) {
...
case 'foo':
closeFooPortalInstance(portal)
break
}- Make sure you change the current driver under
fetchCurrentDriver. See below:
const fetchCurrentDriver = () => 'foo' as DriverDone! Enjoy using @cryb/portals with the cloud provider of your preferred choice. For any help, view here. If you're feeling generous, create a PR request with your driver so the community can use it. Be sure to follow our Guidelines before submitting a PR.
If you have an issues with @cryb/portals, please either open a GitHub issue, contact a maintainer or join the Cryb Discord Server and ask in #tech-support.
