Skip to content

Instantly share code, notes, and snippets.

@jondubois
jondubois / sc-crud-sample_index.html
Last active September 23, 2018 09:31
sc-crud-sample_index.html
<!DOCTYPE html>
<html>
<head>
<title>SocketCluster Inventory Tracking Sample</title>
<link href="/styles/bootstrap.min.css" rel="stylesheet" type="text/css">
<link rel="icon" type="image/png" href="/favicon.ico">
<style>
body {
padding: 10px;
}
@jondubois
jondubois / sc-crud-sample_app-inventory.js
Last active September 24, 2018 11:58
sc-crud-sample_app-inventory.js
import getCategoryListPageComponent from '/pages/page-category-list.js';
import getCategoryDetailsPageComponent from '/pages/page-category-details.js';
import getProductDetailsPageComponent from '/pages/page-product-details.js';
import getLoginPageComponent from '/pages/page-login.js';
let socket = socketCluster.create();
let pageOptions = {
socket
};
@jondubois
jondubois / sc-crud-sample_page-login.js
Last active September 23, 2018 09:54
sc-crud-sample_page-login.js
function getPageComponent(pageOptions) {
let socket = pageOptions.socket;
return Vue.extend({
data: function () {
return {
error: null,
username: 'bob',
password: 'password123'
@jondubois
jondubois / sc-crud-sample_page-category-details.js
Last active September 23, 2018 10:05
sc-crud-sample_page-category-details.js
import SCCollection from '/node_modules/sc-collection/sc-collection.js';
import SCModel from '/node_modules/sc-model/sc-model.js';
function getPageComponent(pageOptions) {
return Vue.extend({
props: {
categoryId: String
},
data: function () {
this.categoryModel = new SCModel({
@jondubois
jondubois / sc-crud-sample_worker.js
Last active September 30, 2018 21:22
sc-crud-sample_worker.js
var SCWorker = require('socketcluster/scworker');
var fs = require('fs');
var express = require('express');
var serveStatic = require('serve-static');
var path = require('path');
var dummyData = require('./sc_modules/dummy-data');
var authentication = require('./sc_modules/authentication');
var scCrudRethink = require('sc-crud-rethink');
class Worker extends SCWorker {
@jondubois
jondubois / sc-crud-sample_authentication.js
Last active September 30, 2018 21:30
sc-crud-sample_authentication.js
module.exports.attach = function (scServer, socket) {
var tokenExpiresInSeconds = 10 * 60;
var tokenRenewalIntervalInMilliseconds = Math.round(
1000 * tokenExpiresInSeconds / 3
);
// Keep renewing the token (if there is one) at a predefined interval to make
// sure that it doesn't expire while the connection is active.
var renewAuthTokenInterval = setInterval(function () {
var currentToken = socket.getAuthToken();
@jondubois
jondubois / ag-listen-for-inbound-connections.js
Last active January 20, 2019 15:34
ag-listen-for-inbound-connections
// --- in server.js ---
// Asyngular/WebSocket connection handling loop.
(async () => {
for await (let {socket} of agServer.listener('connection')) {
// Handle socket connection.
}
})();
@jondubois
jondubois / ag-listen-for-inbound-rpcs-and-messages.js
Last active January 20, 2019 16:27
ag-listen-for-inbound-rpcs-and-messages
// --- in server.js ---
// Asyngular/WebSocket connection handling loop.
(async () => {
for await (let {socket} of agServer.listener('connection')) {
// Handle socket connection.
(async () => {
// Set up a loop to handle and respond to RPCs for a procedure.
for await (let req of socket.procedure('customProc')) {
if (req.data && req.data.bad) {
@jondubois
jondubois / ag-connecting-to-the-server.js
Last active February 10, 2019 11:59
ag-connecting-to-the-server
// --- in public/index.html ---
// This logic should already be in public/index.html by default.
let socket = asyngularClient.create();
@jondubois
jondubois / ag-invoking-rpcs.js
Created January 20, 2019 15:39
ag-invoking-rpcs
// --- in public/index.html ---
// ... After the socket is created.
(async () => {
let result = await socket.invoke('customProc', {foo: 'bar'});
// result will be 'Success'
})();