You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Encore uses static analysis to determine which services are accessing each Pub/Sub topic,
209
+
and what operations each service is performing.
210
+
211
+
That information is used for features such as rendering architecture diagrams, and is used by Encore Cloud to provision infrastructure correctly and configure IAM permissions.
212
+
213
+
This means `Topic` objects can't be passed around however you like,
214
+
as it makes static analysis impossible in many cases. To simplify your workflow, given these restrictions,
215
+
Encore supports defining a "reference" to a topic that can be passed around any way you want.
216
+
217
+
### Using topic references
218
+
219
+
Define a topic reference by calling `topic.ref<DesiredPermissions>()` from within a service, where `DesiredPermissions` is one of the pre-defined permission types defined in the `encore.dev/pubsub` module.
220
+
221
+
This means you're effectively pre-declaring the permissions you need, and only the methods that
222
+
are allowed by those permissions are available on the returned reference object.
223
+
224
+
For example, to get a reference to a topic that can publish messages:
225
+
226
+
```typescript
227
+
import { Publisher } from"encore.dev/pubsub";
228
+
const ref =cartEvents.ref<Publisher>();
229
+
230
+
// You can now freely pass around `ref`, and you can use
231
+
// `ref.publish()` just like you would `cartEvents.publish()`.
232
+
```
233
+
234
+
To ensure Encore still is aware of which permissions each service needs, the call to `topic.ref`
235
+
must be made from within a service, so that Encore knows which service to associate the permissions with.
236
+
237
+
Currently, the only permission type is `Publisher`, which allows publishing events to the topic.
238
+
We plan to add more permission types in the future.
0 commit comments