Skip to main content

Built-in queue

Subscribe to queues and post messages out of the box with Squid's built-in queue

When creating a Squid application, Squid automatically creates built-in queue integrations for your dev and prod environments.

To access the queue from the client, create a reference to a QueueManager using queue and passing the topic name:

Client code
const topic = squid.queue('topic-name');

If the topic doesn't yet exist, it is automatically created upon initializing the queue.

To read messages from a queue, use the consume method. This returns an Observable that updates whenever a new message is posted to the queue.

Client code
const topicMessagesObs = topic.consume<MyType>();

topicMessagesObs.subscribe((message: MyType) => {
console.log(message);
});
Note

When you subscribe to a topic, the Observable you receive is configured to only include messages produced after the subscription is established. This behavior is due to the subscription process involving a server call, which introduces an approximate delay of ~100ms. Therefore, you can expect a short wait before the Observable starts delivering new messages.

To prevent memory leaks, complete your Observable when no longer in use:

Client code
topicMessagesObs.unsubscribe();

To add messages to a queue, use the produce method. This method takes an array of messages that are then consumed by clients subscribed to the queue.

Client code
topicMessagesObs.produce(['hello', 'world']);

Securing your topics

Accessing your queue topics on the client requires security functions.

To secure a Squid queue topic, use the @secureTopic decorator, passing the topic name and the type of action. The following code allows read and write access to a queue for the 'topic-name' topic:

Backend code
import { SquidService, secureTopic } from '@squidcloud/backend';

export class ExampleService extends SquidService {
@secureTopic('topic-name', 'all')
allowTopicAccess(): boolean {
return true;
}
}

To learn more about security functions, see the Queue security docs.