<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Barry Martin</title>
    <description>The latest articles on DEV Community by Barry Martin (@barry_martin_de1dbd9d83cd).</description>
    <link>https://dev.to/barry_martin_de1dbd9d83cd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2724048%2F0a9a61a9-4224-43a0-9e7d-29da02f1d19b.png</url>
      <title>DEV Community: Barry Martin</title>
      <link>https://dev.to/barry_martin_de1dbd9d83cd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/barry_martin_de1dbd9d83cd"/>
    <language>en</language>
    <item>
      <title>Dynamically updating websocket URL with useEffect</title>
      <dc:creator>Barry Martin</dc:creator>
      <pubDate>Thu, 16 Jan 2025 14:30:38 +0000</pubDate>
      <link>https://dev.to/barry_martin_de1dbd9d83cd/dynamically-updating-websocket-url-with-useeffect-130j</link>
      <guid>https://dev.to/barry_martin_de1dbd9d83cd/dynamically-updating-websocket-url-with-useeffect-130j</guid>
      <description>&lt;p&gt;I have a react app that uses web sockets. When the page loads I need to run an async function that retrieves the MDNS name of the unit. I then am trying to use this MDNS name as part of the web socket URL connection. I'm not an expert so I'm wondering if all the useEffects run concurrently or one after the other. Any suggestions as to how to solve this would be appreciated. The getMDNS and getUserNames work correctly, I'm just getting the error in the browser log window:&lt;/p&gt;

&lt;p&gt;index-EUpgzpwz.js:40 SyntaxError: Failed to construct 'WebSocket': The URL 'ws://[object Object].local/ws' is invalid.&lt;/p&gt;

&lt;p&gt;The code is below. Any help would be appreciated.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";
export default function DataTables() {
  const [tc1user, setTc1User] = useState&amp;lt;any&amp;gt;("TC1 User Name");
  const [tc2user, setTc2User] = useState&amp;lt;any&amp;gt;("TC2 User Name");
  const [tc1, setTc1] = useState&amp;lt;number&amp;gt;(0);
  const [tc2, setTc2] = useState&amp;lt;number&amp;gt;(0);
  const [mdns, setMdns] = useState("");

  useEffect(() =&amp;gt; {
    const getMdns = async () =&amp;gt; {
      // setIsLoading(true);
      try {
        const webResult = await fetch("/api/get-mdns");
        const myText = await webResult.text();
        const mdnsjson = JSON.parse(myText);
        setMdns(mdnsjson.mdns);
        // console.log(mdnsjson.mdns);
      } catch (error) {
        console.log("Error fetching mdns", error);
      } finally {
        // setIsLoading(false);
      }
    }
    getMdns();
  }, []);

  useEffect(() =&amp;gt; {
    const getUserNames = async () =&amp;gt; {
      try {
        const webResult = await fetch("/api/get-user");
        const myText = await webResult.text();
        const userjson = JSON.parse(myText);
        setTc1User(userjson[0].role);
        setTc2User(userjson[1].role);
      } catch (error) {
        console.log("Error fetching data", error);
      } finally {
      }
    }
    getUserNames();
  }, []);

  useEffect(() =&amp;gt; {
    let webmdns = mdns;
    webmdns = "ws://" + webmdns + ".local/ws"
    const ws = new WebSocket(webmdns); // Connect to websocket using mdns name currently in use

    ws.onopen = () =&amp;gt; {
      console.log('WebSocket connected');
    };
    ws.onmessage = (event) =&amp;gt; {
      const data = JSON.parse(event.data);
      setTc1(data.TC1);
      setTc2(data.TC2);
    };
    ws.onerror = (error) =&amp;gt; {
      console.error('WebSocket error:', error);
    };
    return () =&amp;gt; {
      ws.close();
      console.log('WebSocket disconnected');
    };
  }, []);

  return (
    &amp;lt;&amp;gt;
    // Do stuff here
    &amp;lt;/&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>websocket</category>
    </item>
  </channel>
</rss>
