57 Matching Annotations
  1. Oct 2023
  2. Sep 2023
  3. Aug 2023
  4. Jul 2023
    1. That is because React.StrictMode renders the component twice (as it is intentional), that causes 2 connections get created. Create a ws connection inside useEffect and close on the effect cleanup would help or remove StrictMode from index file.

      It's not a bug, it's a feature - they say.

      React DX is so special...

    1. WebSocket support in Pub/Sub works by encapsulating MQTT packets (Pub/Sub’s underlying native protocol) within WebSocket framesExternal link icon

      ```js // Ensure MQTT.js is installed first // > npm install mqtt import * as mqtt from "mqtt"

      // Where 'url' is "mqtts://BROKER.NAMESPACE.cloudflarepubsub.com:8884" function example(url) { let client = mqtt.connect(url, { protocolVersion: 5, reconnectPeriod: 0, username: 'anything', password: jwt, // pass this from a form field in your app clientId: '', })

      client.on('connect', function () { client.subscribe(topic, function (err) { if (err) { client.end(); } else { console.log(subscribed to ${topic}) } })

      client.on('message', function (topic, message) { let line = (new Date()).toLocaleString('en-US') + ": " + message.toString() + "\n"; console.log(line) }) } ```

    1. ```js exports.handler = async (event) => { if (event.headers != undefined) { const headers = toLowerCaseProperties(event.headers);

          if (headers['sec-websocket-protocol'] != undefined) {
              const subprotocolHeader = headers['sec-websocket-protocol'];
              const subprotocols = subprotocolHeader.split(',');
      
              if (subprotocols.indexOf('myprotocol') >= 0) {
                  const response = {
                      statusCode: 200,
                      headers: {
                          "Sec-WebSocket-Protocol" : "myprotocol"
                      }
                  };
                  return response;
              }
          }
      }
      
      const response = {
          statusCode: 400
      };
      
      return response;
      

      };function toLowerCaseProperties(obj) { var wrapper = {}; for (var key in obj) { wrapper[key.toLowerCase()] = obj[key]; } return wrapper; } ```

    1. The Hibernation API allows a Durable Object that is not currently running an event handler, such as handling a WebSocket message, HTTP request, or alarm, to be removed from memory while keeping its WebSockets connected (“hibernation”).
  5. Apr 2023
  6. Mar 2023
  7. Feb 2023
    1. js const wss = new WebSocketStream(url); const { readable } = await wss.connection; const reader = readable.getReader(); while (true) { const { value, done } = await reader.read(); if (done) break; await process(value); } done();


      js const wss = new WebSocketStream(url); const { writable } = await wss.connection; const writer = writable.getWriter(); for await (const message of messages) { await writer.write(message); }


      js const controller = new AbortController(); const wss = new WebSocketStream(url, { signal: controller.signal }); setTimeout(() => controller.abort(), 1000);

  8. Jan 2023
  9. Dec 2022
  10. Jun 2022
  11. Apr 2022
  12. Mar 2022
  13. Dec 2021
    1. Increasing StaleTime

      React Query comes with a default staleTime of zero. This means that every query will be immediately considered as stale, which means it will refetch when a new subscriber mounts or when the user refocuses the window. It is aimed to keep your data as up-to-date as necessary.

      This goal overlaps a lot with WebSockets, which update your data in real-time. Why would I need to refetch at all if I just manually invalidated because the server just told me to do so via a dedicated message?

      So if you update all your data via websockets anyways, consider setting a high staleTime. In my example, I just used Infinity. This means the data will be fetched initially via useQuery, and then always come from the cache. Refetching only happens via the explicit query invalidation.

      You can best achieve this by setting global query defaults when creating the QueryClient:

      const queryClient = new QueryClient({
        defaultOptions: {
          queries: {
            staleTime: Infinity,
          },
        },
      })
      
  14. Jul 2021
  15. datatracker.ietf.org datatracker.ietf.org
    1. Relationship to TCP and HTTP _This section is non-normative._ The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request. By default, the WebSocket Protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over Transport Layer Security (TLS) [RFC2818].
  16. Jul 2020
    1. Listens to changes in a PostgreSQL Database and broadcasts them over WebSocket. It works like this:the Phoenix server listens to PostgreSQL's replication functionality (using Postgres' logical decoding).it converts the byte stream into JSON.it then broadcasts over WebSocket.