{"id":13772080,"url":"https://github.com/feross/simple-websocket","last_synced_at":"2025-04-12T14:20:49.520Z","repository":{"id":19550033,"uuid":"22798555","full_name":"feross/simple-websocket","owner":"feross","description":"Simple, EventEmitter API for WebSockets","archived":false,"fork":false,"pushed_at":"2023-01-30T17:06:43.000Z","size":509,"stargazers_count":186,"open_issues_count":4,"forks_count":33,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-05-22T06:44:17.710Z","etag":null,"topics":["browser","nodejs","stream","streaming","websocket"],"latest_commit_sha":null,"homepage":"","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/feross.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":"2014-08-10T01:02:11.000Z","updated_at":"2024-05-09T07:51:11.000Z","dependencies_parsed_at":"2023-02-17T07:45:35.024Z","dependency_job_id":null,"html_url":"https://github.com/feross/simple-websocket","commit_stats":null,"previous_names":[],"tags_count":54,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feross%2Fsimple-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feross%2Fsimple-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feross%2Fsimple-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feross%2Fsimple-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/feross","download_url":"https://codeload.github.com/feross/simple-websocket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248578874,"owners_count":21127714,"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":["browser","nodejs","stream","streaming","websocket"],"created_at":"2024-08-03T17:00:59.700Z","updated_at":"2025-04-12T14:20:49.495Z","avatar_url":"https://github.com/feross.png","language":"JavaScript","funding_links":[],"categories":["Modules"],"sub_categories":[],"readme":"# simple-websocket [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]\n\n[travis-image]: https://img.shields.io/travis/feross/simple-websocket/master.svg\n[travis-url]: https://travis-ci.org/feross/simple-websocket\n[npm-image]: https://img.shields.io/npm/v/simple-websocket.svg\n[npm-url]: https://npmjs.org/package/simple-websocket\n[downloads-image]: https://img.shields.io/npm/dm/simple-websocket.svg\n[downloads-url]: https://npmjs.org/package/simple-websocket\n[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg\n[standard-url]: https://standardjs.com\n\n#### Simple, EventEmitter API for WebSockets\n\n[![Sauce Test Status](https://saucelabs.com/browser-matrix/simple-websocket.svg)](https://saucelabs.com/u/simple-websocket)\n\n## features\n\n- **super simple** API for working with WebSockets in the browser\n- supports **text and binary data**\n- node.js [duplex stream](http://nodejs.org/api/stream.html) interface\n- client \u0026 server implementations\n\nThis package is used by [WebTorrent](https://webtorrent.io).\n\n## install\n\n```\nnpm install simple-websocket\n```\n\nThis package works in the browser with [browserify](https://browserify.org). If\nyou do not use a bundler, you can use the `simplewebsocket.min.js` standalone script\ndirectly in a `\u003cscript\u003e` tag. This exports a `SimpleWebsocket` constructor on\n`window`. Wherever you see `Socket` in the examples below, substitute that with\n`SimpleWebsocket`.\n\n## real-world applications that use simple-websocket\n\n- [Virus Cafe](https://virus.cafe) - Make a friend in 2 minutes\n- [WebTorrent](https://webtorrent.io) - The streaming torrent app\n- [StudyNotes](http://www.apstudynotes.org) - Helping students learn faster and better\n- [bittorrent-tracker](https://github.com/feross/bittorrent-tracker) - Simple, robust, BitTorrent tracker (client \u0026 server) implementation\n- [instant.io](https://github.com/feross/instant.io) - Secure, anonymous, streaming file transfer\n- [lxjs-chat](https://github.com/feross/lxjs-chat) - Omegle chat clone\n- [Metastream](https://github.com/samuelmaddock/metastream) - Watch streaming media with friends.\n- \\[ your application here - send a PR \\]\n\n## usage\n\n```js\nvar Socket = require('simple-websocket')\n\nvar socket = new Socket('wss://echo.websocket.org')\nsocket.on('connect', function () {\n  // socket is connected!\n  socket.send('sup!')\n})\n\nsocket.on('data', function (data) {\n  console.log('got message: ' + data)\n})\n```\n\n## api\n\n### `socket = new Socket(url)`\n\nCreate a new WebSocket connection to the server at `url`. This usage is a shorthand\nfor `socket = new Socket({ url: url })`\n\n### `socket = new Socket(opts)`\n\nIf `opts.url` is specified as a string, then a WebSocket connection will be created\nto the server at `opts.url`.\n\nIf `opts.socket` is specified as an instance of a raw WebSocket object, then the\ngiven WebSocket object will be used and one will not be automatically be created\ninternally. (This is for advanced users.)\n\nOther properties on `opts` will be passed through to the underlying superclass,\n`stream.Duplex`.\n\n### `socket.send(data)`\n\nSend text/binary data to the WebSocket server. `data` can be any of several types:\n`String`, `Buffer` (see [buffer](https://github.com/feross/buffer)), `TypedArrayView`\n(`Uint8Array`, etc.), `ArrayBuffer`, or `Blob` (in browsers that support it).\n\nNote: If this method is called before the `socket.on('connect')` event has fired, then\ndata will be buffered.\n\n### `socket.destroy([err])`\n\nDestroy and cleanup this websocket connection.\n\nIf the optional `err` parameter is passed, then it will be emitted as an `'error'`\nevent on the stream.\n\n### `Socket.WEBSOCKET_SUPPORT`\n\nDetect WebSocket support in the javascript environment.\n\n```js\nvar Socket = require('simple-websocket')\n\nif (Socket.WEBSOCKET_SUPPORT) {\n  // websocket support!\n} else {\n  // fallback\n}\n```\n\n## events\n\n### `socket.on('connect', function () {})`\n\nFired when the websocket connection is ready to use.\n\n### `socket.on('data', function (data) {})`\n\nReceived a message from the websocket server.\n\n`data` will be either a `String` or a `Buffer/Uint8Array` (see [buffer](https://github.com/feross/buffer)).\nJSON strings will be parsed and the resulting `Object` emitted.\n\n### `socket.on('close', function () {})`\n\nCalled when the websocket connection has closed.\n\n### `socket.on('error', function (err) {})`\n\n`err` is an `Error` object.\n\nFired when a fatal error occurs.\n\n## server\n\nThe server implementation is basically `ws` but the `'connection'` event provides\nsockets that are instances of `simple-websocket`, i.e. they are duplex streams.\n\n```js\nvar Server = require('simple-websocket/server')\n\nvar server = new Server({ port: port }) // see `ws` docs for other options\n\nserver.on('connection', function (socket) {\n  socket.write('pong')\n  socket.on('data', function (data) {})\n  socket.on('close', function () {})\n  socket.on('error', function (err) {})\n})\n\nserver.close()\n```\n\n## license\n\nMIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeross%2Fsimple-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffeross%2Fsimple-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeross%2Fsimple-websocket/lists"}