Setting Up - kyranet/veza GitHub Wiki

The most basic set up is a server. A server can be used as a "hub" for future clients to connect to, and it's available in both IPC and TCP.

To create it, we will import Server from veza and create a new instance of it, giving it the identifier name for all of its clients, and then we will make it listen to connections.

const { Server } = require('veza');

// We create a new instance of the server
const server = new Server('MyServer');

// We will listen to an IPC path
server.listen('/tmp/MyServer.sock');

// On Windows, the path must start with '//./pipe':
// server.listen('//./pipe/tmp/MyServer.sock');

// Alternatively we can use TCP
// server.listen(8999);

Note: You can check more information about IPC paths in Node.js here.

Got your first server created? Congratulations!


Now it is time to create our first client, which will connect to our server. This can be in another process, even if it is not a fork or a child process of the server, if you used IPC it will only work if the client and the server are in the same machine, but if you plan on a multi-server set up, TCP must be used instead.

To create a client, we import Client from veza and follow the same steps to make a Server, but instead of using listen, you use connectTo:

const { Client } = require('veza');

// We create a new instance of the server
const client = new Client('MyClient');

// We will listen to an IPC path
client.connectTo('/tmp/MyServer.sock');

// On Windows, the path must start with '//./pipe':
// client.connectTo('//./pipe/tmp/MyServer.sock');

// Alternatively we can use TCP
// client.connectTo(8999);

As you see, it is symmetrical with the server. You listen to 'X' and connect to 'X'.

Events

Once we have the server and the client running, it is time to set up the events, you can find the documentation for Client's events here, and Server's events here.

We will now modify the files to handle some:

const { Server } = require('veza');

// We create a new instance of the server and listen to some events
const server = new Server('MyServer')
	.on('open', () => console.log('[veza] [server] Server ready'))
	.on('close', () => console.log('[veza] [server] Server disconnected'))
	.on('error', (error, client) => console.error(`[veza] [server] Errored ${client ? client.name : 'Unknown'}:`, error))
	.on('connect', client => console.log(`[veza] [server] New client: ${client.name}`))
	.on('disconnect', client => console.log(`[veza] [server] Removed client: ${client.name}`))
	.on('message', (message, client) => console.log(`[veza] [server] Received message from ${client.name}:` message));

// We will listen to an IPC path
server.listen('/tmp/MyServer.sock');
const { Client } = require('veza');

// We create a new instance of the server
const client = new Client('MyClient')
	.on('error', (error, client) => console.error(`[veza] [client] Errored ${client ? client.name : 'Unknown'}:`, error))
	.on('ready', client => console.log(`[veza] [client] Connection ${client.name} ready to use`))
	.on('disconnect', client => console.log(`[veza] [client] Disconnected from ${client.name}`))
	.on('message', (message, client) => console.log(`[veza] [client] Received message from ${client.name}:` message));

// We will listen to an IPC path
client.connectTo('/tmp/MyServer.sock');

Note: Veza uses Node.js events for all of this, as Server and Client extend EventEmitter.

Now we start both, and we will see the following logs:

[veza] [server] Server ready
[veza] [client] Connection MyServer ready to use
[veza] [server] New client: MyClient

Messaging

TBD.