<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Websocket on Aditya Naik | Personal Website</title>
    <link>https://d6cbd428.hugo-blog-e6x.pages.dev/tags/websocket/</link>
    <description>Recent content in Websocket on Aditya Naik | Personal Website</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Tue, 12 May 2020 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://d6cbd428.hugo-blog-e6x.pages.dev/tags/websocket/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>How to test your react websocket implementation with cypress</title>
      <link>https://d6cbd428.hugo-blog-e6x.pages.dev/blog/2020/05/12/how-to-test-your-react-websocket-implementation-with-cypress/</link>
      <pubDate>Tue, 12 May 2020 00:00:00 +0000</pubDate>
      <guid>https://d6cbd428.hugo-blog-e6x.pages.dev/blog/2020/05/12/how-to-test-your-react-websocket-implementation-with-cypress/</guid>
      <description>&lt;h3 id=&#34;set-up-demo-application&#34;&gt;Set up demo application&lt;/h3&gt;&#xA;&lt;p&gt;I scaffold a standard react application&lt;/p&gt;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&lt;figure class=&#34;highlight&#34;&gt;&#xA;  &#xA;  &lt;pre class=&#34;language-bash&#34; &gt;&lt;code&gt;$ npx create-react-app ws-demo&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;/figure&gt;&#xA;&lt;p&gt;Clean up and update &lt;code&gt;app.js&lt;/code&gt; to set up a websocket client.&lt;/p&gt;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&lt;figure class=&#34;highlight&#34;&gt;&#xA;  &lt;figcaption&gt;app.js&lt;/figcaption&gt;&#xA;  &lt;pre class=&#34;language-js&#34; data-line=&#34;18-34&#34;&gt;&lt;code&gt;import React, { useState } from &#34;react&#34;;&#xA;&#xA;const App = () =&gt; {&#xA;  const [message, setMessage] = useState(&#34;websocket is closed&#34;);&#xA;&#xA;  return (&#xA;    &lt;div className=&#34;App&#34;&gt;&#xA;      &lt;p id=&#34;websocket&#34;&gt;{message}&lt;/p&gt;&#xA;      &lt;WebsocketHandler setMessage={setMessage} /&gt;&#xA;    &lt;/div&gt;&#xA;  );&#xA;};&#xA;&#xA;export default App;&#xA;&#xA;// WebsocketHandler does what the name suggests - launch/close websocket client and receive messages&#xA;&#xA;const websocketUrl = &#34;ws://127.0.0.1:5000&#34;;&#xA;const WebsocketHandler = ({ setMessage }) =&gt; {&#xA;  const ws = new WebSocket(websocketUrl);&#xA;  ws.onopen = () =&gt; {&#xA;    console.log(&#34;conn open&#34;);&#xA;    ws.send(&#34;connection is open&#34;);&#xA;  };&#xA;  ws.onmessage = (message) =&gt; {&#xA;    setMessage(message.data);&#xA;    console.log(&#34;onmessage triggered&#34;, message);&#xA;    ws.send(&#34;message received&#34;);&#xA;  };&#xA;  ws.onclose = () =&gt; {&#xA;    console.log(&#34;connection closed&#34;);&#xA;  };&#xA;  return null;&#xA;};&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;/figure&gt;&#xA;&lt;p&gt;As highlighted, I have a &lt;code&gt;WebsocketHandler&lt;/code&gt; component which handles websocket and sets state in &lt;code&gt;app.js&lt;/code&gt;&#xA;when it receives the message.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
