Getting started

var result = client.ReadStreamAsync(
    Direction.Forwards,
    "some-stream",
    StreamPosition.Start,
    cancellationToken: cancellationToken);

var events = await result.ToListAsync(cancellationToken);
1
2
3
4
5
6
7
ReadStreamOptions options = ReadStreamOptions.get()
        .forwards()
        .fromStart();

ReadResult result = client.readStream("some-stream", 10, options)
        .get();

List<ResolvedEvent> events = result.getEvents();
1
2
3
4
5
6
7
8
const events = client.readStream(STREAM_NAME, {
  direction: FORWARDS,
  fromRevision: START,
  maxCount: 10,
});
1
2
3
4
5
let result = client
    .read_stream("some-stream", &Default::default(), 10)
    .await?;

if let ReadResult::Ok(events) = result {
    // Doing something productive with the events.
}
1
2
3
4
5
6
7
const events = client.readStream<TestEvent>(STREAM_NAME, {
  direction: FORWARDS,
  fromRevision: START,
  maxCount: 10,
});
1
2
3
4
5

Get started by connecting your application to EventStoreDB. Complete the form below to generate the connection string and examples for different languages and SDKs.

Connection details

This page can help you to generate the connection string for a single-node or cluster deployment of EventStoreDB. You can use one of the following methods:

  • Use the Event Store Cloudopen in new window cluster ID.
  • Use the address of any node of a self-hosted cluster or single-node deployment. You need to have access to the node for the discovery feature to work.
  • Specify the deployment details manually.

Connecting to EventStoreDB

Required packages

Install the client SDK package to your project.

$ dotnet add package EventStore.Client.Grpc.Streams --version 21.2
# Maven
<dependency>
  <groupId>com.eventstore</groupId>
  <artifactId>db-client-java</artifactId>
  <version>0.5</version>
</dependency>

# Gradle
implementation 'com.eventstore:db-client-java:0.5'

# SBT
libraryDependencies += "com.eventstore" % "db-client-java" % "0.6"
# Yarn
$ yarn add @eventstore/db-client

# NPM
$ npm install --save @eventstore/db-client
No additional configuration is needed having Rust installed. Go check https://rustup.rs.
# Yarn
$ yarn add @eventstore/db-client

# NPM
$ npm install --save @eventstore/db-client

Preview clients

The following SDKs are currently in preview and can get API changes:

  • NodeJS
  • Java
  • Go
  • Rust

Connection string

Each SDK has its own way to configure the client, but it's always possible to use the connection string. The connection string below is generated according to the configuration you specified above, and it should work with each official SDK of EventStoreDB.

You can either put the connection string in the input box below, or use the connection details page to generate the connection string from your EventStoreDB deployment.

Creating a client

First thing first, we need a client.

var settings = EventStoreClientSettings
    .Create("{connectionString}");
var client = new EventStoreClient(settings);
1
2
3
EventStoreDBClientSettings settings = EventStoreDBConnectionString.parse("{connectionString}");
EventStoreDBClient client = EventStoreDBClient.create(settings);
1
2
const client = EventStoreDBClient.connectionString`esdb+discover://${CLOUD_ID}.mesdb.eventstore.cloud`;
1
let settings = "{connectionString}".parse()?;
let client = Client::new(settings).await?;
1
2
const client = EventStoreDBClient.connectionString`esdb+discover://${CLOUD_ID}.mesdb.eventstore.cloud`;
1

The client instance can be used as a singleton across the whole application. It doesn't need to open or close the connection.

Creating an event

You can write anything to EventStoreDB as events. The client needs a byte array as the event payload. Normally, you'd use a serialized object and it's up to you to choose the serialization method.

Server-side projections

User-defined server-side projections require events to be serialized to JSON format.

We use JSON for serialization in the documentation examples.

The code snippet below creates an event object instance, serializes it and puts it as payload to the EventData structure, which the client is able to write to the database.

var evt = new TestEvent
{
    EntityId = Guid.NewGuid().ToString("N"),
    ImportantData = "I wrote my first event!"
};

var eventData = new EventData(
    Uuid.NewUuid(),
    "TestEvent",
    JsonSerializer.SerializeToUtf8Bytes(evt)
);
1
2
3
4
5
6
7
8
9
10
11
TestEvent event = new TestEvent();
event.setId(UUID.randomUUID().toString());
event.setImportantData("I wrote my first event!");

EventData eventData = EventData
        .builderAsJson("TestEvent", event)
        .build();
1
2
3
4
5
6
7

    const event = jsonEvent({
      type: "TestEvent",
      data: {
        entityId: uuid(),
        importantData: "I wrote my first event!",
      },
    });
1
2
3
4
5
6
7
8
let event = TestEvent {
    id: Uuid::new_v4().to_string(),
    important_data: "I wrote my first event!".to_string(),
};

let event_data = EventData::json("TestEvent", event)?.id(Uuid::new_v4());
1
2
3
4
5
6
type TestEvent = JSONEventType<
  "TestEvent",
  {
    entityId: string;
    importantData: string;
  }
>;

const event = jsonEvent<TestEvent>({
  type: "TestEvent",
  data: {
    entityId: uuid(),
    importantData: "I wrote my first event!",
  },
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Appending events

Each event in the database has its own unique identifier (UUID). The database uses it to ensure idempotent writes, but it only works if you specify the stream revision when appending events to the stream.

In the snippet below, we append the event to the stream some-stream.

await client.AppendToStreamAsync(
    "some-stream",
    StreamState.Any,
    new[] { eventData },
    cancellationToken: cancellationToken
);
1
2
3
4
5
6
client.appendToStream("some-stream", eventData)
        .get();
1
2
await client.appendToStream(STREAM_NAME, event);
1
client
    .append_to_stream("some-stream", &Default::default(), event_data)
    .await?;
1
2
3
await client.appendToStream(STREAM_NAME, event);
1

Here we are appending events without checking if the stream exists or if the stream version matches the expected event version. See more advanced scenarios in appending events documentation.

Reading events

Finally, we can read events back from the some-stream stream.

var result = client.ReadStreamAsync(
    Direction.Forwards,
    "some-stream",
    StreamPosition.Start,
    cancellationToken: cancellationToken);

var events = await result.ToListAsync(cancellationToken);
1
2
3
4
5
6
7
ReadStreamOptions options = ReadStreamOptions.get()
        .forwards()
        .fromStart();

ReadResult result = client.readStream("some-stream", 10, options)
        .get();

List<ResolvedEvent> events = result.getEvents();
1
2
3
4
5
6
7
8
const events = client.readStream(STREAM_NAME, {
  direction: FORWARDS,
  fromRevision: START,
  maxCount: 10,
});
1
2
3
4
5
let result = client
    .read_stream("some-stream", &Default::default(), 10)
    .await?;

if let ReadResult::Ok(events) = result {
    // Doing something productive with the events.
}
1
2
3
4
5
6
7
const events = client.readStream<TestEvent>(STREAM_NAME, {
  direction: FORWARDS,
  fromRevision: START,
  maxCount: 10,
});
1
2
3
4
5

When you read events from the stream, you get 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.

Last Updated: 9/22/2021, 1:59:50 PM
Contributors: Alexey Zimarev, Mathew McLoughlin