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

Websocket support #674

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c4811ca
demo websoket version
computerpunc May 8, 2019
e32b9fa
Added support for queryStringParameters
computerpunc May 14, 2019
1ea68e0
Bug fixes
computerpunc May 21, 2019
53e9eef
Run WebSocket endpoint on a different port (http port+1)
computerpunc May 23, 2019
602ac7b
GWAPI REST API suppoprt
computerpunc May 26, 2019
c4c6ea6
Changed the way to get function to post-to-connection
computerpunc May 27, 2019
10c377d
Default timeout is 1000ms and can set timeout value via --timeout=
computerpunc May 27, 2019
bca81cd
Minor Improved logs
computerpunc May 27, 2019
da8c749
Update README with new ways of sending messages to clients
computerpunc May 27, 2019
fe4a51d
Merge from upstream/master
computerpunc May 28, 2019
f527670
Leftovers from merge
computerpunc May 28, 2019
4ec82ad
Fix lint errors
computerpunc May 28, 2019
4e61aa5
Fix static AWS = { ... }
computerpunc May 28, 2019
289e8f5
Fix 2 lint errors
computerpunc May 28, 2019
ca34ccb
Removed require('aws-sdk/clients/apigatewaymanagementapi');
computerpunc May 29, 2019
2f3dfbd
Removed the need for require('serverless-offline').AWS
computerpunc May 30, 2019
f9600f8
Last commit leftovers
computerpunc May 30, 2019
f3cf815
Added context and event when calling the handler.
computerpunc Jun 1, 2019
4521b88
Added context and event for connect and disconnect
computerpunc Jun 3, 2019
96dbd16
Merge from master: hapi@18 & hapi-plugin-websocket@2
computerpunc Jun 4, 2019
1e559b7
hapi@18 and hapi-plugin-websocket@2 support
computerpunc Jun 4, 2019
1ea590e
Merge branch 'master' of https://github.com/dherault/serverless-offli…
computerpunc Jun 4, 2019
6638b73
Merge from master and fixes
computerpunc Jun 4, 2019
b51ddb5
added support for callback() in handler
computerpunc Jun 5, 2019
381355b
Moved websocket CreateXXX to websocketHelpers.js
computerpunc Jun 6, 2019
d45ea7a
Restructure manual_test_websocket to include more projects
computerpunc Jun 6, 2019
66ffe6a
Added serverless.yml with warning
computerpunc Jun 6, 2019
02ce0e7
Support for websocketsApiRouteSelectionExpression
computerpunc Jun 11, 2019
3759b06
Fix lint errors
computerpunc Jun 11, 2019
72a1fc4
Merge branch 'master' of https://github.com/dherault/serverless-offli…
computerpunc Jun 11, 2019
687f901
Merge leftovers
computerpunc Jun 11, 2019
b9b0eef
Update README
computerpunc Jun 11, 2019
b6c1095
Update README about WebSocket
computerpunc Jun 11, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitignore
Expand Up @@ -104,6 +104,7 @@ fabric.properties
# auto-generated tag files
tags
=======

.idea/
manual_test/.serverless

.idea/
.serverless
.dynamodb
24 changes: 10 additions & 14 deletions manual_test_websocket/README.md
Expand Up @@ -26,28 +26,24 @@ To start AWS DynamoDB locally (can run only after first deploying locally): `sls

## Testing on AWS

`npm --endpoint={WebSocket endpoint URL on AWS} run test`
`npm --endpoint={WebSocket endpoint URL on AWS} --timeout={timeout in ms} run test`


## Usage Assumption - In order to send messages back to clients
`const newAWSApiGatewayManagementApi=(event, context)=>{`
## Usage in order to send messages back to clients

`POST http://localhost:3001/@connections/{connectionId}`

` const endpoint=event.requestContext.domainName+'/'+event.requestContext.stage;`
Or,

` const apiVersion='2018-11-29';`
`let endpoint=event.apiGatewayUrl;`

` let API=context.API;`
`if (!endpoint) endpoint = event.requestContext.domainName+'/'+event.requestContext.stage;`

` if (!process.env.IS_OFFLINE) {`
`const apiVersion='2018-11-29';`

` API = require('aws-sdk');`
`const apiGM=new API.ApiGatewayManagementApi({ apiVersion, endpoint });`

` require('aws-sdk/clients/apigatewaymanagementapi');`
`apiGM.postToConnection({ConnectionId, Data});`

` }`

` return new API.ApiGatewayManagementApi({ apiVersion, endpoint });`

`};`


35 changes: 26 additions & 9 deletions manual_test_websocket/handler.js
Expand Up @@ -17,15 +17,28 @@ const errorResponse = {
body: 'Request is not OK.'
};

// module.exports.http = async (event, context) => {
// return successfullResponse;
// };

module.exports.connect = async (event, context) => {
// console.log('connect:');
const listener=await ddb.get({TableName:'listeners', Key:{name:'default'}}).promise();
if (listener.Item) await sendToClient(JSON.stringify({action:'update', event:'connect', info:{id:event.requestContext.connectionId}}), listener.Item.id, newAWSApiGatewayManagementApi(event, context)).catch(()=>{});

if (listener.Item) {
const timeout=new Promise((resolve) => setTimeout(resolve,100));
const send=sendToClient( // sendToClient won't return on AWS when client doesn't exits so we set a timeout
JSON.stringify({action:'update', event:'connect', info:{id:event.requestContext.connectionId, event:{...event, apiGatewayUrl:`${event.apiGatewayUrl}`}, context}}),
listener.Item.id,
newAWSApiGatewayManagementApi(event, context)).catch(()=>{});
await Promise.race([send, timeout]);
}
return successfullResponse;
};

module.exports.disconnect = async (event, context) => {
const listener=await ddb.get({TableName:'listeners', Key:{name:'default'}}).promise();
if (listener.Item) await sendToClient(JSON.stringify({action:'update', event:'disconnect', info:{id:event.requestContext.connectionId}}), listener.Item.id, newAWSApiGatewayManagementApi(event, context)).catch(()=>{});
if (listener.Item) await sendToClient(JSON.stringify({action:'update', event:'disconnect', info:{id:event.requestContext.connectionId, event:{...event, apiGatewayUrl:`${event.apiGatewayUrl}`}, context}}), listener.Item.id, newAWSApiGatewayManagementApi(event, context)).catch(()=>{});
return successfullResponse;
};

Expand All @@ -35,10 +48,16 @@ module.exports.defaultHandler = async (event, context) => {
};

module.exports.getClientInfo = async (event, context) => {
// console.log('getClientInfo:');
await sendToClient({action:'update', event:'client-info', info:{id:event.requestContext.connectionId}}, event.requestContext.connectionId, newAWSApiGatewayManagementApi(event, context)).catch(err=>console.log(err));
return successfullResponse;
};

module.exports.getCallInfo = async (event, context) => {
await sendToClient({action:'update', event:'call-info', info:{event:{...event, apiGatewayUrl:`${event.apiGatewayUrl}`}, context}}, event.requestContext.connectionId, newAWSApiGatewayManagementApi(event, context)).catch(err=>console.log(err));
return successfullResponse;
};

module.exports.makeError = async (event, context) => {
const obj=null;
obj.non.non=1;
Expand Down Expand Up @@ -81,17 +100,15 @@ module.exports.deleteListener = async (event, context) => {
};

const newAWSApiGatewayManagementApi=(event, context)=>{
const endpoint=event.requestContext.domainName+'/'+event.requestContext.stage;
let endpoint=event.apiGatewayUrl;

if (!endpoint) endpoint = event.requestContext.domainName+'/'+event.requestContext.stage;
const apiVersion='2018-11-29';
let API=context.API;
if (!process.env.IS_OFFLINE) {
API = require('aws-sdk');
require('aws-sdk/clients/apigatewaymanagementapi');
}
return new API.ApiGatewayManagementApi({ apiVersion, endpoint });
return new AWS.ApiGatewayManagementApi({ apiVersion, endpoint });
};

const sendToClient = (data, connectionId, apigwManagementApi) => {
// console.log(`sendToClient:${connectionId}`);
let sendee=data;
if ('object'==typeof data) sendee=JSON.stringify(data);

Expand Down