How to use the kalm.connect function in kalm

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!',
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 / 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 / distributed_pub_sub / server.js View on Github external
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) {
          provider.broadcast('n.add', body);
        } else provider.connect(frame.remote);
      });
      client.subscribe('n.evt', (body, frame) => {
        providers.forEach((_provider) => {
          if (_provider.label === 'external') {
            _provider.broadcast('r.evt', body);
          }
        });
      });

kalm

The socket optimizer

Apache-2.0
Latest version published 2 years ago

Package Health Score

54 / 100
Full package analysis