Search code examples
c#.netconnectionchannelably-realtime

Get Unknown error when connecting creating a connection using Ably.io


We have a use case where the user has not logged into the application yet and no authentication token has been created. The application needs to connect to the web server on a public channel to check for application updates. The connection fails. With the following Error Reason: "Unknown error; Code: 500; HttpStatus Code: (404)NotFound.

Ably library and SDK from Nuget 0.8.11.

The following code does not connect to the web server.

    public ExtendedAblyIoClient(string name, string ClientId, string ChannelId, string AuthUrl, string ablyKey)
    {
        _name = name;
        _authUrl = AuthUrl;
        _clientId = ClientId;
        _channelId = ChannelId;
        _ablyAppKey = ablyKey;
        _authUri = new Uri(_authUrl);  // local host for testing and development.
        _httpRequestTime = TimeSpan.FromHours(2.0);

        ClientOptions clientOptions = new ClientOptions
        {
            Key = _ablyAppKey,
            ClientId = _clientId,
            AuthUrl = _authUri,
            Tls = false,
            HttpRequestTimeout = _httpRequestTime,
            HttpOpenTimeout = _httpRequestTime
        };

        commonInitialization(clientOptions);
        _channel = _ablyClient.Channels.Get(_channelId);
        _channel.Subscribe(message =>
        {
            OnMessageCallback(_sender, _channelId, message.Data.ToString());
        });

    }

    private void commonInitialization(ClientOptions clientOptions)
    {
        _ablyClient = new AblyRealtime(clientOptions);

        _ablyClient.Connection.On(ConnectionState.Connected, args =>
        {
            realTimeClientOnConnected(_sender);
        });

        _ablyClient.Connection.On(ConnectionState.Disconnected, args =>
        {
            realTimeClientOnDisconnected(_sender);
        });
        _ablyClient.Connection.On(ConnectionState.Failed, args =>
        {
            string WhyError = _name + " Failed: " + _ablyClient.ToString();
            realTimeClientOnDisconnected(WhyError);
        });
    }

Solution

  • The 404 will be from the client library trying to contact the AuthUrl you specified, and getting a 404.

    From your question it sounds like you're expecting to not have to authenticate if connecting to a 'public channel'. This isn't how Ably auth works; any user connecting to your application needs to be authenticated have a token or api key to connect with, which is what it means to be authenticated.

    If you don't want them to connect to any channels that aren't in a public: namespace, and only has subscribe capabilities for those, you can give the token with capabilities set to {"public:*":["subscribe"]}. But you still need to give them a token.

    Have a read through https://www.ably.io/documentation/general/authentication for documentation on Ably's auth model, and https://www.ably.io/documentation/realtime/authentication for authenticating realtime connections specifically.

    Edit: you pointed out that you are passing in a key as well. A client can't use both -- it either connects with the key, or it gets a token from the authUrl and connects with that -- so it's probably just ignoring one of them. Remove the one you don't want to use.

    Edit: I'd also suggest removing the options disabling Tls and changing the http timeouts, and leaving them at their defaults. Unless you have some particular reason for disabling tls, we strongly recommend leaving it enabled, for security reasons.