Installation

This guide will assume that you have already set up an Express project. To learn more about Express, click here.

  1. To begin, install the npm package by running the following command from your project directory:

    npm i @synapsejs/synapse
    
  2. Create a folder resources in the same directory as your Express server file.

  3. Configure your Express server file to include at least the following minimum configuration:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    const path = require('path');
    const express = require('express');
    const synapse = require('@synapsejs/synapse');
    const enableWs = require('express-ws');
    
    const app = express();
    const api = synapse(path.resolve(__dirname, './resources'));
    
    enableWs(app);
    app.ws('/api', api.ws);
    app.use('/api', api.sse, api.http);
    
    api.use((req, res) => {
      const state = res.locals;
      if (res.stream) {
        return res.stream(state);
      }
      return res.status(state.$status).send(state.serialize());
    });
    
    app.listen(3000, () => console.log(`listening on port 3000...`));
    
  4. Your express application is now prepared to serve resources defined in the resources directory to clients via the HTTP, WebSockets, and SSE protocols.

In the next section, we will define a resource in order to create a basic application.