Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Is there a sample code to know how to use this library? #91

Closed
AbbasiMohamad opened this issue Sep 16, 2022 · 5 comments
Closed

Comments

@AbbasiMohamad
Copy link

No description provided.

@eli-darkly
Copy link
Contributor

Currently no, just the API reference. Like all of the LaunchDarkly SSE implementations, this library exists because it's needed by other LaunchDarkly projects (https://launchdarkly.github.io/dotnet-server-sdk/, https://launchdarkly.github.io/dotnet-client-sdk/), so even though it can definitely be used for other purposes, we haven't focused on building out its own documentation.

I can write you a couple of examples right here, though.

@eli-darkly
Copy link
Contributor

Basically, any application that wants to use this to read an SSE stream needs to do the following things, at a minimum:

  1. Configure and create the EventSource. If you don't need any custom behavior, all you need is the URI of the stream you're connecting to:
    var eventSource = new EventSource(new Uri("https://my-stream-server.com/whatever-uri-path"));

Or if you need other configuration:

    var builder = Configuration.Builder(new Uri("https://my-stream-server.com/whatever-uri-path"))
        .RequestHeader("some-header-name", "header-value") // or whatever other options you want
        .Build();
    var eventSource = new EventSource(builder.Build());
  1. Specify a handler that the EventSource will call when it receives messages on the stream.
    eventSource.MessageReceived += (sender, eventArgs) => {
        var eventName = eventArgs.Message.Name;
        var eventData = eventArgs.Message.Data;
        // do whatever you want to do with the message
    }
  1. Tell the EventSource to connect to the stream. Once connected, it continues to run in the background and will automatically reconnect whenever the connection is broken, unless you have configured custom error-handling behavior with the ConfigurationBuilder options.
    Task.Run(() => eventSource.StartAsync());

@eli-darkly
Copy link
Contributor

I hope that's helpful.

@AbbasiMohamad
Copy link
Author

Thanks a lot @eli-darkly . That's what I wanted.

@qcjxberin
Copy link

How to implement the failure to restart when the link is broken?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants