Skip to content

Instantly share code, notes, and snippets.

View robzhu's full-sized avatar

Robert Zhu robzhu

View GitHub Profile
@robzhu
robzhu / client.js
Created January 4, 2019 23:04
WebSocket client.js
const WebSocket = require('ws');
const readline = require('readline');
const url = process.argv[2];
const ws = new WebSocket(url);
ws.on('open', () => console.log('connected'));
ws.on('message', data => console.log(`From server: ${data}`));
ws.on('close', () => {
console.log('disconnected');
@robzhu
robzhu / server.js
Created January 4, 2019 23:18
Local WebSocket Echo server
const WebSocket = require('ws');
const wss = new WebSocket.Server({port: 8080});
wss.on('connection', socket => {
socket.on('message', data => {
socket.send(data);
});
});
console.log(`Listening on ws://localhost:8080`);
@robzhu
robzhu / serverWithActions.js
Last active January 7, 2019 23:22
WS server refactored to split execution logic from state
const WebSocket = require('ws');
const short = require('short-uuid');
const connections = {};
const send = (connectionId, data) => {
const connection = connections[connectionId];
connection.send(data);
}
const defaultActions = {
@robzhu
robzhu / patch.js
Created January 7, 2019 22:37
aws-sdk patch for WebSocket API
require('aws-sdk/lib/node_loader');
var AWS = require('aws-sdk/lib/core');
var Service = AWS.Service;
var apiLoader = AWS.apiLoader;
apiLoader.services['apigatewaymanagementapi'] = {};
AWS.ApiGatewayManagementApi = Service.defineService('apigatewaymanagementapi', ['2018-11-29']);
Object.defineProperty(apiLoader.services['apigatewaymanagementapi'], '2018-11-29', {
get: function get() {
var model = {
@robzhu
robzhu / index.js
Created January 7, 2019 22:56
Lambda code for the echo route
const AWS = require('aws-sdk');
// apply the patch
require('./patch.js');
let send = undefined;
function init(event) {
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
endpoint: event.requestContext.domainName + '/' + event.requestContext.stage
});
const WebSocket = require('ws');
const readline = require('readline');
const url = process.argv[2];
const ws = new WebSocket(url);
ws.on('open', () => console.log('connected'));
ws.on('message', data => console.log(`From server: ${data}`));
ws.on('close', () => {
console.log('disconnected');
@robzhu
robzhu / chat.cs
Created January 21, 2019 14:52
async stream
await foreach (string message in GetChatMessagesAsync()) {
Render(message);
}
DynamoDB.put({ TableName, Item: {
id: "1234",
name: "carl",
friends: ["meatwad", "frylock", "shake"]
}
});
async function removeFriendByValue(friendName: string) {
const Key = { id: "1234" };
// fetch the document
let result = await DynamoDB.get({
TableName,
Key
}).promise();
// find the index
const indexToRemove = result.Item.friends.indexOf(friendName);
if (indexToRemove === -1) {
{
TableName,
Key,
UpdateExpression: `REMOVE friends[${indexToRemove}]`,
ConditionExpression: `friends[${indexToRemove}] = :valueToRemove`,
ExpressionAttributeValues: {
":valueToRemove": friendName
}
}