Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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!',
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--');
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!');
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);
}
});
});