{"id":13541024,"url":"https://github.com/mafintosh/utp-native","last_synced_at":"2025-04-07T05:07:53.720Z","repository":{"id":30459313,"uuid":"34013158","full_name":"mafintosh/utp-native","owner":"mafintosh","description":"Native bindings for libutp","archived":false,"fork":false,"pushed_at":"2024-09-03T21:18:50.000Z","size":215,"stargazers_count":103,"open_issues_count":14,"forks_count":37,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-31T04:04:19.294Z","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":"burnbright/silverstripe-importexport","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":".github/FUNDING.yml","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},"funding":{"github":"mafintosh"}},"created_at":"2015-04-15T19:08:19.000Z","updated_at":"2025-02-11T15:48:02.000Z","dependencies_parsed_at":"2024-10-23T04:24:12.420Z","dependency_job_id":null,"html_url":"https://github.com/mafintosh/utp-native","commit_stats":{"total_commits":160,"total_committers":13,"mean_commits":"12.307692307692308","dds":"0.13124999999999998","last_synced_commit":"6ff640bae5bf2d7e0e84cd757ee24ea08c40e051"},"previous_names":[],"tags_count":47,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Futp-native","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Futp-native/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Futp-native/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Futp-native/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mafintosh","download_url":"https://codeload.github.com/mafintosh/utp-native/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247595335,"owners_count":20963943,"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-08-01T10:00:37.572Z","updated_at":"2025-04-07T05:07:53.699Z","avatar_url":"https://github.com/mafintosh.png","language":"JavaScript","funding_links":["https://github.com/sponsors/mafintosh"],"categories":["Dat Core Modules","Protocols","Networking"],"sub_categories":["Networking"],"readme":"# utp-native\n\nNative bindings for [libutp](https://github.com/bittorrent/libutp). For more information about utp read [BEP 29](http://www.bittorrent.org/beps/bep_0029.html).\n\n```\nnpm install utp-native\n```\n\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)\n\n## Usage\n\n``` js\nconst utp = require('utp-native')\n\nconst server = utp.createServer(function (socket) {\n  socket.pipe(socket) // echo server\n})\n\nserver.listen(10000, function () {\n  const socket = utp.connect(10000)\n\n  socket.write('hello world')\n  socket.end()\n\n  socket.on('data', function (data) {\n    console.log('echo: ' + data)\n  })\n  socket.on('end', function () {\n    console.log('echo: (ended)')\n  })\n})\n```\n\n## API\n\nThere two APIs available. One that mimicks the net core module in Node as much as possible and another one that allows you to reuse the same udp socket for both the client and server. The last one is useful if you plan on using this in combination with NAT hole punching.\n\n## [net](http://nodejs.org/api/net.html)-like API\n\n#### `server = utp.createServer([options], [onconnection])`\n\nCreate a new utp server instance.\n\nOptions include\n\n```js\n{\n  allowHalfOpen: true // set to false to disallow half open connections\n}\n```\n\n#### `server.listen([port], [address], [onlistening])`\n\nListen for on port. If you don't provide a port or pass in `0` a free port will be used. Optionally you can provide an interface address as well, defaults to `0.0.0.0`.\n\n#### `addr = server.address()`\n\nReturns an address object, `{port, address}` that tell you which port / address this server is bound to.\n\n#### `server.on('listening')`\n\nEmitted when the server is listening\n\n#### `server.on('connection', connection)`\n\nEmitted when a client has connected to this server\n\n#### `server.on('error', err)`\n\nEmitted when a critical error happened\n\n#### `server.close()`\n\nCloses the server.\n\n#### `server.on('close')`\n\nEmitted when the server is fully closed. Note that this will only happen after all connections to the server are closed.\n\n#### `server.maxConnections`\n\nSet this property is you want to limit the max amount of connections you want to receive\n\n#### `server.connections`\n\nAn array of all the connections the server has.\n\n#### `server.ref()`\n\nOpposite of unref.\n\n#### `server.unref()`\n\nUnreferences the server from the node event loop.\n\n#### `connection = utp.connect(port, [host], [options])`\n\nCreate a new client connection. host defaults to localhost.\nThe client connection is a duplex stream that you can write / read from.\n\nOptions include:\n\n```js\n{\n  allowHalfOpen: true // set to false to disallow half open connections\n}\n```\n\n#### `address = connection.address()`\n\nSimilar to `server.address`.\n\n#### `connection.remoteAddress`\n\nThe address of the remote peer.\n\n#### `connection.remotePort`\n\nThe port of the remote peer.\n\n#### `connection.setInteractive(interactive)`\n\nIf you don't need every packet as soon as they arrive\nset `connection.setInteractive(false)`.\n\nThis might greatly improve performance\n\n#### `connection.setContentSize(size)`\n\nSet the expected content size. This will make utp-native\nbuffer larger chunks of data until `size` bytes have been read.\n\nThis might greatly improve performance\n\n#### `connection.setTimeout(ms, [ontimeout])`\n\nSet a continuous timeout. If no packets have been received within `ms`\na timeout event is triggered. Up to you to listen for this event and\npotentially destroy the socket. All timeouts are cancelled on socket end.\n\n#### `connection.on('connect')`\n\nEmitted when the connection is actually connected.\n\n#### `connection.on('close')`\n\nEmitted when the connection is fully closed.\n\n#### `connection.on('error', err)`\n\nEmitted if an unexpected error happens.\n\n#### `connection.destroy()`\n\nForcefully destroys the connection.\n\nIn addition to this the connection has all the classic stream methods such as `.write` etc.\n\nNote that utp requires the first data message to be sent from the client in a client/server scenario.\nIn most cases this is what happens anyway but something to be aware of. This module will cork the server stream until it\nreceives a client message because of that.\n\n## Socket API\n\nThe socket api allows you to reuse the same underlying UDP socket to both connect to other clients on accept incoming connections. It also mimicks the node core [dgram socket](https://nodejs.org/api/dgram.html#dgram_class_dgram_socket) api.\n\n#### `socket = utp([options])`\n\nCreate a new utp socket.\n\nOptions include:\n\n```js\n{\n  allowHalfOpen: true // set to false to disallow half open connections\n}\n```\n\n#### `socket.bind([port], [host], [onlistening])`\n\nBind the socket.\n\n#### `socket.on('listening')`\n\nEmitted when the socket is bound.\n\n#### `socket.send(buf, offset, len, port, host, [callback])`\n\nSend a udp message.\n\n#### `socket.on('message', buffer, rinfo)`\n\nListen for a udp message.\n\n#### `socket.close()`\n\nClose the socket.\n\n#### `address = socket.address()`\n\nReturns an address object, `{port, address}` that tell you which port / address this socket is bound to.\n\n#### `socket.on('close')`\n\nEmitted when the socket is fully closed.\n\n#### `socket.on('error')`\n\nEmitted if the socket experiences an error.\n\n#### `socket.listen([port], [host], [onlistening])`\n\nStart listening for incoming connections. Performs a bind as well.\n\n#### `socket.on('connection', connection)`\n\nEmitted after you start listening and a client connects to this socket.\nConnection is similar to the connection used in the net api.\n\n#### `connection = socket.connect(port, host)`\n\nConnect to another socket. Connection is similar to the connection used in the net api.\n\n#### `socket.unref()`\n\nDereference the socket from the node event loop.\n\n#### `socket.ref()`\n\nOpposite of `socket.unref()`\n\n## Development\n\nWhen developing you'll need to install the build tools based on your platform to make node-gyp run.\nThen run:\n\n```sh\nnpm run fetch-libutp\n```\n\nThis will fetch the libutp dependency as a gitsubmodule.\nThen build it using\n\n```sh\nnpm install\n```\n\nTo rebuild it simply do:\n\n```sh\nnode-gyp build\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Futp-native","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmafintosh%2Futp-native","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Futp-native/lists"}