In this guide, we will create the Logux proxy between WebSocket and your back-end server.
- You already have back-end HTTP server. It authenticates users and generates HTML pages. You want to continue using this HTTP server to process Logux actions.
- You use Logux Redux on the client side.
If you like Node.js and want the best performance, you can try to move business logic directly to Logux Server. Or you can keep high-performance parts in Logux Server and send others to the back-end HTTP server.
Creating the Project
Install Node.js (version 12.0 or later).
Create a directory for a project. We will use server-logux
name.
mkdir server-logux
cd server-logux
Create package.json
with:
{
"name": "server-logux",
"private": true,
"type": "module",
"scripts": {
"start": "node index.js"
}
}
Install Logux Server:
npm i @logux/server
pnpm add @logux/server
yarn add @logux/server
Create index.js
with:
import { Server } from '@logux/server'
const server = new Server(
Server.loadOptions(process, {
subprotocol: '1.0.0',
supports: '1.x',
fileUrl: import.meta.url
})
)
server.listen()
Create .env
file. Put this file to .gitignore
. Set your local back-end server URL and secret:
LOGUX_BACKEND=http://localhost:3000/
LOGUX_CONTROL_SECRET=secret
The proxy is ready. You can start it with:
npm start
pnpm start
yarn start
To stop the server press Command+. on Mac OS X and Ctrl+C on Linux and Windows.
The proxy will send the user’s authentication request, Logux subscriptions, and actions to http://localhost:3000/logux
. Your back-end can send actions to the client by sending an HTTP request to http://localhost:31338
.
Back-end Server
Now we need to prepare back-end to receive requests from Logux proxy server.
logux-django
package adds Back-end Protocol support to Django.
Go to your Django application folder:
cd ../django-server
Install logux-django
:
pip install logux-django
Add path(r'logux/', include('logux.urls')),
into your urls.py
Sets Logux settings in your settings.py
:
# Logux settings: https://logux.io/guide/starting/proxy-server/
LOGUX_CONTROL_SECRET = "secret"
LOGUX_URL = "http://localhost:31338"
LOGUX_AUTH_FUNC = (lambda user_id, token: True) if DEBUG is True else None
logux_rails
gem adds Back-end Protocol support to Ruby on Rails.
Go to your Ruby on Rails application folder:
cd ../server-rails
Add gems to Gemfile
and call bundle
:
gem 'logux_rails'
gem 'dotenv-rails', groups: [:development, :test]
Create .env
file. Put this file to .gitignore
.
LOGUX_CONTROL_SECRET=secret
LOGUX_URL=http://localhost:31338/
Create config/initializers/logux.rb
file:
Logux.configuration do |config|
config.password = ENV['LOGUX_CONTROL_SECRET']
config.logux_host = ENV['LOGUX_URL']
config.auth_rule = lambda do |user_id, token|
# Allow only local users until we will have a proper authentication
Rails.env.development?
end
end
Add Logux to config/routes.rb
:
Amplifr::Application.routes.draw do
mount Logux::Engine, at: '/'
-
Read about Logux Back-end Protocol.
-
Implement protocol on your HTTP server.
-
Feel free to ask for help in Logux support chat.
-
You will need proper storage to keep Logux proxy URL and secret. We recommend using
.env
with the library to support this file in your environment.LOGUX_CONTROL_SECRET=secret LOGUX_URL=http://localhost:31338/