Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I close the connection, it doesn't send queryStringParameters #56

Closed
kyun opened this issue Dec 28, 2019 · 1 comment
Closed

When I close the connection, it doesn't send queryStringParameters #56

kyun opened this issue Dec 28, 2019 · 1 comment

Comments

@kyun
Copy link

kyun commented Dec 28, 2019

I built up the WebSocket server with AWS API Gateway.

and, I am connecting this with AWS Lambda.

It works well when I connect, but when I disconnect, it doesn't work well.

My React Client Code

import React from 'react';

function socketHandler(uid){
  const wss = new Sockette(`wss://MY_API_GATEWAY_URL.execute-api.ap-northeast-2.amazonaws.com/dev?groupId=GROUP_ID&uid=${uid}`, {
    timeout: 1000,
    maxAttempts: 10,
    onopen: (e: any) => { console.log(e); console.log('Websocket is connected') },
    onmessage: (e: any) => {
      console.log(e.data);
    },
    onclose: (e: any) => { console.log(e); console.log('Websocket is disconnected'); }
  });
  return wss;
}

const SocketForm = (props) => {
  const [socket, setSocket] = React.useState(null);
  
  React.useEffect(()=> {
    setSocket(socketHandler(`MY_UID`);
  }, []);
  const socketDisconnect = () => {
    socket.close();
  }
  return (
    <div> 
      <button onClick={socketDisconnect}>Disconnect</button>
    </div>
  )
}

My Lambda Code

 /* it works well when I connect. */
export const socketHandler = async (evt) => {
  const {groupId, uid} = evt.queryStringParameters; // When I disconnect it occurs ERROR.
 
   /* Skip Saving connectionId */

   return { statusCode: 200, body: JSON.stringify({}) };
};

I'd like to use WebSocket URL with queryParameters even I disconnect.

If I missed something, please reply me.

@lukeed
Copy link
Owner

lukeed commented Dec 29, 2019

Hey,

I think you may be missing something with your AWS Lambda code. Generally, disconnect happens in a separate handler, but I'm not sure what exactly you're doing.

In any event, on the client-side, what Sockette does is exactly the same as the following code:

function socketHandler(uid){
  const url = `wss://MY_API_GATEWAY_URL.execute-api.ap-northeast-2.amazonaws.com/dev?groupId=GROUP_ID&uid=${uid}`;
  return new WebSocket(url);
}

const wss = socketHandler('123');

setTimeout(() => {
  // some time later...
  wss.close(1000);
}, 3000);

Sockette doesn't add/change anything in this process; see here

If the above still fails for you, either your Lambda code needs adjusting or the browser API doesn't do what you're expecting – which, IMO, still means that your Lambda needs adjusting.

Hope that helps! Keep me posted

@lukeed lukeed closed this as completed Dec 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants