Skip to content

Commit 2b6a3f4

Browse files
committed
Prepare for publishing
1 parent e4b690b commit 2b6a3f4

5 files changed

Lines changed: 96 additions & 7 deletions

File tree

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2024 Ben Reinhart
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ServerSentEvents
22

3-
This module implements a performant [Server Sent Event](https://en.wikipedia.org/wiki/Server-sent_events) parser.
3+
Efficient and fully spec conformant Server Sent Event parser for Elixir.
44

55
## Usage
66

@@ -28,6 +28,59 @@ IO.inspect(rest) # ""
2828

2929
This can be useful for streaming environments where a single event may not reliably arrive in one chunk.
3030

31+
## Real world example
32+
33+
AI providers like OpenAI and Anthropic stream AI generated messages using Server Sent Events.
34+
This module can handle parsing the server sent events, returning a list of maps. For example,
35+
we can parse a streaming response from Anthropic:
36+
37+
```elixir
38+
Req.post("https://api.anthropic.com/v1/messages",
39+
json: request,
40+
into: fn {:data, data}, {req, res} ->
41+
buffer = Request.get_private(req, :sse_buffer, "")
42+
{events, buffer} = ServerSentEvents.parse(buffer <> data)
43+
Request.put_private(req, :sse_buffer, buffer)
44+
45+
if events != [] do
46+
# Do something with events, e.g., send to a process consuming them.
47+
send(pid, {:events, events})
48+
end
49+
50+
{:cont, {req, res}}
51+
end,
52+
headers: %{
53+
"x-api-key" => api_key(),
54+
"anthropic-version" => "2023-06-01"
55+
}
56+
)
57+
```
58+
59+
The first chunk from Anthropic tends to contain a couple of messages that look something like the following when parsed by this module:
60+
61+
```elixir
62+
# Parsing first chunk from Anthropic
63+
{events, ""} = ServerSentEvents.parse(chunk)
64+
IO.inspect(events)
65+
# [
66+
# %{
67+
# "data" => "{\"type\":\"message_start\",\"message\":{\"id\":\"msg_01LAFhYgKvtBB5ac5n41oyDn\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"claude-3-5-sonnet-20241022\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":12,\"output_tokens\":2}} }",
68+
# "event" => "message_start"
69+
# },
70+
# %{
71+
# "data" => "{\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"} }",
72+
# "event" => "content_block_start"
73+
# },
74+
# %{"data" => "{\"type\": \"ping\"}", "event" => "ping"},
75+
# %{
76+
# "data" => "{\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Here's\"} }",
77+
# "event" => "content_block_delta"
78+
# }
79+
# ]
80+
```
81+
82+
This is typically followed by filtering out unwanted events and JSON parsing the `data` field of meaningful events.
83+
3184
## Installation
3285

3386
The package can be installed by adding `server_sent_events` to your list of dependencies in `mix.exs`:

lib/server_sent_events.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
defmodule ServerSentEvents do
22
@moduledoc """
3-
This module implements a performant [Server Sent Event](https://en.wikipedia.org/wiki/Server-sent_events) parser.
3+
This module is an efficient and fully spec conformant Server Sent Event parser.
44
5-
The parser fully conforms to the [Server-Sent Events spec](https://html.spec.whatwg.org/multipage/server-sent-events.html).
5+
- https://en.wikipedia.org/wiki/Server-sent_events
6+
- https://html.spec.whatwg.org/multipage/server-sent-events.html
67
"""
78

89
@doc ~s"""

mix.exs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
defmodule ServerSentEvents.MixProject do
22
use Mix.Project
33

4+
@github_repo_url "https://github.com/benjreinhart/server_sent_events"
5+
6+
@description "Efficient and fully spec conformant Server Sent Event parser"
7+
48
def project do
59
[
610
app: :server_sent_events,
711
version: "0.1.0",
812
elixir: "~> 1.12",
913
start_permanent: Mix.env() == :prod,
14+
name: "Server Sent Events",
15+
description: @description,
16+
source_url: @github_repo_url,
17+
homepage_url: @github_repo_url,
18+
package: package(),
1019
deps: deps()
1120
]
1221
end
1322

14-
# Run "mix help compile.app" to learn about applications.
1523
def application do
1624
[
1725
extra_applications: [:logger]
1826
]
1927
end
2028

21-
# Run "mix help deps" to learn about dependencies.
29+
def description do
30+
@description
31+
end
32+
33+
def package do
34+
[
35+
maintainers: ["Ben Reinhart"],
36+
licenses: ["MIT"],
37+
links: %{
38+
"GitHub" => @github_repo_url
39+
}
40+
]
41+
end
42+
2243
defp deps do
2344
[
24-
# {:dep_from_hexpm, "~> 0.3.0"},
25-
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
45+
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false}
2646
]
2747
end
2848
end

mix.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
%{
2+
"earmark_parser": {:hex, :earmark_parser, "1.4.41", "ab34711c9dc6212dda44fcd20ecb87ac3f3fce6f0ca2f28d4a00e4154f8cd599", [:mix], [], "hexpm", "a81a04c7e34b6617c2792e291b5a2e57ab316365c2644ddc553bb9ed863ebefa"},
3+
"ex_doc": {:hex, :ex_doc, "0.34.2", "13eedf3844ccdce25cfd837b99bea9ad92c4e511233199440488d217c92571e8", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "5ce5f16b41208a50106afed3de6a2ed34f4acfd65715b82a0b84b49d995f95c1"},
4+
"makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"},
5+
"makeup_elixir": {:hex, :makeup_elixir, "1.0.0", "74bb8348c9b3a51d5c589bf5aebb0466a84b33274150e3b6ece1da45584afc82", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "49159b7d7d999e836bedaf09dcf35ca18b312230cf901b725a64f3f42e407983"},
6+
"makeup_erlang": {:hex, :makeup_erlang, "1.0.1", "c7f58c120b2b5aa5fd80d540a89fdf866ed42f1f3994e4fe189abebeab610839", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "8a89a1eeccc2d798d6ea15496a6e4870b75e014d1af514b1b71fa33134f57814"},
7+
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
8+
}

0 commit comments

Comments
 (0)