# Reading from the $all stream

Reading from the all stream is similar to reading from an individual stream but with some small differences. Primarily the need to provide an admin user account credentials and that you need to to provide a transaction log position instead of a stream revision.

# Reading forwards

The simplest way to read the $all stream forwards is to supply a direction and transaction log position to start from. This can either be a stream position Start or a big int (unsigned 64 bit integer):

    var events = client.ReadAllAsync(
    	Direction.Forwards, Position.Start);
    const events = await client.readAll(10, {
      direction: FORWARDS,
      fromPosition: START,
    });
    // Make sure to add code blocks to your code group

    This will return an AsyncEnumerable that can be iterated on:

      await foreach (var e in events) {
      	Console.WriteLine(Encoding.UTF8.GetString(e.Event.Data.ToArray()));
      }
      for (var resolvedEvent of events) {
        console.log(resolvedEvent.event.data);
      }
      // Make sure to add code blocks to your code group

      There are a number of additional arguments you can provide when reading a stream.

      # maxCount

      Passing in the max count allows you to limit the number of events that returned.

      # resolveLinkTos

      When using projections to create new events you can set whether the generated events are pointers to existing events. Setting this value to true will tell EventStoreDB to return the event as well as the event linking to it.

        var result = client.ReadAllAsync(
            Direction.Forwards,
            Position.Start,
            resolveLinkTos: true,
            cancellationToken: cancellationToken);
        const events = await client.readAll(10, {
          direction: BACKWARDS,
          fromPosition: END,
          resolveLinkTos: true,
        });
        // Make sure to add code blocks to your code group

        # configureOperationOptions

        This argument is generic setting class for all operations that can be set on all operations executed against EventStoreDB.

        # userCredentials

        The credentials used to read the data can be supplied. to be used by the subscription as follows. This will override the default credentials set on the connection.

          var result = client.ReadAllAsync(
              Direction.Forwards,
              Position.Start,
              userCredentials: new UserCredentials("admin", "changeit"),
              cancellationToken: cancellationToken);
          const credentials = {
            username: "admin",
            password: "changeit",
          };
          const events = await client.readAll(10, {
            direction: FORWARDS,
            fromPosition: START,
            credentials,
          });
          // Make sure to add code blocks to your code group

          # Reading backwards

          As well as being able to read a stream forwards you can also go backwards. When reading backwards is the position will have to be set to the end if you want to read all the events:

            var events = client.ReadAllAsync(
            	Direction.Backwards, Position.End);
            const events = await client.readAll(10, {
              direction: BACKWARDS,
              fromPosition: END,
            });
            // Make sure to add code blocks to your code group

            TIP

            You can use reading backwards to find the last position in the stream. Just read backwards one event and get the position.

            # Handling system events

            When reading from the all stream EventStoreDB will also return system events. In most cases you won't need to care about these events and they should be ignored.

            All system events begin with $ or $$ and can be easily ignored by checking the EventType property.

              var events = client.ReadAllAsync(
              	Direction.Forwards, Position.Start);
              
              await foreach (var e in events) {
              	if (e.Event.EventType.StartsWith("$")) {
              		continue;
              	}
              
              	Console.WriteLine(Encoding.UTF8.GetString(e.Event.Data.ToArray()));
              }
              const events = await client.readAll(10, {
                direction: FORWARDS,
                fromPosition: START,
              });
              
              for (var resolvedEvent of events) {
                if (resolvedEvent.event.type.startsWith("$")) {
                  continue;
                }
                console.log(resolvedEvent.event.type);
              }
              // Make sure to add code blocks to your code group
              Last Updated: 12/30/2020, 10:38:09 AM