{"id":23004477,"url":"https://github.com/ctatu/ws-wrapper-dart2","last_synced_at":"2025-08-14T01:32:44.027Z","repository":{"id":45982223,"uuid":"199470912","full_name":"cTatu/ws-wrapper-dart2","owner":"cTatu","description":"Websocket event based wrapper for flutter","archived":false,"fork":false,"pushed_at":"2019-08-10T17:24:31.000Z","size":16,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-06T13:50:11.387Z","etag":null,"topics":["based","channels","dart","events","flutter","socket","websocket","wrapper"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/websocket_channel_wrapper","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cTatu.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}},"created_at":"2019-07-29T14:40:06.000Z","updated_at":"2024-05-06T13:50:11.388Z","dependencies_parsed_at":"2022-07-18T12:48:04.683Z","dependency_job_id":null,"html_url":"https://github.com/cTatu/ws-wrapper-dart2","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cTatu%2Fws-wrapper-dart2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cTatu%2Fws-wrapper-dart2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cTatu%2Fws-wrapper-dart2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cTatu%2Fws-wrapper-dart2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cTatu","download_url":"https://codeload.github.com/cTatu/ws-wrapper-dart2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229791400,"owners_count":18124683,"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":["based","channels","dart","events","flutter","socket","websocket","wrapper"],"created_at":"2024-12-15T07:18:39.335Z","updated_at":"2024-12-15T07:18:41.414Z","avatar_url":"https://github.com/cTatu.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"Wrapper for web_socket_channel package lightweight and isomorphic lib with socket.io-like event handling, Future-based requests. This wrapper implements the protocol for [this ws node wrapper](https://github.com/bminer/ws-wrapper).\n\n**WARNING: server -\u003e client requests are not sopported. If someone needs them open a PR**\n\n## Usage\n\nA simple usage example:\n\n**Client side:**\n```dart\nimport 'dart:async';\nimport 'dart:io';\nimport 'package:websocket_channel_wrapper/websocket_channel_wrapper.dart';\n\nconst PORT = 42069;\n\nmain() {\n  WebSocketChannelWrapper socket = WebSocketChannelWrapper('ws://192.168.0.18:$PORT',\n                                              headers: {'id': '1234567890qwertyuiop'});\n\n  print('Connecting...');\n\n  var onReady = socket.onConnect.first;\n\n  onReady.timeout(Duration(seconds: 5), onTimeout: () =\u003e print('Server could not be reach, trying to re-connect'));\n\n  socket.onConnect.skip(1).listen((_) =\u003e print('Successful re-connection to server'));\n\n  socket.onDone.listen((_) =\u003e print(socket.autoReconnect ? 'Connection lost, trying to re-connect!' : 'Connection ended!'));\n\n  onReady.then((_) async { // [onConnect] It's called every time the WebSocket reconnect\n    print('Connected!');\n\n    socket.emit('msg', ['DART_WEBSOCKET_WRAPPER', 'Hello, World!']);\n\n    socket.on('serverTime').listen((time) =\u003e print('Time: $time'));  // Time: 2019-07-29T14:00:32.635Z\n\n    socket.request('userCount').then((n) =\u003e print('# users: $n'));   // # users: 1\n\n    socket.request('checkError').catchError((e) =\u003e print(e));        // Yep, errors work\n\n    var filename = '/path/to/file/image.jpg';\n    sendFile(filename, socket);   // Send binary file over socket\n\n    Timer(Duration(seconds: 10), () {    // Close the socket after 1 minute\n      var reason = 'break time';\n      print('Close reason: $reason');\n      socket.close(closeReason: reason);\n    });\n  });\n}\n\nsendFile(String filename, socket) async {\n  var image = File(filename);\n  var contents = await image.readAsBytes();\n\n  final stopwatch = Stopwatch()..start();   // Start timer to measure speed\n  \n  socket.request('transferFile', [filename, contents]).then((_) { // Send first file name then the bytes\n\n    final speed = contents.length / stopwatch.elapsedMicroseconds;\n    print('File sended in ${stopwatch.elapsed} at $speed MB/s');  // File sended in 0:00:00.058191 at 1.63 MB/s\n\n  }).catchError((e) =\u003e print(e));\n}\n\n\n```\n\n**Server side:**\n```js\nconst WebSocketServer = require(\"ws\").Server\n  , WebSocketWrapper = require(\"ws-wrapper\")\nconst fs = require('fs')\n\nconst PORT = 42069\n\nvar wss = new WebSocketServer({port: PORT});\nvar sockets = new Set();\n\nwss.on(\"connection\", (sckt, req) =\u003e {\n  const socket = new WebSocketWrapper(sckt)\n  sockets.add(socket)\n\n  console.log('New socket connected')\n  socket.emit('serverTime', new Date())  // Send data to client on connect\n  \n  socket.on(\"msg\", function(from, msg) {\n      console.log(`Received message from ${from}: ${msg}`) // Received message from DART_WEBSOCKET_WRAPPER: Hello, World!\n  });\n\n  socket.on(\"userCount\", () =\u003e {  // Request\n  return sockets.size;\n  });\n  \n  socket.on(\"checkError\", () =\u003e { // Request rejecting the Promise\n      throw 'Yep, errors work'\n  })\n\n  socket.on(\"transferFile\", (filename, buffer) =\u003e {\n    return new Promise((resolve, reject) =\u003e {\n      fs.writeFile(filename, Buffer.from(buffer), function(err) { // Save binary data recieved to file\n        if(err)\n          reject(err)\n        else{\n          resolve(true)\n          console.log('File received and saved!')\n        }\n      })\n    })\n  })\n\n  socket.on(\"disconnect\", (event) =\u003e {\n      console.log(`REASON: ${event.reason} CODE: ${event.code}`) // REASON: break time    CODE: 1007\n      sockets.delete(socket);\n  });\n})\n\nconsole.log('Listening on port: ' + PORT)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctatu%2Fws-wrapper-dart2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fctatu%2Fws-wrapper-dart2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctatu%2Fws-wrapper-dart2/lists"}