{"id":24375975,"url":"https://github.com/betaweb/websockets","last_synced_at":"2025-09-11T05:08:55.648Z","repository":{"id":66350176,"uuid":"260223534","full_name":"betaWeb/websockets","owner":"betaWeb","description":"A simple JavaScript class to handle browser-side Websockets","archived":false,"fork":false,"pushed_at":"2020-05-07T08:52:23.000Z","size":322,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-08T22:08:19.226Z","etag":null,"topics":["browser","javascript","javascript-class","simple-api","websocket","websocket-client","websockets"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/betaWeb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-30T13:46:45.000Z","updated_at":"2020-05-07T08:52:26.000Z","dependencies_parsed_at":"2023-06-03T04:45:36.600Z","dependency_job_id":null,"html_url":"https://github.com/betaWeb/websockets","commit_stats":{"total_commits":18,"total_committers":2,"mean_commits":9.0,"dds":0.2777777777777778,"last_synced_commit":"3b3d7c2b581df39943e84cc30926818bac77890e"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/betaWeb%2Fwebsockets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/betaWeb%2Fwebsockets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/betaWeb%2Fwebsockets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/betaWeb%2Fwebsockets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/betaWeb","download_url":"https://codeload.github.com/betaWeb/websockets/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243215458,"owners_count":20255173,"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","javascript","javascript-class","simple-api","websocket","websocket-client","websockets"],"created_at":"2025-01-19T05:58:56.812Z","updated_at":"2025-03-12T12:23:37.706Z","avatar_url":"https://github.com/betaWeb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Websockets\n\nA simple JavaScript class to handle browser-side Websockets.\n\n\u003e Note : this library provides an API only for browser-side Websokets.\n\n## Getting started\n\nInclude `Websockets.min.js` script in your HTML :\n```html\n\u003cscript defer src=\"path/to/Websockets.min.js\"\u003e\u003c/script\u003e\n```\n\nAnd declare a new instance in your script :\n```javascript\nconst client = new Websockets({\n    port: 9000,\n    scheme: 'ws',\n    onprogress(progress, dataSize, data) {\n        if (progress === 0) {\n            console.log('Data sended successfully !')\n        } else {\n            console.log(`${dataSize - progress}/${dataSize} bytes have been sent.`)\n        }\n    }\n})\n\nclient.on(Websockets.DEFAULT_EVENTS.CONNECTED, () =\u003e {\n    console.log(\"It's alive ! ALIVE !\")\n})\n\nclient.on('wakedup', () =\u003e {\n    console.log(\"Waked up !\")\n})\n\nawait client.connect()\n\nawait client.emit('wakeup', { body: \"It's time to wake up !\" })\n\nclient.destroy()\n```\n\nAnd.. that's it ! :) \n\n\u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\n\n## Available getters and methods\n### Getters\n```typescript\n// Websockets class default options\nstatic DEFAULT_OPTIONS: Object.\u003cString, any\u003e\n\n// WebSocket native API close codes by status\nstatic CLOSE_STATUS: Object.\u003cString, Number\u003e\n\n// WebSocket  native API close messages by codes\nstatic CLOSE_STATUS_MESSAGES: Object.\u003cNumber, String\u003e\n\n// Websockets class default events\nstatic DEFAULT_EVENTS: Object.\u003cString, String\u003e\n\n// returns true if WebSocket API is supported by the actual browser, false otherwise\nstatic hasSupport: Boolean\n\n// WebSocket schme ('auto', 'ws', 'wss')\nscheme: String\n\n// WebSocket URL\nurl: String\n\n// Registered events via `Websockets.on()` method\nevents: Object.\u003cString, Array\u003e\n\n// returns true if WebSocket client is instantiated, false otherwise\nisInitialized: Boolean\n\n// returns true if WebSocket client is connecting, false otherwise\nisConnecting: Boolean\n\n// returns true if WebSocket client is connected, false otherwise\nisOpen: Boolean\n\n// returns true if WebSocket client is sending data, false otherwise\nisSending: Boolean\n\n// returns true if WebSocket connection is closing or closed, false otherwise\nisClosed: Boolean\n```\n\n\u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\n\n### Methods\n\n```typescript\nconnect(): Promise\n```\n\nConnect the WebSocket client to the specified endpoint.\n\n__Example :__\n```javascript\nawait client.connect()\n```\n\n\u003cbr\u003e\u003chr\u003e\u003cbr\u003e\n\n```typescript\nsend(data: any): Promise\n```\n\nsend data to the server.\n\n__Example :__\n```javascript\nawait client.send('PING')\n```\n\n\u003cbr\u003e\u003chr\u003e\u003cbr\u003e\n\n```typescript\nemit(type: String, payload?: any = '', namespaced?: boolean = true): Promise\n```\n\nFormat a data object with `type` and `payload`, and send it to the server. If `namespaced` is false, `type` parameter will doesn't be prefixed by the namespace specified in the Websockets instance options.\n\n__Example :__\n```javascript\n// Emit 'USER_MESSAGE' event to the server with a payload\nawait client.emit('USER_MESSAGE', { message: 'Hello, World !', user_id: 1 })\n\n\n// Emit 'USER_MESSAGE' event to the server with a payload and without namespace\nawait client.emit('USER_MESSAGE', { message: 'Hello, World !', user_id: 1 }, false)\n```\n\n\u003cbr\u003e\u003chr\u003e\u003cbr\u003e\n\n```typescript\non(type: String, callback: Function, namespaced?: boolean = true): Websockets\n```\n\nAdd an event. If `namespaced` is false, `type` parameter will doesn't be prefixed by the namespace specified in the Websockets instance options.\n\n__Example :__\n```javascript\n// Listen to 'PONG' event from the server\nclient.on('PONG', () =\u003e console.log('The server responds with PONG !'))\n\n// Send 'PING' event to the server\nawait client.send('PING')\n```\n\n\u003cbr\u003e\u003chr\u003e\u003cbr\u003e\n\n```typescript\nonce(type: String, callback: Function, namespaced?: boolean = true): Websockets\n```\n\nAdd an event. This event will be listenend only once. After that, it will be deleted.\nIf `namespaced` is false, `type` parameter will doesn't be prefixed by the namespace specified in the Websockets instance options.\n\n__Example :__\n```javascript\n// Listen to 'PONG' event from the server\nclient.once('PONG', () =\u003e console.log('This event is unique !'))\n\n// Send 'PING' event to the server\nawait client.send('PING')\n```\n\n\u003cbr\u003e\u003chr\u003e\u003cbr\u003e\n\n```typescript\noff(type: String, callback?: Function = null, namespaced?: boolean = true): Websockets\n```\n\nRemoves an event. if `callback` parameter is `null`, all events of type `type` will be removed, otherwise, only the event that corresponds to the `callback` parameter will be removed. If `namespaced` is false, `type` parameter doesn't be prefixed by the namespace specified in the Websockets instance options.\n\n__Example :__\n```javascript\nconst onPong = () =\u003e console.log(\"The server responds with PONG !\\n\")\n\n// Listen to 'PONG' event from the server\nclient.on('PONG', onPong)\n\n// Send 'PING' event to the server\nawait client.send('PING')\n\n// Detach 'PONG' event listener\nclient.off('PONG', onPong)\n```\n\n\u003cbr\u003e\u003chr\u003e\u003cbr\u003e\n\n```typescript\nonMessage(callback: Function): Websockets\n```\n\nThe callback parameter will be triggered with each message sent from the server.\n\n__Example :__\n```javascript\nclient.onMessage(data =\u003e console.log(data))\n```\n\n\u003cbr\u003e\u003chr\u003e\u003cbr\u003e\n\n```typescript\ndisconnect(): void\n```\n\ndisconnect the WebSocket client.\n\n__Example :__\n```javascript\nclient.disconnect()\n```\n\n\u003cbr\u003e\u003chr\u003e\u003cbr\u003e\n\n```typescript\ndestroy(): void\n```\n\nRemove all Websockets events and disconnect the WebSocket client.\n\n__Example :__\n```javascript\nclient.destroy()\n```\n\n\u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\n\n## Instance options\n\nTODO\n\n\u003cbr\u003e\u003cbr\u003e","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbetaweb%2Fwebsockets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbetaweb%2Fwebsockets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbetaweb%2Fwebsockets/lists"}