{"id":20453010,"url":"https://github.com/alexcmgit/simple-socket","last_synced_at":"2025-09-24T18:31:25.415Z","repository":{"id":109371910,"uuid":"596303405","full_name":"alexrintt/simple-socket","owner":"alexrintt","description":"A simple library to work with Dart sockets.","archived":false,"fork":false,"pushed_at":"2023-09-11T06:59:56.000Z","size":15,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-29T23:53:16.430Z","etag":null,"topics":["dart","library","package","real","socket","tcp","time"],"latest_commit_sha":null,"homepage":"","language":"Dart","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/alexrintt.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2023-02-01T22:20:24.000Z","updated_at":"2024-09-26T07:14:27.000Z","dependencies_parsed_at":"2024-11-15T11:11:22.313Z","dependency_job_id":"53d3c08e-7b77-41ea-946d-f9ff4ba25854","html_url":"https://github.com/alexrintt/simple-socket","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexrintt%2Fsimple-socket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexrintt%2Fsimple-socket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexrintt%2Fsimple-socket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexrintt%2Fsimple-socket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexrintt","download_url":"https://codeload.github.com/alexrintt/simple-socket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234109894,"owners_count":18781541,"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":["dart","library","package","real","socket","tcp","time"],"created_at":"2024-11-15T11:11:12.191Z","updated_at":"2025-09-24T18:31:25.013Z","avatar_url":"https://github.com/alexrintt.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple socket library for Dart\n\nA simple library to work with Dart sockets.\n\nThe [socket-io NodeJS port for Dart](https://github.com/rikulo/socket.io-client-dart) is pretty good but it has missing type definitions and I don't like to work with `dynamic` data type. Also, it is pretty heavy, I don't need all that stuff, all I want is to make sure I have a minimal intuitive library that I can work on top of Dart socket classes without worrying about leaking the memory because I forgot the 9th Stream listener opened.\n\nCurrent implemented features:\n\n- Server-side supported.\n- Client-side supported.\n- Message-framing through `send` APIs.\n- Fully-typed API although optional.\n- Lightweight: less than 400 lines of code.\n- Event-driven callbacks.\n- Custom event types.\n\nBasic usage:\n\n```dart\nfinal SimpleServerSocket server = await SimpleServerSocket.bind();\n\nserver.on\u003cSimpleSocket\u003e('connection', (SimpleSocket client) {\n  // or: client.sendMessage('hello', 'Hi, client.');\n  client.sendSignal('hello');\n\n  client.on\u003cvoid\u003e('bye', (_) =\u003e server.destroy());\n});\n\nserver.listen();\n\n// On client-side:\n\nfinal SimpleSocket socket =\n    await SimpleSocket.connect('localhost', server.port);\n\nsocket.on\u003cvoid\u003e('hello', (_) =\u003e socket.replyWithSignal('bye'));\n```\n\nUsing messages:\n\n```dart\nfinal SimpleServerSocket server = await SimpleServerSocket.bind();\n\nserver.on\u003cSimpleSocket\u003e('connection', (SimpleSocket client) {\n  client.sendMessage('greetings', 'Sup client!');\n\n  client.on\u003cList\u003cint\u003e\u003e('bye', (List\u003cint\u003e data) {\n    print(String.fromCharCodes(data)); // Bye bye...\n    server.destroy();\n  });\n});\n\nserver.listen();\n\n// On client-side:\n\nfinal SimpleSocket socket =\n    await SimpleSocket.connect('localhost', server.port);\n\nsocket.on\u003cList\u003cint\u003e\u003e('greetings', (List\u003cint\u003e data) {\n  print(String.fromCharCodes(data)); // Sup client!\n  socket.reply('bye', 'Bye bye...');\n});\n```\n\n\u003cdetails\u003e\n  \u003csummary\u003eThat's what looks like working with raw Dart sockets.\u003c/summary\u003e\n\n```dart\nfinal ServerSocket server =\n    await ServerSocket.bind(InternetAddress.anyIPv4, 0);\n\nlate final StreamSubscription\u003cSocket\u003e onNewClientListener;\n\nFuture\u003cvoid\u003e closeServer() async {\n  await onNewClientListener.cancel();\n  await server.close();\n}\n\nonNewClientListener = server.listen(\n  (Socket client) {\n    late final StreamSubscription\u003cString\u003e onNewMessageListener;\n\n    Future\u003cvoid\u003e cancelListener() async {\n      await onNewMessageListener.cancel();\n    }\n\n    client.write('Sup client!');\n\n    onNewMessageListener = client.map(String.fromCharCodes).listen(\n      // No TCP messaging-frame support!\n      (String message) {\n        print(message);\n        if (message == 'bye') {\n          client.close();\n          closeServer();\n        }\n      },\n      cancelOnError: true,\n      onDone: cancelListener,\n      onError: (_) =\u003e cancelListener(),\n    );\n  },\n  cancelOnError: true,\n  onDone: closeServer,\n  onError: (_) =\u003e closeServer(),\n);\n\n// On client-side.\n\nfinal Socket socket = await Socket.connect('localhost', server.port);\n\nlate final StreamSubscription\u003cString\u003e onNewServerMessageListener;\n\nFuture\u003cvoid\u003e cancelListener() async {\n  socket.destroy();\n  await socket.close();\n  await onNewServerMessageListener.cancel();\n}\n\nonNewServerMessageListener = socket.map(String.fromCharCodes).listen(\n  // No TCP messaging-frame support!\n  (String message) {\n    if (message.startsWith('Sup')) {\n      print(message);\n      socket.write('bye');\n    }\n  },\n  cancelOnError: true,\n  onDone: cancelListener,\n  onError: (_) =\u003e cancelListener(),\n);\n```\n\n\u003c/details\u003e\n\n## Further reading\n\n- Message Framing - \u003chttps://blog.stephencleary.com/2009/04/message-framing.html\u003e.\n- How to create TCP message framing for the stream - \u003chttps://stackoverflow.com/questions/47382549/how-to-create-tcp-message-framing-for-the-stream\u003e.\n- Dart TCP socket concatenates all 'write' sync calls as a single packet - \u003chttps://stackoverflow.com/questions/75314786/dart-tcp-socket-concatenates-all-write-sync-calls-as-a-single-packet\u003e.\n\n\u003cbr /\u003e\u003cbr /\u003e\n\n\u003csamp\u003e\n\n\u003ch2 align=\"center\"\u003e\n  Open Source\n\u003c/h2\u003e\n\u003cp align=\"center\"\u003e\n  \u003csub\u003eCopyright © 2023-present, Alex Rintt.\u003c/sub\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003eSimple Socket \u003ca href=\"/LICENSE\"\u003eis MIT licensed 💖\u003c/a\u003e\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/51419598/216178168-82c36f0b-8634-425a-b74c-cb3c1361beb3.png\" width=\"35\" /\u003e\n\u003c/p\u003e\n\n\u003c/samp\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexcmgit%2Fsimple-socket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexcmgit%2Fsimple-socket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexcmgit%2Fsimple-socket/lists"}