Skip to content

cloudcmd v11.7.0

Compare
Choose a tag to compare
@coderaiser coderaiser released this 25 Oct 10:13
· 1167 commits to master since this release

Prefix Socket

Feature we going to talk about today related to programmatic using of Cloud Commander. When you use globally installed or deployed application, any prefixes almost never will bother you.

But, if you use Cloud Commander as express middleware this information can be interesting for you :).

There is such thing as--prefix in Cloud Commander. It was created to simplify using as express middleware in a first place, but gradually overgrown with new cases such as prefix for web sockets connection, and it was not a problem, while Cloud Commander's api used this way:

app.use(cloudcmd());

But whenever you want to use express mount point you should set prefix as well:

app.use('/cloudcmd', cloudcmd({
    config: {
        prefix: '/cloudcmd',
   }
}));

Because with req.baseUrl we can determine what mount point is, but we cannot initiate web socket connection in middleware, it is just not right :).

For this purpose was decided to split prefix into two independent categories (anyways it is breaks single responsibility principle):

  • prefix - used to set mount point
  • prefix socket - used to set connection channel

With help of this dividing we can use

app.use('/cloudcmd', cloudcmd());

without setting prefix at all :). If we will set prefix it will be ignored:

app.use('/will-be-used', cloudcmd({
    config: {
        prefix: '/will-be-ignored'
    }
}));

So setting mounting point it is like force and it will override any prefix.

In case you will need to separate websockets channels, like:

  • /console
  • /config
  • /gritty
  • /dword
  • /edward
  • /deepword

Into some independent namespace, you can use prefixSocket, if prefixSocket=/hello, then channels will be:

  • /hello/console
  • /hello/config
  • /hello/gritty
  • /hello/dword
  • /hello/edward
  • /hello/deepword

I do not think that most users needs to set prefixSocket, but with help of this separation, using Cloud Commander middleware becames much more simple and pleasant :).

Before next major v12 release, when --prefix-socket is not set, it will be equal to --prefix to address backward compatibility. After release it will be two totally separated conceptions.

At the end I would like to show you simplest full example of using Cloud Commander as express middleware using mounting point with usual use of websockets.

const http = require('http');
const cloudcmd = require('cloudcmd');
const io = require('socket.io');
const app = require('express')();

const port = 1337;
const prefix = '/cloudcmd';

const server = http.createServer(app);
const socket = io.listen(server, {
  path: `${prefix}/socket.io`,
});

app.use(prefix, cloudcmd({
    socket,
}));

server.listen(port);

fix

  • feature(cloudcmd) add ability to set prefix for web sockets connections with --prefix-socket (#200)

feature

  • (cloudcmd) add ability to set prefix for web sockets connections with --prefix-socket (#200)
  • (package) deepword v5.0.0
  • (package) dword v9.0.0
  • (package) edward v9.0.0
  • (package) console-io v10.0.0
  • (package) ponse v3.0.0
  • (package) files-io v3.0.0