Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to Ably Realtime using socket_io_client #148

Closed
Nealsoni00 opened this issue Aug 8, 2021 · 5 comments
Closed

Connect to Ably Realtime using socket_io_client #148

Nealsoni00 opened this issue Aug 8, 2021 · 5 comments

Comments

@Nealsoni00
Copy link

Nealsoni00 commented Aug 8, 2021

Hey! I want to connect to ably on Flutter Desktop (MacOS/Windows) so I am unable to use the ably-flutter package.

Is it possible to connect to ably real-time using the socket.io client for flutter? Instructions on how to do this would be a good stopgap for the short-term solving a portion of #76.

Thanks!

┆Issue is synchronized with this Jira Uncategorised by Unito

@ben-xD
Copy link
Contributor

ben-xD commented Sep 5, 2021

Hey @Nealsoni00, unfortunately socket.io does not support connecting to Ably, this feature would need to be added to their library. So this would be a request for socket.io more than for Ably, since we do provide the details on how to connect to Ably. However, the adding support for Ably in socket.io would be complicated/ quite involved, as we have different features. For example, Ably provides presence, integration with other Ably features (reactor, push notifications), whereas Socket.io falls back to http. We have found that sticking to WebSockets for realtime channel messages works well, as Web Sockets are supported on 98.17% machines globally.

@ben-xD ben-xD closed this as completed Sep 5, 2021
@ben-xD
Copy link
Contributor

ben-xD commented Sep 13, 2021

@Nealsoni00, you could actually connect to Ably using MQTT though, since Ably has an MQTT Adapter, which translates back and forth between MQTT and Ably's protocol. This means mqtt_client should work.

For futher information, take a look at MQTT Adapter

@richard457
Copy link

@ben-xD can you share a working example? I appreciate

@richard457
Copy link

tried final client = MqttServerClient('mqtt.ably.io',
'crarPQ.t0xNhA:z0F8ZSFXk-MsNEtwVYVdfcx6KmPFNuGhd1R');
where crarPQ.t0xNhA:z0F8ZSFXk-MsNEtwVYVdfcx6KmPFNuGhd1R81nD is ably Private API Key
and connect like this: ``` /// Set logging on if needed, defaults to off
client.logging(on: false);

/// If you intend to use a keep alive you must set it here otherwise keep alive will be disabled.
client.keepAlivePeriod = 20;

/// Add the unsolicited disconnection callback
client.onDisconnected = onDisconnected;

/// Add the successful connection callback
client.onConnected = onConnected;

/// Add a subscribed callback, there is also an unsubscribed callback if you need it.
/// You can add these before connection or change them dynamically after connection if
/// you wish. There is also an onSubscribeFail callback for failed subscriptions, these
/// can fail either because you have tried to subscribe to an invalid topic or the broker
/// rejects the subscribe request.
client.onSubscribed = onSubscribed;

/// Set a ping received callback if needed, called whenever a ping response(pong) is received
/// from the broker.
client.pongCallback = pong;

/// Create a connection message to use or use the default one. The default one sets the
/// client identifier, any supplied username/password and clean session,
/// an example of a specific one below.
final connMess = MqttConnectMessage()
    .withClientIdentifier(
        'crarPQ.t0xNhA:z0F8ZSFXk-MsNEtwVYVdfcx6KmPFNuGhd1R81nDWlc0')
    .startClean() // Non persistent session for testing
    .withWillQos(MqttQos.atLeastOnce);
log.d('EXAMPLE::Mosquitto client connecting....');
client.connectionMessage = connMess;

/// Connect the client, any errors here are communicated by raising of the appropriate exception. Note
/// in some circumstances the broker will just disconnect us, see the spec about this, we however will
/// never send malformed messages.
try {
  await client.connect();
} on NoConnectionException catch (e) {
  // Raised by the client when connection fails.
  log.d('EXAMPLE::client exception - $e');
  client.disconnect();
} on SocketException catch (e) {
  // Raised by the socket layer
  log.d('EXAMPLE::socket exception - $e');
  client.disconnect();
}```

@richard457
Copy link

but the above code seems to not connect, any tip?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants