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!',
});
}, 2000);
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' });
});
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) {
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) {
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!');
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 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!',