{"id":13482996,"url":"https://github.com/mafintosh/discovery-server","last_synced_at":"2025-04-30T11:27:42.515Z","repository":{"id":57212849,"uuid":"109984821","full_name":"mafintosh/discovery-server","owner":"mafintosh","description":"A tcp/utp server that auto announces itself using discovery-channel","archived":false,"fork":false,"pushed_at":"2017-11-09T19:23:49.000Z","size":5,"stargazers_count":57,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-18T13:20:58.607Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/mafintosh.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":"2017-11-08T14:15:28.000Z","updated_at":"2025-02-11T15:49:52.000Z","dependencies_parsed_at":"2022-09-07T09:50:09.809Z","dependency_job_id":null,"html_url":"https://github.com/mafintosh/discovery-server","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fdiscovery-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fdiscovery-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fdiscovery-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fdiscovery-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mafintosh","download_url":"https://codeload.github.com/mafintosh/discovery-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251690146,"owners_count":21628052,"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-07-31T17:01:07.337Z","updated_at":"2025-04-30T11:27:42.489Z","avatar_url":"https://github.com/mafintosh.png","language":"JavaScript","readme":"# discovery-server\n\nA tcp/utp server that auto announces itself using [discovery-channel](https://github.com/maxogden/discovery-channel). Basically a server-only version of [discovery-swarm](https://github.com/mafintosh/discovery-swarm)\n\n## Usage\n\nFirst spin up a server\n\n```js\nvar createServer = require('discovery-server')\n\nvar server = createServer(function (socket) {\n  console.log('New connection!')\n  socket.write('Hello, World!\\n')\n  socket.end()\n})\n\nserver.listen('hello-world-server', 8080, function () {\n  console.log('Now listening ...')\n})\n```\n\nThen using discovery-channel you can connect to it\n\n```js\nvar dc = require('discovery-channel')()\nvar net = require('net')\n\ndc.on('peer', function (key, peer) {\n  var socket = net.connect(peer.port, peer.host)\n\n  socket.on('data', function (data) {\n    process.stdout.write(data)\n    socket.on('end', function () {\n      process.exit()\n    })\n  })\n\n  socket.on('error', function () {\n    socket.destroy()\n  })\n})\n\ndc.join('hello-world-server')\n```\n\n## API\n\n#### `var server = createServer([discoveryOptions], [onconnection])`\n\nCreate a new tcp + utp server. `discoveryOptions` are forwarded to the [discovery-channel](https://github.com/maxogden/discovery-channel) constructor. In addition you can set `{utp: false}` to disable utp and only use tcp\n\nOptionally you can pass a `onconnection` listener\n\n#### `server.listen(key, [port], [onlistening])`\n\nStart listening and announce your ip:port using the given key.\n\n#### `server.close([onclose])`\n\nCompletely close down the server.\n\n#### `server.join(key)`\n\nAnnounce to an addition key after listening. It is also safe to call `.listen` multiple times instead.\n\n#### `server.address()`\n\nReturns an address object after the server has started listening containing the port and local address.\n\n#### `server.leave(key)`\n\nStop announcing a key.\n\n#### `server.on('connection', connection, info)`\n\nEmitted when a tcp or utp socket connects. The info object looks like this `{type: 'utp' | 'tcp'}` and can be used to figure out what kind of connection you received.\n\n#### `server.on('listening')`\n\nEmitted when the server is fully listening.\n\n#### `server.on('close')`\n\nEmitted when the server is fully closed.\n\n## License\n\nMIT\n","funding_links":[],"categories":["Modules","P2P"],"sub_categories":["React Components"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Fdiscovery-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmafintosh%2Fdiscovery-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Fdiscovery-server/lists"}