How to use kalm - 10 common examples

To help you get started, we’ve selected a few kalm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kalm / kalm.js / examples / distributed_pub_sub / client.js View on Github external
const crypto = require('crypto');
const kalm = require('kalm');
const ws = require('@kalm/ws');

const clientId = crypto.randomBytes(4).toString('hex');

const Client = kalm.connect({
  label: clientId,
  transport: ws(),
  port: 3938,
  routine: kalm.routines.realtime(),
});

Client.subscribe('r.evt', (body, frame) => {
  console.log('Relayed event', body, frame);
});

// now send some events
setInterval(() => {
  Client.write('c.evt', {
    origin: clientId,
    timestamp: Date.now(),
    message: 'hello world!',
  });
}, 2000);
github kalm / kalm.js / examples / distributed_pub_sub / client.js View on Github external
const crypto = require('crypto');
const kalm = require('kalm');
const ws = require('@kalm/ws');

const clientId = crypto.randomBytes(4).toString('hex');

const Client = kalm.connect({
  label: clientId,
  transport: ws(),
  port: 3938,
  routine: kalm.routines.realtime(),
});

Client.subscribe('r.evt', (body, frame) => {
  console.log('Relayed event', body, frame);
});

// now send some events
setInterval(() => {
  Client.write('c.evt', {
    origin: clientId,
    timestamp: Date.now(),
    message: 'hello world!',
github kalm / kalm.js / examples / chat_websocket / server.js View on Github external
const kalm = require('kalm');
const ws = require('@kalm/ws');

const Server = kalm.listen({
  label: 'server',
  port: 8800,
  transport: ws(),
  routine: kalm.routines.tick(5), // Hz
  host: '0.0.0.0',
});

Server.on('connection', (client) => {
  client.subscribe('c.evt', (body, frame) => {
    Server.broadcast('r.evt', body);
  });

  Server.broadcast('r.sys', { msg: 'user joined' });
});

Server.on('deconnection', (client) => {
  Server.broadcast('r.sys', { msg: 'user left' });
});
github kalm / kalm.js / examples / distributed_pub_sub / server.js View on Github external
const kalm = require('kalm');
const ws = require('@kalm/ws');
const tcp = require('@kalm/tcp');

const seed = { host: '0.0.0.0', port: 3000 };
const tickSeed = Date.now();

const seedHost = '0.0.0.0'; // Apply seed config

const providers = [
  kalm.listen({
    label: 'internal',
    transport: tcp(),
    port: 3000,
    routine: kalm.routines.realtime(),
    host: '0.0.0.0', // Apply local ip
  }),
  kalm.listen({
    label: 'external',
    transport: ws(),
    port: 3938,
    routine: kalm.routines.tick(120, tickSeed),
    host: '0.0.0.0', // Apply local ip
  }),
];

providers.forEach((provider) => {
  const isIntern = provider.label === 'internal';
  const isSeed = (isIntern && seed.host === seedHost);

  if (!isSeed && isIntern) {
github kalm / kalm.js / examples / distributed_pub_sub / server.js View on Github external
const seedHost = '0.0.0.0'; // Apply seed config

const providers = [
  kalm.listen({
    label: 'internal',
    transport: tcp(),
    port: 3000,
    routine: kalm.routines.realtime(),
    host: '0.0.0.0', // Apply local ip
  }),
  kalm.listen({
    label: 'external',
    transport: ws(),
    port: 3938,
    routine: kalm.routines.tick(120, tickSeed),
    host: '0.0.0.0', // Apply local ip
  }),
];

providers.forEach((provider) => {
  const isIntern = provider.label === 'internal';
  const isSeed = (isIntern && seed.host === seedHost);

  if (!isSeed && isIntern) {
    kalm.connect({}).write('n.add', { host: seedHost });
  }

  provider.on('connection', (client) => {
    if (isIntern) {
      client.subscribe('n.add', (body, frame) => {
        if (isSeed) {
github kalm / kalm.js / examples / chat_websocket / server.js View on Github external
const kalm = require('kalm');
const ws = require('@kalm/ws');

const Server = kalm.listen({
  label: 'server',
  port: 8800,
  transport: ws(),
  routine: kalm.routines.tick(5), // Hz
  host: '0.0.0.0',
});

Server.on('connection', (client) => {
  client.subscribe('c.evt', (body, frame) => {
    Server.broadcast('r.evt', body);
  });

  Server.broadcast('r.sys', { msg: 'user joined' });
});

Server.on('deconnection', (client) => {
github kalm / kalm.js / examples / typescript / client.ts View on Github external
import kalm from 'kalm';
import ws from '@kalm/ws';

const client = kalm.connect({
  transport: ws(),
  port: 3938,
  routine: kalm.routines.realtime(),
});

type MyCustomPayload = {
  foo: string
  message: string
};

client.subscribe('r.evt', (body: MyCustomPayload, frame) => {
  console.log('Server event', body, frame);
});

client.write('c.evt', 'hello world!');
github kalm / kalm.js / examples / chat / client.js View on Github external
const kalm = require('kalm');
const ws = require('@kalm/ws');
const { randomBytes } = require('crypto');

const Client = kalm.connect({
  label: randomBytes(4).toString('hex'),
  host: '0.0.0.0',
  port: 8800,
  transport: ws(),
  routine: kalm.routines.realtime(),
});

Client.subscribe('r.evt', (evt, frame) => console.log(`${evt.name}: ${evt.msg}`, frame));
Client.subscribe('r.sys', (evt, frame) => console.log(`[System]: ${evt.msg}`, frame));

Client.on('connect', () => {
  Client.write('c.evt', { name: Client.label, msg: 'Hey everyone!' });
});

Client.on('disconnect', () => {
  console.log('--Connection lost--');
});
github kalm / kalm.js / examples / typescript / server.ts View on Github external
import kalm from 'kalm';
import ws from '@kalm/ws';

const provider = kalm.listen({
  transport: ws(),
  port: 3938,
  routine: kalm.routines.tick(5),
  host: '0.0.0.0',
});

type MyCustomPayload = {
  foo: string
  message: string
};

provider.on('connection', (client) => {
  client.subscribe('foo', (body: MyCustomPayload, frame) => {
    console.log('Client event', body, frame);
  });

  const payload: MyCustomPayload = {
    foo: 'bar',
    message: 'hello from the server!',
github kalm / kalm.js / examples / chat / client.js View on Github external
const kalm = require('kalm');
const ws = require('@kalm/ws');
const { randomBytes } = require('crypto');

const Client = kalm.connect({
  label: randomBytes(4).toString('hex'),
  host: '0.0.0.0',
  port: 8800,
  transport: ws(),
  routine: kalm.routines.realtime(),
});

Client.subscribe('r.evt', (evt, frame) => console.log(`${evt.name}: ${evt.msg}`, frame));
Client.subscribe('r.sys', (evt, frame) => console.log(`[System]: ${evt.msg}`, frame));

Client.on('connect', () => {
  Client.write('c.evt', { name: Client.label, msg: 'Hey everyone!' });
});

Client.on('disconnect', () => {
  console.log('--Connection lost--');

kalm

The socket optimizer

Apache-2.0
Latest version published 1 year ago

Package Health Score

61 / 100
Full package analysis