Skip to content
/ server Public

Build your own Logux server or make a proxy between a WebSocket and an HTTP backend in any language

License

Notifications You must be signed in to change notification settings

logux/server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0a78e42 · Oct 4, 2024
Aug 6, 2024
Aug 25, 2024
Feb 15, 2024
Aug 25, 2024
Aug 25, 2024
Aug 25, 2024
Jul 18, 2023
Feb 15, 2024
Feb 15, 2024
Jul 18, 2023
Feb 15, 2024
Aug 6, 2024
Feb 15, 2024
Jul 18, 2023
Aug 25, 2024
Aug 6, 2024
Aug 25, 2024
Jul 18, 2023
Aug 25, 2024
Oct 26, 2016
Dec 24, 2021
Jan 5, 2022
Oct 4, 2024
Oct 26, 2016
Aug 4, 2023
Aug 6, 2024
Jul 18, 2023
Jul 18, 2023
Oct 4, 2024
Oct 4, 2024
Sep 3, 2022
Aug 6, 2024

Repository files navigation

Logux Server Cult Of Martians

Logux is a new way to connect client and server. Instead of sending HTTP requests (e.g., AJAX and GraphQL) it synchronizes log of operations between client, server, and other clients.

This repository contains Logux server with:

  • Framework to write own server.
  • Proxy between WebSocket and HTTP server on any other language.
Sponsored by Evil Martians

Logux Server as Proxy

import { fileURLToPath } from 'url'

const server = new Server(
  Server.loadOptions(process, {
    controlSecret: 'secret',
    subprotocol: '1.0.0',
    supports: '0.6.2',
    backend: 'http://localhost:3000/logux',
    fileUrl: import.meta.url
  })
)

server.listen()

Logux Server as Framework

import { fileURLToPath } from 'url'
import { isFirstOlder } from '@logux/core'
import { dirname } from 'path'
import { Server } from '@logux/server'

const server = new Server(
  Server.loadOptions(process, {
    subprotocol: '1.0.0',
    supports: '1.x',
    fileUrl: import.meta.url
  })
)

server.auth(async ({ userId, token }) => {
  const user = await findUserByToken(token)
  return !!user && userId === user.id
})

server.channel('user/:id', {
  access (ctx, action, meta) {
    return ctx.params.id === ctx.userId
  },
  async load (ctx, action, meta) {
    const user = await db.loadUser(ctx.params.id)
    return { type: 'USER_NAME', name: user.name }
  }
})

server.type('CHANGE_NAME', {
  access (ctx, action, meta) {
    return action.user === ctx.userId
  },
  resend (ctx, action, meta) {
    return { channel: `user/${ ctx.userId }` }
  },
  async process (ctx, action, meta) {
    if (isFirstOlder(lastNameChange(action.user), meta)) {
      await db.changeUserName({ id: action.user, name: action.name })
    }
  }
})

server.listen()