Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 73 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Port of awesome JavaScript Node.js library - [Socket.io-client v2.0.1](https://g

## Usage


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

Expand Down Expand Up @@ -39,6 +38,78 @@ Port of awesome JavaScript Node.js library - [Socket.io-client v2.0.1](https://g
socket.on('fromServer', (_) => print(_));
}

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

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

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](https://github.com/rikulo/socket.io-client-dart/issues/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.
Expand Down Expand Up @@ -73,4 +144,4 @@ If you are new to Git or GitHub, please read [this guide](https://help.github.co
* Thanks [@felangel](https://github.com/felangel) for https://github.com/rikulo/socket.io-client-dart/issues/7
* Thanks [@Oskang09](https://github.com/Oskang09) for https://github.com/rikulo/socket.io-client-dart/issues/21
* Thanks [@bruce3x](https://github.com/bruce3x) for https://github.com/rikulo/socket.io-client-dart/issues/25
* Thanks [@Kavantix](https://github.com/Kavantix) for https://github.com/rikulo/socket.io-client-dart/issues/26
* Thanks [@Kavantix](https://github.com/Kavantix) for https://github.com/rikulo/socket.io-client-dart/issues/26