<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>ArkTS on Daniel Zhang</title><link>https://zhangdapeng.dev/tags/arkts/</link><description>Recent content in ArkTS on Daniel Zhang</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Tue, 24 Nov 2026 10:00:00 +0800</lastBuildDate><atom:link href="https://zhangdapeng.dev/tags/arkts/index.xml" rel="self" type="application/rss+xml"/><item><title>Managing RongCloud IM Connections on HarmonyOS: Fixing Error 34001</title><link>https://zhangdapeng.dev/p/rongcloud-harmonyos-im-connection-34001/</link><pubDate>Tue, 24 Nov 2026 10:00:00 +0800</pubDate><guid>https://zhangdapeng.dev/p/rongcloud-harmonyos-im-connection-34001/</guid><description>&lt;img src="https://zhangdapeng.dev/p/rongcloud-harmonyos-im-connection-34001/cover.png" alt="Featured image of post Managing RongCloud IM Connections on HarmonyOS: Fixing Error 34001" /&gt;&lt;h1 id="managing-rongcloud-im-connections-on-harmonyos-fixing-error-34001"&gt;Managing RongCloud IM Connections on HarmonyOS: Fixing Error 34001
&lt;/h1&gt;&lt;p&gt;When integrating the RongCloud IM SDK into a HarmonyOS application, you may encounter the following error:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;34001: RC_CONNECTION_EXIST
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This error indicates that an IM connection already exists or that the SDK is currently reconnecting.&lt;/p&gt;
&lt;p&gt;In most cases, the problem is caused by calling &lt;code&gt;connect()&lt;/code&gt; multiple times without checking the existing connection state.&lt;/p&gt;
&lt;p&gt;This article explains how to understand error 34001, when to call &lt;code&gt;disconnect()&lt;/code&gt;, and how to manage connection states correctly in a HarmonyOS application.&lt;/p&gt;
&lt;h2 id="1-understanding-error-34001"&gt;1. Understanding Error 34001
&lt;/h2&gt;&lt;p&gt;The error code &lt;code&gt;34001&lt;/code&gt; corresponds to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;RC_CONNECTION_EXIST
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It means that a connection has already been established or that a connection/reconnection process is in progress.&lt;/p&gt;
&lt;p&gt;Common scenarios include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Calling &lt;code&gt;connect()&lt;/code&gt; repeatedly during application initialization.&lt;/li&gt;
&lt;li&gt;Calling &lt;code&gt;connect()&lt;/code&gt; when returning from the background.&lt;/li&gt;
&lt;li&gt;Triggering multiple connection requests from different pages.&lt;/li&gt;
&lt;li&gt;Calling &lt;code&gt;connect()&lt;/code&gt; inside a connection status callback.&lt;/li&gt;
&lt;li&gt;Attempting to reconnect while the SDK is already handling automatic reconnection.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; This error does not necessarily mean the connection is broken. It may indicate that the SDK is already maintaining or establishing the connection.&lt;/p&gt;
&lt;p&gt;According to RongCloud&amp;rsquo;s documentation, an application should generally call &lt;code&gt;connect()&lt;/code&gt; once during its lifecycle rather than repeatedly initiating connections.&lt;/p&gt;
&lt;h2 id="2-how-to-connect-to-rongcloud-on-harmonyos"&gt;2. How to Connect to RongCloud on HarmonyOS
&lt;/h2&gt;&lt;p&gt;The HarmonyOS IM SDK provides the following connection interface:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;IMEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;token&lt;/span&gt;: &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;timeout&lt;/span&gt;: &lt;span class="kt"&gt;number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;IConnectResult&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Parameters:&lt;/p&gt;
&lt;table&gt;
	&lt;thead&gt;
			&lt;tr&gt;
					&lt;th&gt;Parameter&lt;/th&gt;
					&lt;th&gt;Description&lt;/th&gt;
			&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
			&lt;tr&gt;
					&lt;td&gt;token&lt;/td&gt;
					&lt;td&gt;IM authentication token obtained from your application server&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;timeout&lt;/td&gt;
					&lt;td&gt;Connection timeout in seconds&lt;/td&gt;
			&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;YOUR_IM_TOKEN&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;IMEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;EngineError&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;RongCloud connected successfully&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;EngineError&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ConnectTokenExpired&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;IM token expired&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// Request a new token from your application server.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;EngineError&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ConnectionTimeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Connection timed out&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// Handle the timeout according to your application logic.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Connection failed: &lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The timeout parameter controls how long the SDK attempts to establish a connection.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;timeout &amp;lt;= 0&lt;/code&gt;: The SDK continues attempting to connect until successful or a business-level error occurs.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;timeout &amp;gt; 0&lt;/code&gt;: The SDK attempts to connect within the specified period. If the connection times out, it stops retrying, and the application can initiate another connection attempt.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Once connected, the SDK handles automatic reconnection. You should not repeatedly call &lt;code&gt;connect()&lt;/code&gt; whenever the network changes or the application returns to the foreground.&lt;/p&gt;
&lt;h2 id="3-when-should-you-call-disconnect"&gt;3. When Should You Call disconnect()?
&lt;/h2&gt;&lt;p&gt;The HarmonyOS SDK provides the following interface:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;IMEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;disconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isReceivePush&lt;/span&gt;: &lt;span class="kt"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The parameter determines whether the user can receive remote push notifications after disconnecting.&lt;/p&gt;
&lt;table&gt;
	&lt;thead&gt;
			&lt;tr&gt;
					&lt;th&gt;Parameter&lt;/th&gt;
					&lt;th&gt;Description&lt;/th&gt;
			&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
			&lt;tr&gt;
					&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
					&lt;td&gt;Allow remote push notifications after disconnecting&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
					&lt;td&gt;Do not allow remote push notifications after disconnecting&lt;/td&gt;
			&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;IMEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;disconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The SDK documentation recommends using &lt;code&gt;disconnect()&lt;/code&gt; when the application needs to log out or switch users.&lt;/p&gt;
&lt;p&gt;It should not be called simply because the network connection is temporarily unavailable.&lt;/p&gt;
&lt;h3 id="31-user-logout"&gt;3.1 User Logout
&lt;/h3&gt;&lt;p&gt;When a user explicitly logs out, the application should disconnect the current IM connection.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;IMEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;disconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This prevents the disconnected account from receiving remote push notifications on the current device.&lt;/p&gt;
&lt;p&gt;The application should also clear its own authentication information and user session.&lt;/p&gt;
&lt;h3 id="32-switching-accounts"&gt;3.2 Switching Accounts
&lt;/h3&gt;&lt;p&gt;When switching from User A to User B, the application must avoid reusing the previous user&amp;rsquo;s connection.&lt;/p&gt;
&lt;p&gt;A recommended business flow is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Stop operations associated with the current user.&lt;/li&gt;
&lt;li&gt;Disconnect the current RongCloud IM connection.&lt;/li&gt;
&lt;li&gt;Update the application&amp;rsquo;s login session.&lt;/li&gt;
&lt;li&gt;Obtain the new user&amp;rsquo;s IM token from the application server.&lt;/li&gt;
&lt;li&gt;Establish the new user&amp;rsquo;s connection.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Conceptually:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// 1. Disconnect the current user.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;IMEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;disconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// 2. Update the application login session.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// 3. Request a new IM token from your backend.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// 4. Connect using the new token.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The HarmonyOS &lt;code&gt;disconnect()&lt;/code&gt; interface returns &lt;code&gt;void&lt;/code&gt;, not a Promise. Therefore, do not assume that calling &lt;code&gt;disconnect()&lt;/code&gt; means the disconnection has already completed synchronously.&lt;/p&gt;
&lt;p&gt;Coordinate account switching with the SDK&amp;rsquo;s connection-state notifications and ensure that the previous connection is no longer active before initiating the next login connection.&lt;/p&gt;
&lt;h3 id="33-temporary-network-disconnection"&gt;3.3 Temporary Network Disconnection
&lt;/h3&gt;&lt;p&gt;For ordinary network interruptions, you generally do not need to call &lt;code&gt;disconnect()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;RongCloud automatically handles reconnection when network conditions change.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Wi-Fi becomes unavailable.&lt;/li&gt;
&lt;li&gt;The device switches between Wi-Fi and mobile data.&lt;/li&gt;
&lt;li&gt;The application moves into the background.&lt;/li&gt;
&lt;li&gt;The application returns to the foreground.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In these situations, let the SDK manage the connection recovery process.&lt;/p&gt;
&lt;h2 id="4-understanding-automatic-reconnection"&gt;4. Understanding Automatic Reconnection
&lt;/h2&gt;&lt;p&gt;One important feature of RongCloud IM is its built-in reconnection mechanism.&lt;/p&gt;
&lt;p&gt;After a successful connection, the SDK takes responsibility for maintaining the connection.&lt;/p&gt;
&lt;p&gt;When a network interruption occurs, the SDK automatically attempts to reconnect.&lt;/p&gt;
&lt;p&gt;The application should therefore distinguish between two situations:&lt;/p&gt;
&lt;table&gt;
	&lt;thead&gt;
			&lt;tr&gt;
					&lt;th&gt;Scenario&lt;/th&gt;
					&lt;th&gt;SDK behavior&lt;/th&gt;
					&lt;th&gt;Recommended application behavior&lt;/th&gt;
			&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
			&lt;tr&gt;
					&lt;td&gt;Temporary network interruption&lt;/td&gt;
					&lt;td&gt;Automatically reconnects&lt;/td&gt;
					&lt;td&gt;Do not call &lt;code&gt;connect()&lt;/code&gt; manually&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;Application enters the background&lt;/td&gt;
					&lt;td&gt;Handles connection recovery&lt;/td&gt;
					&lt;td&gt;Do not disconnect unnecessarily&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;Application returns to the foreground&lt;/td&gt;
					&lt;td&gt;May already be connected or reconnecting&lt;/td&gt;
					&lt;td&gt;Check the connection state&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;User logs out&lt;/td&gt;
					&lt;td&gt;Automatic reconnection stops after disconnecting&lt;/td&gt;
					&lt;td&gt;Clear the login session&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;User switches accounts&lt;/td&gt;
					&lt;td&gt;Previous connection must be replaced&lt;/td&gt;
					&lt;td&gt;Coordinate disconnect and new login&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;Token expires&lt;/td&gt;
					&lt;td&gt;Connection cannot continue with an invalid token&lt;/td&gt;
					&lt;td&gt;Request a new token&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;User is kicked offline&lt;/td&gt;
					&lt;td&gt;Connection is terminated for a business reason&lt;/td&gt;
					&lt;td&gt;Notify the user and handle authentication&lt;/td&gt;
			&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;A particularly important distinction is that an ordinary network interruption is not the same as an explicit logout.&lt;/p&gt;
&lt;p&gt;Calling &lt;code&gt;disconnect()&lt;/code&gt; manually exits the SDK&amp;rsquo;s automatic reconnection mechanism.&lt;/p&gt;
&lt;h2 id="5-preventing-duplicate-connection-requests"&gt;5. Preventing Duplicate Connection Requests
&lt;/h2&gt;&lt;p&gt;To avoid error 34001, it is a good idea to manage connection requests centrally.&lt;/p&gt;
&lt;p&gt;Instead of allowing multiple pages or lifecycle callbacks to call &lt;code&gt;connect()&lt;/code&gt; independently, maintain a shared connection manager.&lt;/p&gt;
&lt;p&gt;A simple state model could look like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;enum&lt;/span&gt; &lt;span class="nx"&gt;IMConnectionState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;IDLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;CONNECTING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;CONNECTED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;DISCONNECTING&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The application can use these states to prevent duplicate operations.&lt;/p&gt;
&lt;p&gt;For example, the connection manager should follow these rules:&lt;/p&gt;
&lt;table&gt;
	&lt;thead&gt;
			&lt;tr&gt;
					&lt;th&gt;Current state&lt;/th&gt;
					&lt;th&gt;Requested operation&lt;/th&gt;
					&lt;th&gt;Action&lt;/th&gt;
			&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
			&lt;tr&gt;
					&lt;td&gt;IDLE&lt;/td&gt;
					&lt;td&gt;Connect&lt;/td&gt;
					&lt;td&gt;Start connection&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;CONNECTING&lt;/td&gt;
					&lt;td&gt;Connect&lt;/td&gt;
					&lt;td&gt;Ignore duplicate request&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;CONNECTED&lt;/td&gt;
					&lt;td&gt;Connect&lt;/td&gt;
					&lt;td&gt;Do not reconnect&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;DISCONNECTING&lt;/td&gt;
					&lt;td&gt;Connect&lt;/td&gt;
					&lt;td&gt;Wait for disconnection handling&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;CONNECTED&lt;/td&gt;
					&lt;td&gt;Disconnect&lt;/td&gt;
					&lt;td&gt;Start disconnection&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;IDLE&lt;/td&gt;
					&lt;td&gt;Disconnect&lt;/td&gt;
					&lt;td&gt;No operation needed&lt;/td&gt;
			&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The important principle is to ensure that only one part of the application is responsible for managing the IM connection.&lt;/p&gt;
&lt;p&gt;A practical implementation should also:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Register connection-state listeners before connecting.&lt;/li&gt;
&lt;li&gt;Prevent multiple simultaneous connection requests.&lt;/li&gt;
&lt;li&gt;Handle connection results consistently.&lt;/li&gt;
&lt;li&gt;Distinguish SDK automatic reconnection from application-initiated reconnection.&lt;/li&gt;
&lt;li&gt;Avoid calling &lt;code&gt;connect()&lt;/code&gt; directly from every page&amp;rsquo;s lifecycle callback.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="6-handling-connection-status-changes"&gt;6. Handling Connection Status Changes
&lt;/h2&gt;&lt;p&gt;RongCloud provides connection-state listeners for monitoring changes in the IM connection.&lt;/p&gt;
&lt;p&gt;These listeners are useful for updating the application&amp;rsquo;s UI and handling exceptional connection states.&lt;/p&gt;
&lt;p&gt;A recommended handling strategy is:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Temporary network disconnection&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Update the UI to indicate that the connection is recovering.&lt;/li&gt;
&lt;li&gt;Allow the SDK to reconnect automatically.&lt;/li&gt;
&lt;li&gt;Do not call &lt;code&gt;connect()&lt;/code&gt; again.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Successful reconnection&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Update the connection state to connected.&lt;/li&gt;
&lt;li&gt;Restore normal messaging functionality.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Token expiration&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Request a new IM token from your application server.&lt;/li&gt;
&lt;li&gt;Establish a new connection according to the SDK&amp;rsquo;s connection-state flow.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Being kicked offline&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Notify the user that the account has been logged in on another device.&lt;/li&gt;
&lt;li&gt;Clear or invalidate the current application login session as appropriate.&lt;/li&gt;
&lt;li&gt;Do not automatically reconnect with the old session.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Manual disconnection&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Keep the application in a disconnected state.&lt;/li&gt;
&lt;li&gt;Only reconnect when a new login or another explicit business operation requires it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Avoid treating every disconnection notification as a signal to immediately call &lt;code&gt;connect()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Otherwise, the application may repeatedly trigger connection requests while the SDK is already processing a reconnection.&lt;/p&gt;
&lt;h2 id="7-common-mistakes-to-avoid"&gt;7. Common Mistakes to Avoid
&lt;/h2&gt;&lt;p&gt;Here are several common mistakes when integrating RongCloud IM into a HarmonyOS application.&lt;/p&gt;
&lt;h3 id="mistake-1-calling-disconnect-before-every-connect"&gt;Mistake 1: Calling disconnect() Before Every connect()
&lt;/h3&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;IMEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;disconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;IMEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is not a recommended general-purpose solution.&lt;/p&gt;
&lt;p&gt;Calling &lt;code&gt;disconnect()&lt;/code&gt; before every connection attempt can interrupt an existing connection and interfere with the SDK&amp;rsquo;s automatic reconnection mechanism.&lt;/p&gt;
&lt;p&gt;Only disconnect when the business logic explicitly requires it, such as logout or account switching.&lt;/p&gt;
&lt;h3 id="mistake-2-reconnecting-whenever-the-application-returns-to-the-foreground"&gt;Mistake 2: Reconnecting Whenever the Application Returns to the Foreground
&lt;/h3&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;onForeground() {&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;IMEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This can trigger error 34001 because the SDK may already be connected or reconnecting.&lt;/p&gt;
&lt;p&gt;The application lifecycle should not be treated as an unconditional signal to establish a new IM connection.&lt;/p&gt;
&lt;h3 id="mistake-3-calling-connect-from-multiple-pages"&gt;Mistake 3: Calling connect() from Multiple Pages
&lt;/h3&gt;&lt;p&gt;If several pages independently initialize RongCloud, they may all attempt to establish a connection.&lt;/p&gt;
&lt;p&gt;A better architecture is to initialize and manage the IM connection in a centralized application-level service.&lt;/p&gt;
&lt;h3 id="mistake-4-automatically-reconnecting-after-every-disconnect-event"&gt;Mistake 4: Automatically Reconnecting After Every Disconnect Event
&lt;/h3&gt;&lt;p&gt;Not all disconnection events should trigger a new connection.&lt;/p&gt;
&lt;p&gt;For example, a manual logout or an account kicked offline should not be handled in the same way as a temporary network interruption.&lt;/p&gt;
&lt;p&gt;Always identify the reason for the connection-state change before deciding what to do.&lt;/p&gt;
&lt;h2 id="8-recommended-connection-management-strategy"&gt;8. Recommended Connection Management Strategy
&lt;/h2&gt;&lt;p&gt;For a HarmonyOS application using RongCloud IM, the recommended approach is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Initialize the RongCloud SDK once.&lt;/li&gt;
&lt;li&gt;Register connection-state listeners.&lt;/li&gt;
&lt;li&gt;Obtain the IM token from your application backend.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;connect()&lt;/code&gt; when the user logs in.&lt;/li&gt;
&lt;li&gt;Let the SDK handle ordinary network interruptions and automatic reconnection.&lt;/li&gt;
&lt;li&gt;Avoid repeated connection requests during application lifecycle changes.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;disconnect(false)&lt;/code&gt; when the user logs out or switches accounts and remote push should be disabled.&lt;/li&gt;
&lt;li&gt;Coordinate the next connection with the previous connection&amp;rsquo;s state.&lt;/li&gt;
&lt;li&gt;Handle token expiration and forced logout as separate business scenarios.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The most important rule is:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Do not use disconnect() and connect() as a workaround for error 34001. Manage the connection state correctly and let RongCloud handle automatic reconnection.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion
&lt;/h2&gt;&lt;p&gt;RongCloud error &lt;code&gt;34001 (RC_CONNECTION_EXIST)&lt;/code&gt; usually indicates that an IM connection already exists or that the SDK is currently reconnecting.&lt;/p&gt;
&lt;p&gt;For HarmonyOS applications, the correct approach is to distinguish between normal network interruptions and explicit business operations such as logout or account switching.&lt;/p&gt;
&lt;p&gt;Use &lt;code&gt;disconnect()&lt;/code&gt; only when the application genuinely needs to terminate the current IM session. For ordinary network changes and foreground/background transitions, let the SDK handle reconnection.&lt;/p&gt;
&lt;p&gt;Centralizing connection management and preventing duplicate &lt;code&gt;connect()&lt;/code&gt; calls will make the IM integration more reliable and help avoid unnecessary connection errors.&lt;/p&gt;
&lt;h2 id="references"&gt;References
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.rongcloud.cn/harmonyOS-imlib/connection/connect" target="_blank" rel="noopener"
 &gt;RongCloud HarmonyOS IM SDK - Connection&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.rongcloud.cn/harmonyOS-imlib/connection/disconnect" target="_blank" rel="noopener"
 &gt;RongCloud HarmonyOS IM SDK - Disconnect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.rongcloud.cn/" target="_blank" rel="noopener"
 &gt;RongCloud Developer Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>