{"id":25195972,"url":"https://github.com/simplisticated/connection-for-node","last_synced_at":"2025-04-04T15:44:10.180Z","repository":{"id":49012854,"uuid":"201739942","full_name":"simplisticated/Connection-for-Node","owner":"simplisticated","description":"The new way of socket communication. Made for Node.js. Written in TypeScript.","archived":false,"fork":false,"pushed_at":"2022-11-12T03:32:04.000Z","size":479,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-03-29T12:46:04.950Z","etag":null,"topics":["nodejs","socket","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/simplisticated.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":"2019-08-11T08:45:52.000Z","updated_at":"2021-12-21T13:29:51.000Z","dependencies_parsed_at":"2023-01-22T02:00:18.401Z","dependency_job_id":null,"html_url":"https://github.com/simplisticated/Connection-for-Node","commit_stats":null,"previous_names":["igormatyushkin014/connection-for-node"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplisticated%2FConnection-for-Node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplisticated%2FConnection-for-Node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplisticated%2FConnection-for-Node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplisticated%2FConnection-for-Node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simplisticated","download_url":"https://codeload.github.com/simplisticated/Connection-for-Node/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247208022,"owners_count":20901568,"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":["nodejs","socket","typescript"],"created_at":"2025-02-10T01:39:15.883Z","updated_at":"2025-04-04T15:44:10.154Z","avatar_url":"https://github.com/simplisticated.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\t\u003cimg src=\"images/logo.png\" alt=\"Manifest\" title=\"Manifest\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\t\u003ca href=\"https://nodejs.org\"\u003e\n\t\t\u003cimg src=\"https://img.shields.io/badge/Created for-Node.js-teal.svg?style=flat\"\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://www.typescriptlang.org\"\u003e\n\t\t\u003cimg src=\"https://img.shields.io/badge/Written in-TypeScript-purple.svg?style=flat\"\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)\"\u003e\n\t\t\u003cimg src=\"https://img.shields.io/badge/License-Apache 2.0-blue.svg?style=flat\"\u003e\n\t\u003c/a\u003e\n\u003c/p\u003e\n\n## At a Glance\n\n`Connection` is a new way of socket communication. It automatically converts sockets into client profiles and helps developer to associate personal data with each connected client. Also, `Connection` simplifies socket networking by asynchronous callbacks. The library is built on top of [socket.io](https://socket.io).\n\n**Important note.** This is a server-side of `Connection` library. For client-side solution, take a look at [JavaScript version](https://github.com/igormatyushkin014/Connection.js).\n\n## How to Get Started\n\nIf you use `npm`, type in Terminal:\n\n```\nnpm install --save @imatyushkin/connection\n```\n\nIf you prefer `yarn`, type:\n\n```\nyarn add @imatyushkin/connection\n```\n\n## Usage\n\n### Initial setup\n\n`Connection` requires HTTP or HTTPS server instance:\n\n```typescript\nconst connection = new Connection({\n\tserver: \u003cHTTP or HTTPS server instance\u003e\n});\n```\n\n### Clients\n\nInstead of low-level sockets, `Connection` considers every client as instance of `Client` type. Every instance has:\n\n- `id`: unique string that identifies client;\n- `socket`: reference to socket object from `socket.io` library;\n- `data`: optional object for storing client's data **(do whatever you want with this object)**.\n\nThe list of existing clients is accesible via:\n\n```typescript\nconnection.getClients()\n```\n\nYou can handle client-related events within the configuration of your `Connection` instance:\n\n```typescript\nconst connection = new Connection({\n\tserver: \u003cHTTP or HTTPS server instance\u003e,\n\tclients: {\n\t\tonConnected: (client) =\u003e {\n\t\t\t/*\n\t\t\t\tHandle new client.\n\t\t\t*/\n\t\t\tconsole.log(`Added client with ID: ${user.id}`);\n\t\t},\n\t\tonDisconnected: (client) =\u003e {\n\t\t\t/*\n\t\t\t\tHandle client's disconnection.\n\t\t\t*/\n\t\t\tconsole.log(`Removed client with ID: ${user.id}`);\n\t\t}\n\t}\n});\n```\n\n### Request and Response\n\nIn `socket.io`, every message sent between client and server includes event name and (optionally) some data. In `Connection` library, we use a different way of communication: request and response. It's similar to regular APIs where we send data by HTTP channel and (sometimes) receive response.\n\nTo receive requests from client, set up your configuration:\n\n```typescript\nconst connection = new Connection({\n\tserver: \u003cHTTP or HTTPS server instance\u003e,\n\tio: {\n\t\tonRequest: (request, respond) =\u003e {\n\t\t\t/*\n\t\t\t\tHandle request sent by client.\n\t\t\t*/\n\t\t\tlet sender = request.from;\n\t\t\t\n\t\t\tif (request.data.requireGreeting) {\n\t\t\t\t/*\n\t\t\t\t\tWe can send response to client by passing data to `respond` function.\n\t\t\t\t*/\n\t\t\t\trespond({\n\t\t\t\t\ttext: `Hello, client \"${sender.id}\"!`\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n});\n```\n\nSending request from server to client is super simple:\n\n```typescript\nconnection.send({\n\tto: \"RECIPIENT-ID\",\n\tevent: \"greeting\",\n\tdata: {\n\t\ttext: \"Hello!\"\n\t}\n});\n```\n\nLet's assume we want to say hello to the first client on the list:\n\n```typescript\nlet client = connection.getClients()[0];\nconnection.send({\n\tto: client.id,\n\tevent: \"greeting\",\n\tdata: {\n\t\ttext: \"Hello!\"\n\t}\n});\n```\n\nIf you want to get response, add `callback` to the options:\n\n```typescript\nlet user = connection.getClients()[0];\nconnection.send({\n\tto: client.id,\n\tevent: \"greeting\",\n\tdata: {\n\t\ttext: \"Hello!\"\n\t},\n\tcallback: (data) =\u003e {\n\t\t/*\n\t\t\tHandle response from client.\n\t\t*/\n\t}\n});\n```\n\nAlso, we can say hello to all connected clients:\n\n```typescript\nconnection.everyone({\n\tevent: \"greeting\",\n\tdata: {\n\t\ttext: \"Hello\"\n\t}\n});\n```\n\n### Conclusion\n\nHere's an example of extended configuration for `Connection` instance:\n\n```typescript\nconst connection = new Connection({\n\tserver: \u003cHTTP or HTTPS server instance\u003e,\n\tevents: {\n\t\tdefaultEvent: \"CustomEvent\"\n\t},\n\tclients: {\n\t\tonConnected: (client) =\u003e {\n\t\t\t/*\n\t\t\t\tHandle new client.\n\t\t\t*/\n\t\t\tconsole.log(`Added client with ID: ${user.id}`);\n\t\t},\n\t\tonDisconnected: (client) =\u003e {\n\t\t\t/*\n\t\t\t\tHandle client's disconnection.\n\t\t\t*/\n\t\t\tconsole.log(`Removed client with ID: ${user.id}`);\n\t\t}\n\t},\n\tio: {\n\t\tonRequest: (request, respond) =\u003e {\n\t\t\t/*\n\t\t\t\tHandle request sent by client.\n\t\t\t*/\n\t\t\tlet sender = request.from;\n\t\t\t\n\t\t\tif (request.data.requireGreeting) {\n\t\t\t\t/*\n\t\t\t\t\tWe can send response to client by passing\n\t\t\t\t\tdata to `respond` function.\n\t\t\t\t*/\n\t\t\t\trespond({\n\t\t\t\t\ttext: `Hello, client \"${sender.id}\"!`\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n});\n```\n\nMost of things presented in configuration are optional. You can combine necessary settings to get the server functioning right.\n\n## License\n\n`Connection` is available under the Apache 2.0 license. See the [LICENSE](./LICENSE) file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplisticated%2Fconnection-for-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimplisticated%2Fconnection-for-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplisticated%2Fconnection-for-node/lists"}