Quick Start
Get a Strata service running in 5 minutes.
Prerequisites
If you don't have Redis running locally, the fastest way to get it going:
# Docker
docker run -d -p 6379:6379 redis
# macOS
brew install redis && brew services start redisInstall
Create a new project and install Strata:
mkdir my-service && cd my-service
npm init -y
npm install @strata-js/strataCreate a Service
Create service.ts with a single context and one operation:
import { StrataService, Context } from '@strata-js/strata';
// Create the service
const service = new StrataService({
service: { serviceGroup: 'QuickStart' },
backend: { type: 'redis', redis: { host: 'localhost', port: 6379 } },
});
// Create a context with an echo operation
const echo = new Context('echo');
echo.registerOperation('send', async (request) =>
{
return { message: `You said: ${ request.payload.text }` };
});
// Register the context and start
service.registerContext(echo);
await service.start();That's all the service needs. It listens on the QuickStart service group, routes requests to the echo context, and the send operation returns whatever text you give it.
Create a Client
In a separate file, create client.ts:
import { StrataClient } from '@strata-js/strata';
const client = new StrataClient({
client: { name: 'QuickStartClient' },
backend: { type: 'redis', redis: { host: 'localhost', port: 6379 } },
});
await client.start();
// Send a request to the service
const response = await client.request('QuickStart', 'echo', 'send', {
text: 'Hello, Strata!',
});
console.log(response.payload);
// { message: 'You said: Hello, Strata!' }
process.exit(0);The client connects to the same Redis backend, sends a request to the QuickStart service group targeting the echo context and send operation, and prints the response payload.
Run It
Start the service in one terminal:
npx tsx service.tsSend a request from another:
npx tsx client.tsYou should see { message: 'You said: Hello, Strata!' } printed in the client terminal.
What Just Happened
- The service connected to Redis and started listening on the
Requests:QuickStartqueue. - The client pushed a request envelope onto that queue with context
echoand operationsend. - The service popped the message, routed it to the
echocontext'ssendhandler, and pushed the response back to the client's dedicated response queue. - The client received the response and printed the payload.
All of the envelope construction, queue management, routing, and response matching happened automatically. You wrote a handler function and a request call -- Strata handled the rest.
Next Steps
- Your First Service -- build a more realistic service with multiple contexts, middleware, configuration files, and service-to-service calls.
- Configuration -- understand every option in the service and client config.
- Architecture -- how services, contexts, and operations fit together.