title: How to get the "lastEventId" from fastapi websocket answer?
body: <p>I want to get the &quot;lastEventId&quot; from a websocket answer from fastapi. When inspecting the code in the browser I see that the returned object has a field &quot;lastEventId&quot; but unfortunately it is empty. Does anyone know how to get this variable?
  (The code is derived from the example code on fastapi webpage and should run under the premise <code>pip install websocket</code>)</p>
  <pre><code>from fastapi import FastAPI, WebSocket
  from fastapi.responses import HTMLResponse
  import re
  from icecream import ic
  
  app = FastAPI()
  
  
  html = &quot;&quot;&quot;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;A Form&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;WebSocket Form&lt;/h1&gt;
          &lt;form name=&quot;testform&quot; action=&quot;&quot; onsubmit=&quot;&quot; onKeyUp=&quot;sendMessage(event)&quot;&gt;
              &lt;input type=&quot;text&quot; id=&quot;messageText&quot; autocomplete=&quot;off&quot; placeholder=&quot;text&quot; /&gt;
              &lt;span id=&quot;texterror&quot;&gt;&lt;/span&gt;
              &lt;button&gt;Send&lt;/button&gt;
          &lt;/form&gt;
          
          &lt;script&gt;
              var ws = new WebSocket(&quot;ws://localhost:8000/ws&quot;);
              ws.onmessage = function(event) {
                  console.log(&quot;answer from fastapi websocket: &quot;,event)
                  //the following variable is always null
                  var field = document.getElementById(event.lastEventId)
                  console.log(&quot;the find the last element: &quot;, field);
              };
  
              function sendMessage(event) {
  
                  // event.srcElemt.id because the final form shall have more inputs
                  var input = document.getElementById(event.srcElement.id);
                  console.log(&quot;value of following element to fastapi websocket: &quot;,input);
                  ws.send(input.value)
              }
  
          &lt;/script&gt;
  
      &lt;/body&gt;
  
  &lt;/html&gt;
  
  &quot;&quot;&quot;
  
  
  
  @app.get(&quot;/&quot;)
  
  async def get():
      return HTMLResponse(html)
  
  
  
  @app.websocket(&quot;/ws&quot;)
  async def websocket_endpoint(websocket: WebSocket):
      await websocket.accept()
      while True:
          ic(websocket.__dict__)
          data = await websocket.receive_text()
          print('&quot;'+data+'&quot;')
          await websocket.send_text(data)
  </code></pre>