{"id":15357670,"url":"https://github.com/nauja/simple-typescript-tcp-jsonrpc","last_synced_at":"2026-04-19T13:32:16.723Z","repository":{"id":139652910,"uuid":"351712429","full_name":"Nauja/simple-typescript-tcp-jsonrpc","owner":"Nauja","description":"Example of a simple TCP server using JSON-RPC as a protocol","archived":false,"fork":false,"pushed_at":"2021-03-26T15:42:21.000Z","size":342,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T14:53:27.048Z","etag":null,"topics":["codecov","eslint","express","javascript-testing","node","nodejs","tcp-server","tslint","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/Nauja.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-26T08:28:50.000Z","updated_at":"2021-04-15T07:12:00.000Z","dependencies_parsed_at":"2023-07-23T08:30:47.078Z","dependency_job_id":null,"html_url":"https://github.com/Nauja/simple-typescript-tcp-jsonrpc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Nauja/simple-typescript-tcp-jsonrpc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nauja%2Fsimple-typescript-tcp-jsonrpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nauja%2Fsimple-typescript-tcp-jsonrpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nauja%2Fsimple-typescript-tcp-jsonrpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nauja%2Fsimple-typescript-tcp-jsonrpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Nauja","download_url":"https://codeload.github.com/Nauja/simple-typescript-tcp-jsonrpc/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nauja%2Fsimple-typescript-tcp-jsonrpc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32009119,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T20:23:30.271Z","status":"online","status_checked_at":"2026-04-19T02:00:07.110Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["codecov","eslint","express","javascript-testing","node","nodejs","tcp-server","tslint","typescript"],"created_at":"2024-10-01T12:37:51.622Z","updated_at":"2026-04-19T13:32:16.711Z","avatar_url":"https://github.com/Nauja.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# simple-typescript-tcp-jsonrpc\n\n[![build status](https://github.com/Nauja/simple-typescript-tcp-jsonrpc/actions/workflows/nodejs.yml/badge.svg)](https://github.com/Nauja/simple-typescript-tcp-jsonrpc/actions/workflows/nodejs.yml)\n[![Lint](https://github.com/Nauja/simple-typescript-tcp-jsonrpc/actions/workflows/lint.yml/badge.svg)](https://github.com/Nauja/simple-typescript-tcp-jsonrpc/actions/workflows/lint.yml)\n[![codecov](https://codecov.io/gh/Nauja/simple-typescript-tcp-jsonrpc/branch/main/graph/badge.svg?token=6ZHBCWGR2L)](https://codecov.io/gh/Nauja/simple-typescript-tcp-jsonrpc)\n[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n\nThis is just an example of a simple TCP server using JSON-RPC as a protocol.\n\nThe goal is to demonstrate how to build a simple TCP server with NodeJS and make a good use of basic best practices such as TypeScript, unit testing or lint.\n\n## Table of contents:\n\n- [Usage](#usage)\n- [TypeScript for static type definitions](#typescript-for-static-type-definitions)\n- [ESLint for code quality](#eslint-for-code-quality)\n- [Prettier for code formatting](#prettier-for-code-formatting)\n- [Jest for JavaScript testing](#jest-for-javascript-testing)\n- [Publish coverage to codecov](#publish-coverage-to-codecov)\n- [Testing](#testing)\n\n## Usage\n\nThis sample is not meant to be run standalone, it is more meant as a demonstration of writing a TCP server in NodeJS, respecting the best practices, and running unit tests.\n\nBut you can still run the sample with:\n\n```bash\n$ npm run build\n$ npm run start\nServer listening for connection requests on socket 0.0.0.0:65521\n```\n\nThis starts a TCP server you can send JSON-RPC messages to.\n\n## TypeScript for static type definitions\n\nIt is greatly recommended to use [TypeScript](https://www.typescriptlang.org/) on a big codebase as it make the code more readable, secure and reliable by adding static type definitions.\n\nThe first thing is to install [typescript](https://www.npmjs.com/package/typescript) and [@types/node](https://www.npmjs.com/package/@types/node) for NodeJS type definitions:\n\n```bash\n$ npm i --save-dev typescript @types/node\n```\n\nNow create a `tsconfig.json` configuration file containing:\n\n```json\n{\n    \"compilerOptions\": {\n        \"module\": \"commonjs\",\n        \"esModuleInterop\": true,\n        \"allowSyntheticDefaultImports\": true,\n        \"target\": \"es6\",\n        \"noImplicitAny\": true,\n        \"moduleResolution\": \"node\",\n        \"sourceMap\": true,\n        \"outDir\": \"dist\",\n        \"baseUrl\": \".\",\n        \"paths\": {\n            \"*\": [\n                \"node_modules/*\",\n                \"src/types/*\"\n            ]\n        }\n    },\n    \"include\": [\n        \"src/**/*\"\n    ]\n}\n```\n\nThis tells TypeScript to compile all `.ts` files from `\"src/**/*\"` to the `\"outDir\": \"dist\"` folder. Also note the `\"module\": \"commonjs\"` configuration that makes TypeScript compile your code to CommonJS modules.\n\nFor example, a `foo.ts` file with the following code:\n\n```js\nexport function foo()\n{\n    // content\n}\n```\n\nWould be compiled to `foo.js`:\n\n```js\n\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.foo = void 0;\nfunction foo() {\n    // content\n}\nexports.foo = foo;\n```\n\nThe last step is to add the following script in `package.json`:\n\n```json\n\"scripts\": {\n    \"build\": \"tsc\"\n}\n```\n\nYou can now build your code with:\n\n```bash\n$ npm run build\n```\n\n## ESLint for code quality\n\n[ESLint](https://eslint.org/) is a tool for helping to find and fix problems in JavaScript code. It is easily usable from command line or with Visual Code.\n\nCreate an `.eslintrc` file:\n\n```bash\n{\n    \"parser\": \"@typescript-eslint/parser\",\n    \"extends\": [\"plugin:@typescript-eslint/recommended\"],\n    \"rules\": {\n        \"sort-imports\": [\n            \"error\",\n            {\n                \"ignoreCase\": false,\n                \"ignoreDeclarationSort\": false,\n                \"ignoreMemberSort\": false,\n                \"memberSyntaxSortOrder\": [\"none\", \"all\", \"multiple\", \"single\"],\n                \"allowSeparatedGroups\": false\n            }\n        ],\n        \"@typescript-eslint/no-explicit-any\": 1,\n        \"@typescript-eslint/no-unused-vars\": \"warn\"\n    }\n}\n```\n\nThis make eslint use the default rules existing in `@typescript-eslint/recommended` plus the ones you define under `\"rules\"`. Here we force to sort imports by names, to avoid using the explicit type `any` in our code, and we warn about unused variables.\n\nCreate an `.eslintignore` file:\n\n```bash\nnode_modules\ndist\n```\n\nThis will make eslint ignore `node_modules` and `dist` folders.\n\nAdd the following script in `package.json`:\n\n```json\n\"scripts\": {\n    \"lint\": \"tsc --noEmit \u0026\u0026 eslint \\\"**/*.{js,ts}\\\" --quiet --fix\"\n}\n```\n\nRun eslint with:\n\n```bash\n$ npm run lint\n```\n\nThere are many rules you can enforce with eslint to make your code more safe and readable. For example, you can forbidden the `require` statement as part of an assignment:\n\n```js\nconst Server = require(\"jsonrpc-node\").TCP.Server\n// Should be:\n// import * as jsonrpc from \"jsonrpc-node\"\n// const Server = jsonrpc.TCP.Server\n```\n\nRunning eslint would output:\n\n```bash\nsimple-typescript-tcp-jsonrpc\\src\\app.ts\n  3:16  error  Require statement not part of import statement  @typescript-eslint/no-var-requires\n\n✖ 1 problem (1 error, 0 warnings)\n```\n\nOr in Visual Code (make sure to install the ESLint extension):\n\n![](https://github.com/Nauja/simple-typescript-tcp-jsonrpc/blob/media/eslint-vscode-require.png?raw=true)\n\n## Prettier for code formatting\n\n[Prettier](https://prettier.io/) is an opinionated tool to format your code consistently so everyone working on the project follow the same coding style and the code is more readable. You can use it both from command line and from VSCode.\n\nJust install it:\n\n```bash\n$ npm i --save-dev prettier\n```\n\nCreate a `.prettierrc` configuration file:\n\n```json\n{\n    \"trailingComma\": \"none\",\n    \"tabWidth\": 4,\n    \"semi\": true,\n    \"singleQuote\": false\n}\n```\n\nWhile those settings may be subjective, make sure to commit `.prettierrc` in your project so everyone follow the same rules.\n\nAdd the following script in `package.json`:\n\n```json\n\"scripts\": {\n    \"format\": \"prettier --write \\\"src/**/*.ts\\\"  \\\"test/**/*.ts\\\"\"\n}\n```\n\nAnd now you can format your codebase with:\n\n```bash\n$ npm run format\n```\n\n## Jest for JavaScript testing\n\nThis sample use [Jest](https://jestjs.io/) for unit testing as explained here [github.com/microsoft/TypeScript-Node-Starter](https://github.com/microsoft/TypeScript-Node-Starter).\n\nThe first thing is to install [jest](https://www.npmjs.com/package/jest) and [@types/jest](https://www.npmjs.com/package/@types/jest) for type definitions:\n\n```bash\n$ npm i --save-dev jest @types/jest\n```\n\nYou also need to install [ts-jest](https://www.npmjs.com/package/ts-jest) for testing a project written in TypeScript:\n\n```bash\n$ npm i --save-dev ts-jest\n```\n\nJest configuration is done in `jest.config.js`:\n\n```bash\nmodule.exports = {\n    globals: {\n        \"ts-jest\": {\n            tsconfig: \"tsconfig.json\"\n        }\n    },\n    moduleFileExtensions: [\n        \"ts\",\n        \"js\"\n    ],\n    transform: {\n        \"^.+\\\\.(ts|tsx)$\": \"ts-jest\"\n    },\n    testMatch: [\n        \"**/test/**/*.test.(ts|js)\"\n    ],\n    testEnvironment: \"node\"\n};\n```\n\nThis configures Jest to run all tests from the `test` directory matching the `**/test/**/*.test.(ts|js)` pattern. It also enable `ts-jest` to allow testing code written in TypeScript.\n\nThe last step is to add the following script in `package.json`:\n\n```json\n\"scripts\": {\n    \"test\": \"jest --forceExit --coverage --verbose\"\n}\n```\n\nYou can now run tests with:\n\n```bash\n$ npm run test\n\u003e simple-typescript-tcp-jsonrpc@1.0.0 test simple-typescript-tcp-jsonrpc\n\u003e jest --forceExit --coverage --verbose\n\n PASS  test/app.test.ts\n  test server RPCs\n    √ call ping should send pong (37 ms)\n\n  console.log\n    Server listening for connection requests on socket 0.0.0.0:65402\n\n      at Server.\u003canonymous\u003e (src/app.ts:23:17)\n\n  console.log\n    server port is 65402\n\n      at test/app.test.ts:13:21\n\n----------|---------|----------|---------|---------|-------------------\nFile      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s\n----------|---------|----------|---------|---------|-------------------\nAll files |     100 |       50 |     100 |     100 |\n app.ts   |     100 |       50 |     100 |     100 | 28\n----------|---------|----------|---------|---------|-------------------\nTest Suites: 1 passed, 1 total\nTests:       1 passed, 1 total\nSnapshots:   0 total\nTime:        3.808 s, estimated 4 s\nRan all test suites.\n```\n\n## Publish coverage to codecov\n\nFor continuous integration, it is best to automatically publish coverage results to a service like [codecov.io](https://codecov.io/).\n\nIt can be easily done with [codecov](https://www.npmjs.com/package/codecov):\n\n```bash\n$ npm install --save-dev codecov\n$ npm run test\n$ ./node_modules/.bin/codecov --token=CODECOV_TOKEN\n```\n\nOnce your report is uploaded to codecov, you can see it online:\n\n![](https://github.com/Nauja/simple-typescript-tcp-jsonrpc/blob/media/codecov-app.png?raw=true)\n\n## Testing\n\n```bash\n$ git clone https://github.com/Nauja/simple-typescript-tcp-jsonrpc.git\n$ cd simple-typescript-tcp-jsonrpc\n$ npm install\n$ npm test\n```\n\n## License\n\nLicensed under the [MIT](LICENSE) License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnauja%2Fsimple-typescript-tcp-jsonrpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnauja%2Fsimple-typescript-tcp-jsonrpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnauja%2Fsimple-typescript-tcp-jsonrpc/lists"}