{"id":15554972,"url":"https://github.com/derrick56007/dart_websockets","last_synced_at":"2025-03-29T02:43:29.593Z","repository":{"id":56827777,"uuid":"312504472","full_name":"derrick56007/dart_websockets","owner":"derrick56007","description":"dart_websockets is a pure dart library for establishing simple WebSocket servers and clients.","archived":false,"fork":false,"pushed_at":"2020-11-20T22:41:35.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T02:43:26.388Z","etag":null,"topics":["dartlang","websocket"],"latest_commit_sha":null,"homepage":"https://github.com/Derrick56007/dart_websockets","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/derrick56007.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":"2020-11-13T07:34:07.000Z","updated_at":"2020-11-20T22:41:38.000Z","dependencies_parsed_at":"2022-08-28T21:10:51.984Z","dependency_job_id":null,"html_url":"https://github.com/derrick56007/dart_websockets","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/derrick56007%2Fdart_websockets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derrick56007%2Fdart_websockets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derrick56007%2Fdart_websockets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derrick56007%2Fdart_websockets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/derrick56007","download_url":"https://codeload.github.com/derrick56007/dart_websockets/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246131243,"owners_count":20728299,"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":["dartlang","websocket"],"created_at":"2024-10-02T15:05:27.329Z","updated_at":"2025-03-29T02:43:29.571Z","avatar_url":"https://github.com/derrick56007.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"## WebSockets for Dart\n\n**dart_websockets** is a pure dart library for establishing **simple** \n[WebSocket](https://tools.ietf.org/html/rfc6455) servers and clients.\n\n## Project Structure\n\nSee the [example](https://github.com/Derrick56007/dart_websockets/tree/main/example) folder for more details\n\n```\nproject/\n├── bin\n│   └── server.dart\n├── website/\n│   ├── web/\n│   │   ├── favicon.ico\n│   │   ├── index.html\n│   │   ├── main.dart\n│   │   └── styles.css\n│   ├── .packages\n│   ├── analysis_options.yaml\n│   ├── pubspec.lock\n│   └── pubspec.yaml\n├── .packages\n├── analysis_options.yaml\n├── pubspec.lock\n└── pubspec.yaml\n```\n\n## Installation\n\n- Add `dart_websockets` to your pubspec.yaml dependencies\n- Enable webdev (`pub global activate webdev`)\n- Run (`webdev build`) inside website/\n\n## Server-Side Usage\n\n```dart\n// inside server.dart\n\nimport 'dart:io';\nimport 'package:dart_websockets/server.dart';\n\nclass MyServer extends FileServer {\n  MyServer(String address, int port) : super(address, port);\n\n  @override\n  void handleSocketStart(HttpRequest req, ServerWebSocket socket) {\n    // when a socket connects to the server we declare the messages to listen to\n\n    socket // listening for the message 'ping'\n      ..on('ping', (data) =\u003e onPing(socket, data));\n\n    // onPing will be called and passed data whenever the 'ping' message is received\n  }\n\n  Future\u003cvoid\u003e onPing(ServerWebSocket socket, data) async {\n    print('received ping: $data');\n\n    // here we send data back to the socket that sent the ping message\n    socket.send('pong', {'info': 'hello-world'});\n  }\n}\n\n// run the server\nvoid main() async {\n  const address = '0.0.0.0';\n  const defaultPort = 8081;\n\n  final port = Platform.environment.containsKey('PORT') ? int.parse(Platform.environment['PORT']) : defaultPort;\n\n  final server = MyServer(address, port);\n  await server.init();\n}\n\n```\n\n## Client-Side Usage\n\n```dart\n// inside main.dart\n\nimport 'package:dart_websockets/client.dart';\n\nvoid main() async {\n  final address = '127.0.0.1';\n  final port = 8081;\n\n  final client = ClientWebSocket(address, port);\n  await client.start();\n\n  client //\n    ..on('pong', (data) =\u003e onPong(client, data));\n\n  client.send('ping', 'ping data');\n}\n\nFuture\u003cvoid\u003e onPong(ClientWebSocket socket, data) async {\n  print('pong data: $data');\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderrick56007%2Fdart_websockets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fderrick56007%2Fdart_websockets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderrick56007%2Fdart_websockets/lists"}