diff --git a/docs/clients/grpc/getting-started/connecting.md b/docs/clients/grpc/getting-started/connecting.md index 2a9f9774..d296f421 100644 --- a/docs/clients/grpc/getting-started/connecting.md +++ b/docs/clients/grpc/getting-started/connecting.md @@ -100,7 +100,7 @@ const writeResult = await writeEventsToStream("testStream") -Here we are writing events without checking if the stream exists or if the stream version matches the expected event version. +Here we are writing events without checking if the stream exists or if the stream version matches the expected event version. See more advanced scenarios in [writing events documentation](../writing-events/README.md). ## Reading events @@ -126,6 +126,4 @@ const events = await readEventsFromStream("testStream") -When you read events from the stream, you can a collection of `ResolvedEvent` structures. The event payload is returned as a byte array and needs to be deserialized. - - +When you read events from the stream, you can a collection of `ResolvedEvent` structures. The event payload is returned as a byte array and needs to be deserialized. See more advanced scenarios in [reading events documentation](../reading-events/README.md). diff --git a/docs/clients/grpc/reading-events/reading-from-the-all-stream.md b/docs/clients/grpc/reading-events/reading-from-the-all-stream.md index d082364c..de9df2bf 100644 --- a/docs/clients/grpc/reading-events/reading-from-the-all-stream.md +++ b/docs/clients/grpc/reading-events/reading-from-the-all-stream.md @@ -43,7 +43,7 @@ When using projections to create new events you can set whether the generated ev -TODO + diff --git a/docs/clients/grpc/subscribing-to-streams/README.md b/docs/clients/grpc/subscribing-to-streams/README.md index fb58f8f4..f55891e2 100644 --- a/docs/clients/grpc/subscribing-to-streams/README.md +++ b/docs/clients/grpc/subscribing-to-streams/README.md @@ -10,7 +10,16 @@ If events already exist, the handler will be called for each event one by one un The simplest stream subscription looks like the following : + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#subscribe-to-stream + + + +<<< @/samples/grpc/nodejs/samples/subscribing-to-streams/index.js#subscribe-to-stream + + The provided handler will be called for every event in the stream. @@ -18,7 +27,16 @@ The provided handler will be called for every event in the stream. Subscribing to `$all` is much the same as subscribing to a single stream. The handler will be called for every event written after the starting position. + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#subscribe-to-all + + + +<<< @/samples/grpc/nodejs/samples/subscribing-to-streams/index.js#subscribe-to-all + + ## Subscribing from a specific position @@ -39,7 +57,16 @@ To subscribe to a stream from a specific position, you need to provide a `Stream The following subscribes to the stream "some-stream" at position 20, this means that events 21 and onward will be handled: + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#subscribe-to-stream-from-position + + + +<<< @/samples/grpc/nodejs/samples/subscribing-to-streams/index.js#subscribe-to-stream-from-position + + ### Subscribing to $all @@ -49,17 +76,44 @@ The corresponding `$all` subscription will subscribe from the event after the on Please note that this position will need to be a legitimate position in `$all`. + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#subscribe-to-all-from-position + + + +<<< @/samples/grpc/nodejs/samples/subscribing-to-streams/index.js#subscribe-to-all-from-position + + ## Subscribing to a stream for live updates You can subscribe to a stream to get live updates by subscribing to the end of the stream : + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#subscribe-to-stream-live + + + +<<< @/samples/grpc/nodejs/samples/subscribing-to-streams/index.js#subscribe-to-stream-live + + And the same works with `$all` : + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#subscribe-to-all-live + + + +<<< @/samples/grpc/nodejs/samples/subscribing-to-streams/index.js#subscribe-to-all-live + + This won't read through the history of the stream, but will rather notify the handler when a new event appears in the respective stream. @@ -73,7 +127,16 @@ Filtered subscriptions make it easier and faster to subscribe to all events of a When reading a stream you can specify whether to resolve link tos or not. By default, link-to events are not resolved. You can set this with the `resolveLinkTos` parameter : + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#subscribe-to-stream-resolving-linktos + + + +<<< @/samples/grpc/nodejs/samples/subscribing-to-streams/index.js#subscribe-to-stream-resolving-linktos + + ## Dropped subscriptions @@ -93,11 +156,29 @@ The possible reasons for a subscription dropping are : You can start from where you left off by keeping a record of the last processed event and continuing from there : + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#subscribe-to-stream-subscription-dropped + + + + + + When subscribed to `$all` you want to keep the position of the event in the `$all` stream : + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#subscribe-to-all-subscription-dropped + + + + + + ## Filter options @@ -105,7 +186,16 @@ Subscriptions to `$all` can include a filter option. This will only notify the e A simple stream prefix filter looks like this : + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#stream-prefix-filtered-subscription + + + +<<< @/samples/grpc/nodejs/samples/subscribing-to-streams/index.js#stream-prefix-filtered-subscription + + The filtering api is described more in-depth in the filtering section. @@ -115,4 +205,14 @@ The user creating a subscription must have read access to the stream it's subscr You can provide user credentials to be used by the subscription as follows. This will override the default credentials set on the connection. + + + <<< @/docs/clients/dotnet/generated/v20.6.1/samples/subscribing-to-streams/Program.cs#overriding-user-credentials + + + +<<< @/samples/grpc/nodejs/samples/subscribing-to-streams/index.js#overriding-user-credentials + + + diff --git a/samples/grpc/nodejs/samples/reading-events/index.js b/samples/grpc/nodejs/samples/reading-events/index.js index 304320d5..65950a39 100644 --- a/samples/grpc/nodejs/samples/reading-events/index.js +++ b/samples/grpc/nodejs/samples/reading-events/index.js @@ -9,8 +9,8 @@ import { export async function readFromStream(client) { // region read-from-stream const events = await client.readEventsFromStream("some-stream", 10, { - fromRevision: START, direction: FORWARD, + fromRevision: START, }); // endregion read-from-stream @@ -26,8 +26,8 @@ export async function readFromStream(client) { export async function readFromStreamPosition(client) { // region read-from-stream-position const events = await client.readEventsFromStream("some-stream", 20, { - fromRevision: BigInt(10), direction: FORWARD, + fromRevision: BigInt(10), }); // endregion read-from-stream-position @@ -45,8 +45,8 @@ export async function readFromStreamPositionCheck(client) { let events = []; try { events = await client.readEventsFromStream("some-stream", 20, { - fromRevision: BigInt(10), direction: FORWARD, + fromRevision: BigInt(10), }); } catch (error) { if (error.type == ErrorType.STREAM_NOT_FOUND) return; @@ -63,10 +63,10 @@ export async function readFromStreamPositionCheck(client) { } export async function readFromStreamBackwards(client) { - // region read-from-stream + // region reading-backwards const events = await client.readEventsFromStream("some-stream", 10, { - fromRevision: END, direction: BACKWARD, + fromRevision: END, }); for (var resolvedEvent of events) { @@ -79,9 +79,9 @@ export async function readFromStreamBackwards(client) { export async function readFromAllStream(client) { // region read-from-all-stream - const events = await client.readEventsFromStream(10, { - fromPosition: START, + const events = await client.readAllEvents(10, { direction: FORWARD, + fromPosition: START, }); // endregion read-from-all-stream @@ -96,9 +96,9 @@ export async function readFromAllStream(client) { export async function ignoreSystemEvents(client) { // region ignore-system-events - const events = await client.readEventsFromStream(10, { - fromPosition: START, + const events = await client.readAllEvents(10, { direction: FORWARD, + fromPosition: START, }); for (var resolvedEvent of events) { @@ -114,9 +114,9 @@ export async function ignoreSystemEvents(client) { export async function readFromAllStreamBackwards(client) { // region read-from-all-stream-backwards - const events = await client.readEventsFromStream(10, { - fromPosition: END, + const events = await client.readAllEvents(10, { direction: BACKWARD, + fromPosition: END, }); // endregion read-from-all-stream-backwards @@ -131,9 +131,9 @@ export async function readFromAllStreamBackwards(client) { export async function filterOutSystemEvents(client) { // region filter-out-system-events - const events = await client.readEventsFromStream(10, { - fromPosition: START, + const events = await client.readAllEvents(10, { direction: FORWARD, + fromPosition: START, }); for (var resolvedEvent of events) { @@ -149,9 +149,9 @@ export async function filterOutSystemEvents(client) { export async function readFromAllStreamResolvingLinkTos(client) { // region read-from-all-stream-resolving-link-Tos - const events = await client.readEventsFromStream(10, { - fromPosition: END, + const events = await client.readAllEvents(10, { direction: BACKWARD, + fromPosition: END, resolveLinks: true, }); // endregion read-from-all-stream-resolving-link-Tos diff --git a/samples/grpc/nodejs/samples/subscribing-to-stream/index.js b/samples/grpc/nodejs/samples/subscribing-to-streams/index.js similarity index 87% rename from samples/grpc/nodejs/samples/subscribing-to-stream/index.js rename to samples/grpc/nodejs/samples/subscribing-to-streams/index.js index 6690ea1c..b5368f6b 100644 --- a/samples/grpc/nodejs/samples/subscribing-to-stream/index.js +++ b/samples/grpc/nodejs/samples/subscribing-to-streams/index.js @@ -24,27 +24,22 @@ export async function subscribeToStreamFromPosition(client) { // region subscribe-to-stream-from-position const subscription = client .subscribeToStream("some-stream", { fromRevision: BigInt(20) }) - .on("data", function (resolvedEvent) { - console.log( - `Received event ${resolvedEvent.event.revision}@${resolvedEvent.event.streamId}` - ); - handleEvent(resolvedEvent); - }); + .on("data", handleEvent); // endregion subscribe-to-stream-from-position } export async function subscribeToStreamLive(client) { - // region subscribe-to-stream-from-position + // region subscribe-to-stream-live const subscription = client .subscribeToStream("some-stream", { fromRevision: END }) .on("data", handleEvent); - // endregion subscribe-to-stream-from-position + // endregion subscribe-to-stream-live } export async function subscribeToStreamResolvingLinkTos(client) { // region subscribe-to-stream-resolving-linktos const subscription = client - .subscribeToStream("some-stream", { + .subscribeToStream("$et-myEventType", { fromRevision: START, resolveLinks: true, }) @@ -67,9 +62,6 @@ export async function subscribeToStreamSubscriptionDropped(client) { // endregion subscribe-to-stream-resolving-linktos } -function handleEvent(event) { - console.log(event); -} // #region subscribe-to-stream-subscription-dropped // var checkpoint = StreamPosition.Start; // await client.SubscribeToStreamAsync( @@ -91,21 +83,14 @@ function handleEvent(event) { export async function subscribeToAll(client) { // region subscribe-to-all - const subscription = client - .subscribeToAll() - .on("data", function (resolvedEvent) { - console.log( - `Received event ${resolvedEvent.event.revision}@${resolvedEvent.event.streamId}` - ); - handleEvent(resolvedEvent); - }); + const subscription = client.subscribeToAll().on("data", handleEvent); // endregion subscribe-to-all } export async function subscribeToAllFromPosition(client) { // region subscribe-to-all-from-position const subscription = client - .subscribeToAll({ fromRevision: BigInt(20) }) + .subscribeToAll({ fromRevision: BigInt(1056) }) .on("data", function (resolvedEvent) { console.log( `Received event ${resolvedEvent.event.revision}@${resolvedEvent.event.streamId}` @@ -118,7 +103,7 @@ export async function subscribeToAllFromPosition(client) { export async function subscribeToAllLive(client) { // region subscribe-to-all-live const subscription = client - .subscribeToStream({ fromRevision: END }) + .subscribeToAll({ fromRevision: END }) .on("data", handleEvent); // endregion subscribe-to-all-live } @@ -178,3 +163,7 @@ export async function subscribeToAllOverridingUserCredentials(client) { .on("data", handleEvent); // endregion overriding-user-credentials } + +function handleEvent(event) { + console.log(event); +} diff --git a/samples/grpc/nodejs/samples/writing-events/index.js b/samples/grpc/nodejs/samples/writing-events/index.js index 4b4e6649..63df4651 100644 --- a/samples/grpc/nodejs/samples/writing-events/index.js +++ b/samples/grpc/nodejs/samples/writing-events/index.js @@ -48,7 +48,7 @@ export async function appendWithSameId(client) { export async function appendWithNoStream(client) { // region append-with-no-stream - const event = jsonEvent({ + const eventOne = jsonEvent({ id: uuid(), eventType: "some-event", payload: { @@ -57,12 +57,21 @@ export async function appendWithNoStream(client) { }, }); - await client.writeEventsToStream("no-stream-stream", event, { + const eventTwo = jsonEvent({ + id: uuid(), + eventType: "some-event", + payload: { + id: "2", + value: "some other value", + }, + }); + + await client.writeEventsToStream("no-stream-stream", eventOne, { expectedRevision: NO_STREAM, }); // attempt to append the same event again - await client.writeEventsToStream("no-stream-stream", event, { + await client.writeEventsToStream("no-stream-stream", eventTwo, { expectedRevision: NO_STREAM, }); // endregion append-with-no-stream