socket_io_client 0.9.10

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

socket.io-client-dart #

Port of awesome JavaScript Node.js library - Socket.io-client v2.0.1 - in Dart

Usage #

import 'package:socket_io/socket_io.dart';
import 'package:socket_io_client/socket_io_client.dart' as IO;

main() {
    // Dart server
    var io = new Server();
    var nsp = io.of('/some');
    nsp.on('connection', (Socket client) {
      print('connection /some');
      client.on('msg', (data) {
        print('data from /some => $data');
        client.emit('fromServer', "ok 2");
      });
    });
      io.on('connection', (Socket client) {
        print('connection default namespace');
        client.on('msg', (data) {
          print('data from default => $data');
          client.emit('fromServer', "ok");
        });
      });
      io.listen(3000);

    // Dart client
    IO.Socket socket = IO.io('http://localhost:3000');
    socket.on('connect', (_) {
     print('connect');
     socket.emit('msg', 'test');
    });
    socket.on('event', (data) => print(data));
    socket.on('disconnect', (_) => print('disconnect'));
    socket.on('fromServer', (_) => print(_));
}

Connect manually #

To connect the socket manually, set the option autoConnect: false and call .connect().

For example,

Socket socket = io('http://localhost:3000', <String, dynamic>{
    'transports': ['websocket'],
    'autoConnect': false,
    'extraHeaders': {'foo': 'bar'} // optional
  });
socket.connect();

Note that .connect() should not be called if autoConnect: true, as this will cause all event handlers to get registered/fired twice. See Issue #33.

Update the extra headers #

Socket socket = ... // Create socket.
socket.io.options['extraHeaders'] = {'foo': 'bar'}; // Update the extra headers.
socket.io..disconnect()..connect(); // Reconnect the socket manually.

Emit with acknowledgement #

Socket socket = ... // Create socket.
socket.on('connect', (_) {
    print('connect');
    socket.emitWithAck('msg', 'init', ack: (data) {
        print('ack $data') ;
        if (data != null) {
          print('from server $data');
        } else {
          print("Null") ;
        }
    });
});

Socket connection events #

These events can be listened on.

const List EVENTS = [
  'connect',
  'connect_error',
  'connect_timeout',
  'connecting',
  'disconnect',
  'error',
  'reconnect',
  'reconnect_attempt',
  'reconnect_failed',
  'reconnect_error',
  'reconnecting',
  'ping',
  'pong'
];

// Replace 'connect' with any of the above events.
socket.on('connect', (_) {
    print('connect');
}

Acknowledge with the socket server that an event has been received. #

socket.on('eventName', (data) {
    final dataList = data as List;
    final ack = dataList.last as Function;
    ack(null);
});

Usage (Flutter) #

In Flutter env. it only works with dart:io websocket, not with dart:html websocket, so in this case you have to add 'transports': ['websocket'] when creates the socket instance.

For example,

IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
    'transports': ['websocket'],
    'extraHeaders': {'foo': 'bar'} // optional
  });

Notes to Contributors #

Fork socket.io-client-dart #

If you'd like to contribute back to the core, you can fork this repository and send us a pull request, when it is ready.

If you are new to Git or GitHub, please read this guide first.

Who Uses #

  • Quire - a simple, collaborative, multi-level task management tool.
  • KEIKAI - a web spreadsheet for Big Data.

Socket.io Dart Server #

Contributors #

0.9.10 #

Bug fix:

  • #72 Can't send Iterable as one packet

0.9.9 #

Bug fix:

  • #67 Retry connection backoff after 54 tries reconnections every 0 second

0.9.8 #

Bug fix:

  • #33 socket.on('receiveMessage',(data)=>print("data")) called twice

0.9.7+2 #

New Feature:

  • #48 add links to github repo in pubspec.yaml

0.9.7+1 #

New Feature:

  • #38 Improve pub.dev score

0.9.6+3 #

Bug fix:

  • #42 Error when using emitWithAck

0.9.5 #

New Feature:

  • #34 Add support for extraHeaders

Bug fix:

  • #39 The factor of Backoff with 54 retries causes an overflow

example/README.md

socket.io-client-dart example #

Port of awesome JavaScript Node.js library - Socket.io-client v2.0.1 - in Dart

Usage #

import 'package:socket_io/socket_io.dart';
import 'package:socket_io_client/socket_io_client.dart' as IO;

main() {
    // Dart server
    var io = new Server();
    var nsp = io.of('/some');
    nsp.on('connection', (Socket client) {
      print('connection /some');
      client.on('msg', (data) {
        print('data from /some => $data');
        client.emit('fromServer', "ok 2");
      });
    });
      io.on('connection', (Socket client) {
        print('connection default namespace');
        client.on('msg', (data) {
          print('data from default => $data');
          client.emit('fromServer', "ok");
        });
      });
      io.listen(3000);

    // Dart client
    IO.Socket socket = IO.io('http://localhost:3000');
    socket.on('connect', (_) {
     print('connect');
     socket.emit('msg', 'test');
    });
    socket.on('event', (data) => print(data));
    socket.on('disconnect', (_) => print('disconnect'));
    socket.on('fromServer', (_) => print(_));
}



Connect manually #

To connect the socket manually, set the option autoConnect: false and call .connect().

For example,

Socket socket = io('http://localhost:3000', <String, dynamic>{
    'transports': ['websocket'],
    'autoConnect': false,
    'extraHeaders': {'foo': 'bar'} // optional
  });
socket.connect();

Note that .connect() should not be called if autoConnect: true, as this will cause all event handlers to get registered/fired twice. See Issue #33.

Update the extra headers #

Socket socket = ... // Create socket.
socket.io.options['extraHeaders'] = {'foo': 'bar'}; // Update the extra headers.
socket.io..disconnect()..connect(); // Reconnect the socket manually.

Emit with acknowledgement #

Socket socket = ... // Create socket.
socket.on('connect', (_) {
    print('connect');
    socket.emitWithAck('msg', 'init', ack: (data) {
        print('ack $data') ;
        if (data != null) {
          print('from server $data');
        } else {
          print("Null") ;
        }
    });
});

Socket connection events #

These events can be listened on.

const List EVENTS = [
  'connect',
  'connect_error',
  'connect_timeout',
  'connecting',
  'disconnect',
  'error',
  'reconnect',
  'reconnect_attempt',
  'reconnect_failed',
  'reconnect_error',
  'reconnecting',
  'ping',
  'pong'
];

// Replace 'connect' with any of the above events.
socket.on('connect', (_) {
    print('connect');
}

Acknowledge with the socket server that an event has been received. #

socket.on('eventName', (data) {
    final dataList = data as List;
    final ack = dataList.last as Function;
    ack(null);
});

Usage (Flutter) #

In Flutter env. it only works with dart:io websocket, not with dart:html websocket, so in this case you have to add 'transports': ['websocket'] when creates the socket instance.

For example,

IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
    'transports': ['websocket'],
    'extraHeaders': {'foo': 'bar'} // optional
  });

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  socket_io_client: ^0.9.10

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:socket_io_client/socket_io_client.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
95
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on Jul 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Health suggestions

Fix lib/src/engine/transport/transports.dart. (-0.50 points)

Analysis of lib/src/engine/transport/transports.dart reported 1 hint:

line 1 col 1: Prefer using /// for doc comments.

Fix lib/src/manager.dart. (-0.50 points)

Analysis of lib/src/manager.dart reported 1 hint:

line 1 col 1: Prefer using /// for doc comments.

Format lib/src/engine/socket.dart.

Run dartfmt to format lib/src/engine/socket.dart.

Fix additional 3 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/engine/transport/transport.dart (Run dartfmt to format lib/src/engine/transport/transport.dart.)
  • lib/src/engine/transport/xhr_transport.dart (Run dartfmt to format lib/src/engine/transport/xhr_transport.dart.)
  • lib/src/socket.dart (Run dartfmt to format lib/src/socket.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
logging ^0.11.3+2 0.11.4
socket_io_common >=0.9.2 <2.0.0 0.9.2
Dev dependencies
build_runner ^1.0.0
build_web_compilers ^0.4.4
pedantic ^1.9.0
socket_io any
test >=1.3.0 <2.0.0