3rdSenna
3rdSenna

Reputation: 376

AWS Websocket Connection response

I'm quiet new with AWS Websockets. I hope my query will make sense. So, I've manage my "chat" app works. I can connect, send messages and disconnect with no issues. I'm trying to improve the reply from Connect AWS Apigateway. I want to see "myData" on client side. It will be changed to a list of object in the future. Thanks in advance

serveless.yml

  socketConnection:
    handler: ${self:custom.pathFunc}/socketOption.socketConnection
    events:
      - websocket:
          route: $connect
      - websocket:
          route: $disconnect

socketOptions.js

const socketConnection = ( event, context, callback ) => {
  if ( event.requestContext.eventType === 'CONNECT' ) {
    addConnection( event.requestContext.connectionId )
    .then( () => {
      callback( null, {
        statusCode: 200,
        body: {
         myData: "here goes any data I want"
        }
      } );
    } )
  } else if ( event.requestContext.eventType === 'DISCONNECT' ) {
  //  ...
  }
};
module.exports.socketConnection = socketConnection

myComponent.vue

connect(){
  this.connection = new WebSocket(this.wss)

  this.connection.onopen = this.onOpen
  this.connection.onmessage = this.onMessage
  this.connection.onclose = this.onClose
  this.connection.onerror = this.onError
},
onOpen(event){
  console.log("onOpen -> event", event)
}

enter image description here

Upvotes: 0

Views: 551

Answers (1)

warrens
warrens

Reputation: 2135

I found info on the AWS forums related to this. An AWS poster "Alan-AWS" said:

That's right, the $connect route corresponds to the connection upgrade request which doesn't have a message.

Which I take to mean that no data can be returned to the client amid the connect processing.

However, I have been able to set a few values in the "context" of my authorization lambda function, and those values are passed thru to each event that the route lambda receives -- which can then be sent in a client response I suppose.

I have also been able to "push" a message thru to the client using the special "@connections" approach.

Upvotes: 1

Related Questions