{"id":15093013,"url":"https://github.com/angular/ngsocket","last_synced_at":"2025-10-06T11:30:53.676Z","repository":{"id":13504239,"uuid":"16195085","full_name":"angular/ngSocket","owner":"angular","description":"WebSocket support for angular","archived":true,"fork":false,"pushed_at":"2015-06-26T05:42:26.000Z","size":326,"stargazers_count":204,"open_issues_count":15,"forks_count":25,"subscribers_count":22,"default_branch":"master","last_synced_at":"2024-09-30T07:21:06.648Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/angular.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-24T04:35:50.000Z","updated_at":"2024-02-26T17:10:46.000Z","dependencies_parsed_at":"2022-09-06T06:41:46.280Z","dependency_job_id":null,"html_url":"https://github.com/angular/ngSocket","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angular%2FngSocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angular%2FngSocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angular%2FngSocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angular%2FngSocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/angular","download_url":"https://codeload.github.com/angular/ngSocket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219877267,"owners_count":16554853,"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":[],"created_at":"2024-09-25T11:02:55.027Z","updated_at":"2025-10-06T11:30:48.363Z","avatar_url":"https://github.com/angular.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ngSocket [![Build Status](https://travis-ci.org/angular/ngSocket.png)](https://travis-ci.org/angular/ngSocket)\n\n## Status: In-Development\n\nAn AngularJS 1.x service for connecting applications to servers with WebSocket support.\n\n## Usage\n\nbower install ngSocket\n\n```javascript\n.controller('SomeController', function (ngSocket) {\n  //Open a WebSocket connection\n  var ws = ngSocket('ws://foo/bar');\n\n  //Can call before socket has opened\n  ws.send({foo: 'bar'});\n});\n```\n\n## API\n\n### Factory: `ngSocket` (in module `ngSocket`)\n\nreturns instance of NGWebSocket\n\n### Methods\n\nname        | arguments                                              | description\n------------|--------------------------------------------------------|------------\nngSocket \u003cbr\u003e_constructor_ | url:String                              | Creates and opens a [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance. `var ws = ngSocket('ws://foo');`\nsend        | data:String,Object returns                             | Adds data to a queue, and attempts to send if socket is ready. Accepts string or object, and will stringify objects before sending to socket.\nonMessage   | callback:Function \u003cbr\u003eoptions{filter:String,RegExp, autoApply:Boolean=true} | Register a callback to be fired on every message received from the websocket, or optionally just when the message's `data` property matches the filter provided in the options object. Each message handled will safely call `$rootScope.$digest()` unless `autoApply` is set to `false in the options. Callback gets called with a [MessageEvent](https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent?redirectlocale=en-US\u0026redirectslug=WebSockets%2FWebSockets_reference%2FMessageEvent) object.\nonOpen      | callback:Function                                      | Function to be executed each time a socket connection is opened for this instance.\nclose       | force:Boolean:_optional_                               | Close the underlying socket, as long as no data is still being sent from the client. Optionally force close, even if data is still being sent, by passing `true` as the `force` parameter. To check if data is being sent, read the value of `socket.bufferedAmount`.\n\n### Properties\nname               | type             | description\n-------------------|------------------|------------\nsocket             | window.WebSocket | [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance.\nsendQueue          | Array\u003cfunction\u003e  | Queue of `send` calls to be made on socket when socket is able to receive data. List is populated by calls to the `send` method, but this array can be spliced if data needs to be manually removed before it's been sent to a socket. Data is removed from the array after it's been sent to the socket.\nonOpenCallbacks    | Array\u003cfunction\u003e  | List of callbacks to be executed when the socket is opened, initially or on re-connection after broken connection. Callbacks should be added to this list through the `onOpen` method.\nonMessageCallbacks | Array\u003cfunction\u003e  | List of callbacks to be executed when a message is received from the socket. Callbacks should be added via the `onMessage` method.\nreadyState         | Number:readonly  | Returns either the readyState value from the underlying WebSocket instance, or a proprietary value representing the internal state of the lib, e.g. if the lib is in a state of re-connecting.\n\n### CancelablePromise\n\nThis type is returned from the `send()` instance method of ngSocket, inherits from [$q.defer().promise](https://docs.angularjs.org/api/ng/service/$q).\n\n### Methods\n\nname        | arguments                                              | description\n------------|--------------------------------------------------------|------------\ncancel      | | Alias to `deferred.reject()`, allows preventing an unsent message from being sent to socket for any arbitrary reason.\nthen        | resolve:Function, reject:Function | Resolves when message has been passed to socket, presuming the socket has a `readyState` of 1. Rejects if the socket is hopelessly disconnected now or in the future (i.e. the library is no longer attempting to reconnect). All messages are immediately rejected when the library has determined that re-establishing a connection is unlikely.\n\n\n### Service: `ngSocketBackend` (in module `ngSocketMock`)\n\nSimilar to [`httpBackend`](http://docs.angularjs.org/api/ngMock.$httpBackend) mock in AngularJS's `ngMock` module\n\n### Methods\n\nname                           | arguments  | description\n-------------------------------|------------|-----------------------------------\nflush                          |            | Executes all pending requests\nexpectConnect                  | url:String | Specify the url of an expected WebSocket connection\nexpectClose                    |            | Expect \"close\" to be called on the WebSocket\nexpectSend                     | msg:String | Expectation of send to be called, with required message\nverifyNoOutstandingExpectation |            | Makes sure all expectations have been satisfied, should be called in afterEach\nverifyNoOutstandingRequest     |            | Makes sure no requests are pending, should be called in afterEach\n\n## Logical Questions\n\n * *Q.*: What if the browser doesn't support WebSockets?\n * *A.*: This module will not help; it does not have a fallback story for browsers that do not support WebSockets.\n\n## Development\n\n```shell\n$ npm install\n$ bower install\n```\n\n### Unit Tests\n`$ npm test` Starts karma and watches files for changes\n\n### Manual Tests\n\nIn the project root directory:\n\n`$ node test-server` Starts a sample web socket server to send/receive messages\n`$ ./node_modules/.bin http-server` - Basic http server to seve a static file\nOpen localhost:8081/test-app.html and watch browser console and node console to see messages passing\n\n### Distribute\n`$ ./dist.sh` For now just copies `src/ngSocket.js` to `dist/` (bower is configured to ignore src/ and test, plus pretty much everything else)\n\n## TODO\n * Automatic re-connection when connection lost\n * Add `onerror` to allow applications to respond to socket errors in their own ways\n * Consider support for ArrayBuffer and Blob datatypes\n * Add `protocols` parameter to constructor\n\n## License\n[Apache 2.0](https://github.com/angular/ngSocket/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fangular%2Fngsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fangular%2Fngsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fangular%2Fngsocket/lists"}