3.0.16 • Published 6 years ago

maeva-sockets v3.0.16

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

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 down
  • connected Web sockets server successfully connected to database
  • disconnected Web sockets server lost connection to database
  • error Web sockets server had an error
  • incoming New web socket client
  • listening Web sockets server started
  • outcoming Web socket sent a message to client
  • response 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');
  }
})
3.0.16

6 years ago

3.0.15

6 years ago

3.0.14

6 years ago

3.0.13

6 years ago

3.0.12

6 years ago

3.0.11

6 years ago

3.0.10

6 years ago

3.0.9

6 years ago

3.0.8

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.0

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.11

6 years ago

1.3.10

6 years ago

1.3.9

6 years ago

1.3.8

6 years ago

1.3.7

6 years ago

1.3.6

6 years ago

1.3.5

6 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago