maeva-sockets v3.0.16
maeva-sockets
Client/server web sockets API to connect any database via maeva
Why
Even when you have a database server set up, you need a relay server (HTTP or Web Sockets) so that your client can connect to the database server. maeva-sockets
gives a framework so you can easily create a server your client can talk to to transport database queries.
Server
It is easy to set up a server. Let's say you want to set up a server that can transport queries to a MongoDB server:
import sockets from 'maeva-sockets/server';
import mongodb from 'maeva-mongodb';
const WS_URL = 'ws://localhost:9999'; // Web sockets server URL
const DB_URL = 'mongodb://localhost:7777'; // Database URL
sockets
.start(WS_URL, {connector: () => mongodb(DB_URL, {keepAlive: 5000})})
.on('error', error => console.log(error));
Client
import * as data from 'maeva';
import sockets from 'maeva-sockets/client';
// Define your data model
const userModel = data.model('users', {name: String});
// Connect to sockets server
data.connect(sockets('ws://localhost:9999', {keepAlive: 3000}));
// Now you can call any queries
const users = await data.findMany(userModel);
Server methods
sockets.start(url: string, options: ServerOptions) => EventEmitter
sockets.start(options: ServerOptions) => EventEmitter
// Close server
const server = sockets.start('ws://localhost:9999');
server.emit('close');
// Off helper
server.off('event', fn) === server.removeListener('event', fn);
Server options
port: number
host: string
path: string
Server events
closed
Web sockets server downconnected
Web sockets server successfully connected to databasedisconnected
Web sockets server lost connection to databaseerror
Web sockets server had an errorincoming
New web socket clientlistening
Web sockets server startedoutcoming
Web socket sent a message to clientresponse
Web socket server got response from DB
Client
sockets(url: string, options: ClientOptions);
Client options
debug: boolean
keepAlive: number
When the sockets server is up, but the connection to database is down, client will receive an error that has code DISCONNECTED_FROM_DB
.
const client = data.connect(sockets('ws://localhost:8080'));
client.emitter.on('error', (error) => {
if (error.code === 'DISCONNECTED_FROM_DB') {
console.log('server lost connection to db');
}
})
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago