Skip to content

Instantly share code, notes, and snippets.

View thomasjallerton's full-sized avatar

Thomas Allerton thomasjallerton

View GitHub Profile
### Keybase proof
I hereby claim:
* I am thomasjallerton on github.
* I am thomasallerton (https://keybase.io/thomasallerton) on keybase.
* I have a public key ASBkkbdF-ZZovVbmAZAAIdvdY8N0Wmug03C1xZ-x06GtJAo
To claim this, I am signing this object:
@thomasjallerton
thomasjallerton / pom.xml
Created May 6, 2019 17:34
Chat application pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nimbus-framework</groupId>
<artifactId>nimbus-example-websocket-chat</artifactId>
<version>1.0-SNAPSHOT</version>
@thomasjallerton
thomasjallerton / WebSocketApiLocalDeployment.java
Created May 6, 2019 17:24
Local deployment of chat application
public class WebSocketApiLocalDeployment {
@Test
public void localWebChat() {
LocalNimbusDeployment localNimbusDeployment = LocalNimbusDeployment.getNewInstance("com.nimbusframework.examples.webchat");
localNimbusDeployment.startAllServers();
}
}
@thomasjallerton
thomasjallerton / WebSocketApiTest.java
Created May 6, 2019 17:13
Unit test for WebSocket API
public class WebSocketApiTest {
@Test
public void onConnectAddsUserToConnectionDetail() {
LocalNimbusDeployment localNimbusDeployment = LocalNimbusDeployment.getNewInstance("com.nimbusframework.examples.webchat");
Map<String, String> queryStringParams = new HashMap<>();
queryStringParams.put("user", "user1");
Map<String, String> headers = new HashMap<>();
@thomasjallerton
thomasjallerton / Website.java
Created May 6, 2019 16:48
The client website description
@FileStorageBucket(
bucketName = Website.WEBSITE_BUCKET,
staticWebsite = true,
indexFile = "webchat.html",
stages = {DEV_STAGE, PRODUCTION_STAGE}
)
@FileUpload(bucketName = Website.WEBSITE_BUCKET,
localPath = "src/website",
targetPath = "",
substituteNimbusVariables = true,
@thomasjallerton
thomasjallerton / webchat.js
Created May 6, 2019 16:41
WebChat client code
connected = document.getElementById("connected");
recipient = document.getElementById("recipient");
log = document.getElementById("log");
chat = document.getElementById("chat");
username = document.getElementById("username");
form = chat.form;
state = document.getElementById("status");
if (window.WebSocket === undefined) {
state.innerHTML = "sockets not supported";
@thomasjallerton
thomasjallerton / WebchatRestApi.java
Created May 6, 2019 16:30
REST API for chat application
public class WebchatRestApi {
private DocumentStoreClient<UserDetail> userDetails = ClientBuilder.getDocumentStoreClient(UserDetail.class);
private KeyValueStoreClient<String, ConnectionDetail> connectionDetails = ClientBuilder.getKeyValueStoreClient(String.class, ConnectionDetail.class);
@HttpServerlessFunction(method = HttpMethod.POST, path = "register", stages = {DEV_STAGE, PRODUCTION_STAGE}, allowedCorsOrigin = "${WEBCHATNIMBUS_URL}")
@UsesDocumentStore(dataModel = UserDetail.class, stages = {DEV_STAGE, PRODUCTION_STAGE})
public void register(String username) {
userDetails.put(new UserDetail(username, null));
}
@thomasjallerton
thomasjallerton / WebchatApi.java
Created May 6, 2019 16:12
WebSocket API for chat application
public class WebchatApi {
private ServerlessFunctionWebSocketClient webSocketClient = ClientBuilder.getServerlessFunctionWebSocketClient();
private DocumentStoreClient<UserDetail> userDetails = ClientBuilder.getDocumentStoreClient(UserDetail.class);
private KeyValueStoreClient<String, ConnectionDetail> connectionDetails = ClientBuilder.getKeyValueStoreClient(String.class, ConnectionDetail.class);
@WebSocketServerlessFunction(topic = "$connect", stages = {DEV_STAGE, PRODUCTION_STAGE})
@UsesDocumentStore(dataModel = UserDetail.class, stages = {DEV_STAGE, PRODUCTION_STAGE})
@UsesKeyValueStore(dataModel = ConnectionDetail.class, stages = {DEV_STAGE, PRODUCTION_STAGE})
public void onConnect(WebSocketEvent event) throws Exception {
@thomasjallerton
thomasjallerton / Message.java
Created May 6, 2019 16:09
Message model that backend sends to clients
public class Message {
private String message;
private String sender;
public Message() {}
public Message(String message, String sender) {
this.message = message;
@thomasjallerton
thomasjallerton / WebSocketMessage.java
Created May 6, 2019 16:08
WebSocket message model that client sends to backend
public class WebSocketMessage {
public WebSocketMessage() {}
private String message = "";
private String recipient = "";
public String getMessage() {
return message;