{"id":23777284,"url":"https://github.com/inaka/lasse","last_synced_at":"2025-09-05T20:32:19.059Z","repository":{"id":17647761,"uuid":"20452255","full_name":"inaka/lasse","owner":"inaka","description":"SSE handler for Cowboy","archived":false,"fork":false,"pushed_at":"2018-10-09T17:32:20.000Z","size":272,"stargazers_count":45,"open_issues_count":1,"forks_count":9,"subscribers_count":45,"default_branch":"master","last_synced_at":"2024-12-06T19:17:58.070Z","etag":null,"topics":["cowboy","erlang","hacktoberfest","lasse","sse"],"latest_commit_sha":null,"homepage":"","language":"Erlang","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inaka.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-03T17:24:57.000Z","updated_at":"2023-01-09T17:33:40.000Z","dependencies_parsed_at":"2022-09-02T22:00:55.131Z","dependency_job_id":null,"html_url":"https://github.com/inaka/lasse","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inaka%2Flasse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inaka%2Flasse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inaka%2Flasse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inaka%2Flasse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inaka","download_url":"https://codeload.github.com/inaka/lasse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231913900,"owners_count":18445008,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cowboy","erlang","hacktoberfest","lasse","sse"],"created_at":"2025-01-01T08:23:09.336Z","updated_at":"2025-01-01T08:23:10.025Z","avatar_url":"https://github.com/inaka.png","language":"Erlang","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lasse\n\n\u003cimg src=\"http://i61.tinypic.com/40pkl.jpg\" align=\"right\" style=\"float:right\" height=\"400\" /\u003e\n\nSSE handler for Cowboy.\n\n### References\n\n* [Cowboy](https://github.com/extend/cowboy)\n* [SSE](http://dev.w3.org/html5/eventsource/)\n* [canillita's handler](https://github.com/inaka/canillita/blob/master/src/canillita_news_handler.erl) (as a reference)\n\n### Usage\n\n``lasse`` provides a [cowboy loop handler](http://ninenines.eu/docs/en/cowboy/HEAD/guide/loop_handlers/)\ncalled ``lasse_handler`` that describes a behaviour. To include it in your server routes, just add\nthe following tuple to your [dispatch routes](http://ninenines.eu/docs/en/cowboy/HEAD/guide/routing/):\n\n```erlang\n{\u003c\u003c\"/your/[:route]\"\u003e\u003e, lasse_handler, [your_module]}\n% or\n{\u003c\u003c\"/your/[:route]\"\u003e\u003e, lasse_handler, [#{module =\u003e your_module, init_args =\u003e Args}]}\n```\n\nSpecifying the ``module`` (e.g ``your_module``) is mandatory while providing a value for ``init_args``\nis optional.\n\nAdditionally, in your module, you have to implement the ``lasse_handler`` behaviour and its\n[callbacks](#callbacks):\n\n```erlang\n-behaviour(lasse_handler).\n```\n\n### Contact Us\n\nIf you find any **bugs** or have a **problem** while using this library, please [open an issue](https://github.com/inaka/lasse/issues/new) in this repo (or a pull request :)).\n\nAnd you can check all of our open-source projects at [inaka.github.io](http://inaka.github.io)\n\n### Examples\n\nYou can find some example applications that implement the ``lasse_handler`` in the ``examples`` folder.\n\n## API\n\n### notify(Pid, Message) -\u003e ok\n\nUsed to send in-band messages to the handler given its Pid.\n\nTypes:\n\n- Pid = pid()\n- Message = any()\n\n\u003ca name=\"callbacks\"\u003e\u003c/a\u003e\n## Callbacks\n\n### init(LastEventId, Req) -\u003e {ok, NewReq, State}\n    | {ok, NewReq, InitialEvents, State}\n    | {no_content, NewReq, State}\n    | {shutdown, StatusCode, Headers, Body, NewReq, State}\n\nWill be called upon initialization of the handler, receiving the value of the ``\"last-event-id\"`` header\nif there is one or ``undefined`` otherwise. If everything goes well it should return\n``{ok, NewReq, State}`` to leave the connection open or ``{ok, NewReq, InitialEvents, State}`` if there are\nany ``InitialEvents`` that need to ben sent before the handler enters its loop.\n\nIn case the handler has no content to deliver it should return ``{no_content, NewReq, State}`` and the client\nwill receive a response with a status code ``204 No Content``. A custom response can be provided for other\nscenarios by returning ``{shutdown, StatusCode, Headers, Body, NewReq, State}``, which will cause the handler\nto reply to the client with the information supplied and then terminate.\n\nTypes:\n\n- InitArgs = any()\n- LastEventId = binary() | undefined\n- Req = cowboy_req:req()\n- NewReq = cowboy_req:req()\n- State = any()\n- StatusCode = cowboy:http_status()\n- Headers = cowboy:http_headers()\n- Body = iodata()\n\n### handle_notify(Msg, State) -\u003e Result\n\nReceives and processes in-band messages sent through the ``lasse_handler:notify/2`` function.\n\nTypes:\n\n- Msg = any()\n- State = any()\n- Result = [result()](#result_type)\n\n### handle_info(Msg, State) -\u003e Result\n\nReceives and processes out-of-band messages sent directly to the handler's process.\n\nTypes:\n\n- Msg = any()\n- State = any()\n- Result = [result()](#result_type)\n\n### handle_error(Msg, Reason, State) -\u003e NewState\n\nIf there's a problem while sending a chunk to the client, this function will be called after which the handler will terminate.\n\nTypes:\n\n- Msg = any()\n- Reason = atom()\n- State = any()\n- NewState = any()\n\n### terminate(Reason, Req, State) -\u003e ok\n\nThis function will be called before terminating the handler, its return value is ignored.\n\nTypes:\n\n- Reason = atom()\n- Req = cowboy:http_headers()\n- State = any()\n\n## Types\n\n\u003ca name=\"result_type\"\u003e\u003c/a\u003e\n### result() = {'send', Event :: event(), NewState :: any()}\n    | {'nosend', NewState :: any()}\n    | {'stop', NewState :: any()}\n\n### event() = [event_value(), ...]\n\nThe field ``data`` is required for every event returned by ``handle_notify()`` and ``hanfle_info()``,\nif none is supplied ``data_required`` will be thrown.\n\n### event_value() = {'id', binary()}\n    | {'event', binary()}\n    | {'data', binary()}\n    | {'retry', binary()}\n    | {'comment', binary()}\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finaka%2Flasse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finaka%2Flasse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finaka%2Flasse/lists"}