init method

void init({
  1. required String url,
  2. String? handshakeEvent,
  3. String? handshakeAckEvent,
  4. List<String> transports = const ['websocket'],
  5. int reconnectionAttempts = 15,
  6. int reconnectionDelay = 2000,
  7. Map<String, String>? extraHeaders,
  8. bool listenToConnectivity = true,
})

Creates and connects the socket to url.

  • handshakeEvent: event emitted right after connecting, with the payload from SmartApiSocketHooks.buildHandshakePayload. Leave null if your server doesn't need a post-connect handshake.
  • handshakeAckEvent: event the server emits back to acknowledge the handshake. Defaults to the same name as handshakeEvent.
  • transports, reconnectionAttempts, reconnectionDelay: passed straight through to socket_io_client's OptionBuilder.
  • extraHeaders: extra headers/options for the underlying engine.io connection (defaults to {'forceNew': 'true'}, matching typical Flutter socket setups). Values must be Strings.
  • listenToConnectivity: when true (default), the socket auto-reconnects as soon as connectivity_plus reports the network is back, and isConnected/isHandshakeComplete are reset to false while offline.

Implementation

void init({
  required String url,
  String? handshakeEvent,
  String? handshakeAckEvent,
  List<String> transports = const ['websocket'],
  int reconnectionAttempts = 15,
  int reconnectionDelay = 2000,
  Map<String, String>? extraHeaders,
  bool listenToConnectivity = true,
}) {
  _handshakeEvent = handshakeEvent;
  _handshakeAckEvent = handshakeAckEvent ?? handshakeEvent;

  _socket = io.io(
    url,
    io.OptionBuilder()
        .setTransports(transports)
        .enableAutoConnect()
        .setReconnectionAttempts(reconnectionAttempts)
        .setReconnectionDelay(reconnectionDelay)
        .setExtraHeaders(extraHeaders ?? {'forceNew': 'true'})
        .build(),
  );

  _bindCoreListeners();

  if (listenToConnectivity) _startListeningToNetwork();
}