{"id":15488653,"url":"https://github.com/byjg/jquery-sse","last_synced_at":"2025-04-19T11:56:09.379Z","repository":{"id":57282387,"uuid":"38286093","full_name":"byjg/jquery-sse","owner":"byjg","description":"jQuery Plugin for Server-Sent Events (SSE) EventSource Polyfill","archived":false,"fork":false,"pushed_at":"2022-04-01T03:47:43.000Z","size":30,"stargazers_count":52,"open_issues_count":1,"forks_count":18,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-19T08:14:18.152Z","etag":null,"topics":["eventsource","jquery-plugin","jquery-sse","server-sent-events"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/byjg.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2015-06-30T03:40:53.000Z","updated_at":"2024-08-02T08:11:02.000Z","dependencies_parsed_at":"2022-09-09T06:01:26.605Z","dependency_job_id":null,"html_url":"https://github.com/byjg/jquery-sse","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/byjg%2Fjquery-sse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fjquery-sse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fjquery-sse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fjquery-sse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/byjg","download_url":"https://codeload.github.com/byjg/jquery-sse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249690061,"owners_count":21311307,"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":["eventsource","jquery-plugin","jquery-sse","server-sent-events"],"created_at":"2024-10-02T07:01:23.446Z","updated_at":"2025-04-19T11:56:09.354Z","avatar_url":"https://github.com/byjg.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jQuery SSE\n\n[![Build Status](https://github.com/byjg/jquery-sse/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/byjg/jquery-sse/actions/workflows/build.yml)\n[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)\n[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/jquery-sse/)\n[![GitHub license](https://img.shields.io/github/license/byjg/jquery-sse.svg)](https://opensource.byjg.com/opensource/licensing.html)\n[![GitHub release](https://img.shields.io/github/release/byjg/jquery-sse.svg)](https://github.com/byjg/jquery-sse/releases/)\n[![](https://data.jsdelivr.com/v1/package/npm/jquery-sse/badge)](https://www.jsdelivr.com/package/npm/jquery-sse)\n\nA lightweigth jQuery Plugin for Server-Sent Events (SSE) EventSource Polyfill. \nThis plugin try to use the native EventSource object if it supported by the browser.\nIf there is no native support the request is made by ajax requests (polling).\nYou do not need to change the server side nor the client side.\n\n*If you are looking for a SSE Polyfill library without jQuery dependency\ntry [yaj-sse](https://github.com/byjg/yaj-sse). The yaj-sse is a port\nfrom version 0.1.4 of jQuery SSE.*\n\n## Example\n\nClient Side\n\n```javascript\nvar sse = $.SSE('http://example.com/sse-server.php', {\n    onMessage: function(e){ \n        console.log(\"Message\"); console.log(e); \n    }\n});\nsse.start();\n```\n\nServer Side\n\n```php\necho \"data: My Message\\n\";\necho \"\\n\";\n```\n\n## Dependencies\n\n* jQuery\n\n## Install\n\nJust download the repository and point to the jQuery plugin:\n\n```html\n\u003cscript src=\"jquery.sse.js\" \u003e\u003c/script\u003e\n```\n\nor\n\n```html\n\u003cscript src=\"jquery.sse.min.js\" \u003e\u003c/script\u003e\n```\n\nYou can also install using bower:\n\n```bash\nbower install jquery-sse\n```\n\n## Usage:\n\n### Constructor\n\n```\nvar sse = $.SSE(url, settings);\n```\n\n* url: URL for the server will be sent the events to this page;\n* settings: The events and options for the SSE instance\n\n### Settings List\n\nAll the options:\n\n```\nvar sseObject = $.SSE('sse-server.php', {\n    onOpen: function (e) {},\n    onEnd: function (e) {},\n    onError: function (e) {},\n    onMessage: function (e) {},\n    options: {},\n    headers: {},\n    events: {}\n});\n```\n\n**Event onOpen**\n\nFired when the connection is opened the first time;\n\n```javascript\nonOpen: function(e){ \n    console.log(\"Open\"); console.log(e); \n},\n```\n\n**Event onEnd**\n\nFired when the connection is closed and the client will not listen for the server events;\n\n```javascript\nonEnd: function(e){ \n    console.log(\"End\"); console.log(e); \n},\n```\n\n**Event onError**\n\nFired when the connection error occurs;\n\n```javascript\nonError: function(e){ \n    console.log(\"Could not connect\"); \n},\n```\n\n**Event onMessage**\n\nFired when the a message without event is received\n\n```javascript\nonMessage: function(e){ \n    console.log(\"Message\"); console.log(e); \n},\n```\n\n**Custom Options**\n\nDefine the options for the SSE instance\n\n```javascript\noptions: {\n    forceAjax: false\n},\n```\n\n* **forceAjax**: Uses ajax even if the EventSource object is supported natively;\n\n\n**Custom Events**\n\nFired when the server set the event and match with the key\n\nFor example, if you have a custom event called `myEvent` you may use the follow code:\n\n```javascript\nevents: {\n    myEvent: function(e) {\n        console.log('Custom Event');\n        console.log(e);\n    }\n}\n```\n\nServer side:\n\n```php\necho \"event: myEvent\\n\";   // Must match with events in the HTML.\necho \"data: My Message\\n\";\necho \"\\n\";\n```\n\n**Custom Headers**\n\nYou can send custom headers to the request.\n\n```javascript\nheaders: {\n    'Authorization': 'Bearer 1a234fd4983d'\n}\n```\n\nNote: As the EventSource does not support send custom headers to the request,\nthe object will fallback automatically to 'forceAjax=true', even this it is not set.\n\n\n## Methods\n\n**start**\n\nStart the EventSource communication\n\n```javascript\nsse.start();\n```\n\n**stop**\n\nStop the EventSource communication\n\n```javascript\nsse.stop();\n```\n\n\n## Quirks\n\nThe ajax does not support the streaming as the event source supports. In that case we recommend\ncreate a server without streaming and set the \"retry\" to determine query frequency;\n\nExample Server Side:\n\n```php\necho \"retry: 3000\\n\";\necho \"data: My Message\\n\";\necho \"\\n\";\n```\n\n## Minify\n\n```\napt install uglifyjs\n\nuglifyjs --compress 'drop_console,drop_debugger' --mangle -r '$,require,exports,_' -o jquery.sse.min.js jquery.sse.js\n```\n\n## Running the examples\n\nStart the webserver:\n\n```shell\ndocker run -it --rm -p 8080:80 -v $PWD:/var/www/html byjg/php:7.4-fpm-nginx \n```\n\nOpen the browser:\nhttp://localhost:8080/examples/sse-client.html\n\n## References\n\n* http://www.w3.org/TR/2009/WD-eventsource-20091029/\n* https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events\n* http://html5doctor.com/server-sent-events/\n* http://www.html5rocks.com/en/tutorials/eventsource/basics/\n\n----\n[Open source ByJG](http://opensource.byjg.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyjg%2Fjquery-sse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbyjg%2Fjquery-sse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyjg%2Fjquery-sse/lists"}