Giter Site home page Giter Site logo

logux-server's Introduction

Logux Server

Logux is a client-server communication protocol. It synchronizes events between clients and server logs.

This framework helps you to write Logux server and define back-end callbacks for each client’s event type.

This is a first proof-of-concept version. It simply synchronizes all the events between clients, not yet having many syntax sugar that we've planned for future.

Sponsored by Evil Martians

Getting Started

Installation

Install Node.js.

Create new Node.js project:

mkdir PROJECT_NAME
cd PROJECT_NAME
npm init

Install Logux Server:

npm install --save logux-server logux-core

Create Main File

Create server.js with this boilerplate:

const cleanEvery = require('logux-core').cleanEvery
const Server = require('logux-server').Server

const app = new Server({
  subprotocol: '1.0.0',
  supports: '1.x',
  root: __dirname
})

app.auth(token => {
  // TODO Return user by token or false on bad token. It should be Promise.
})

app.log.on('event', (event, meta) => {
  // TODO Do something on client event. Write to database, ask other service.
})

cleanEvery(app.log, 1000)
app.log.keep((event, meta) => {
  // TODO return true if event should not be removed yet from log
})

app.listen(app.loadOptions(process))

Write Business Logic

Logux is a communication protocol. It doesn’t know anything about your database. You need to write custom logic inside your event callbacks.

app.log.on('event', (event, meta) => {
  if (event.type === 'changeName') {
    users.find({ id: event.user }).then(user => {
      user.update({ name: event.name })
    })
  }
})

Read logux-core docs for app.log API.

If you already have business logic written in PHP, Ruby, Java — don’t worry. You can do whatever you want in the event listener. For one, you may just call the legacy REST API:

if (event.type === 'changeName') {
  request.put(`http://example.com/users/${event.user}`).form({
    name: event.name
  })
}

Test Your Logic Locally

You can run your server with:

npm start

Use ws://localhost:1337 URL in Logux Client.

Get SSL Certificate

Logux uses WebSockets for communicating between client and server. Without SSL, old proxies and firewalls can block WebSockets connection. Also, SSL will obviously help to prevent many attacks against your server.

Probably the best way to get a free SSL certificate is Let’s Encrypt.

Save certificate PEM-files to cert.pem and key.pem in your project directory or change listen() options to correct certificate paths.

Start Production Server

Use your favorite DevOps tools to start Logux server in production mode:

NODE_ENV=production npm start

You DevOps tools could set Logux Server options via arguments or environment variables:

Command-line Environment Description
-h, --host LOGUX_HOST Host to bind server
-p, --port LOGUX_PORT Port to bind server
-k, --key LOGUX_KEY Path to SSL key
-c, --cert LOGUX_CERT Path to SSL certificate

logux-server's People

Contributors

ai avatar gazay avatar gbezyuk avatar mavrin avatar okonet avatar pavlokovalov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.