A JavaScript client library for building interactive web applications with Ruby Live framework.
- Real-time Communication: WebSocket-based client-server communication.
- DOM Manipulation: Efficient updating, replacing, and modifying HTML elements.
- Event Forwarding: Forward client events to server for processing.
- Controller Loading: Declarative JavaScript controller loading with
data-live-controller
. - Automatic Cleanup: Proper lifecycle management and memory cleanup.
- Live Elements: Automatic binding and unbinding of live elements.
npm install @socketry/live
import { Live } from '@socketry/live';
// Start the live connection
const live = Live.start({
path: 'live', // WebSocket endpoint
base: window.location.href
});
Live.js supports declarative controller loading using the data-live-controller
attribute:
<div class="live" id="game" data-live-controller="/static/game_controller.mjs">
<!-- Game content -->
</div>
// game_controller.mjs
export default function(element) {
console.log('Controller loaded for:', element);
// Setup your controller logic
element.addEventListener('click', handleClick);
// Return a controller object with cleanup
return {
dispose() {
element.removeEventListener('click', handleClick);
}
};
}
Live.start(options)
- Create and start a new Live instanceoptions.window
- Window object (defaults to globalThis)options.path
- WebSocket path (defaults to 'live')options.base
- Base URL (defaults to window.location.href)
connect()
- Establish WebSocket connectiondisconnect()
- Close WebSocket connection
update(id, html, options)
- Update element contentreplace(selector, html, options)
- Replace elementsprepend(selector, html, options)
- Prepend contentappend(selector, html, options)
- Append contentremove(selector, options)
- Remove elements
forward(id, event)
- Forward event to serverforwardEvent(id, event, detail, preventDefault)
- Forward DOM eventforwardFormEvent(id, event, detail, preventDefault)
- Forward form event
script(id, code, options)
- Execute JavaScript codeloadController(id, path, options)
- Load JavaScript controller
dispatchEvent(selector, type, options)
- Dispatch custom events
Most methods accept an options
parameter with:
options.reply
- If truthy, server will reply with{reply: options.reply}
Controllers are JavaScript modules that manage view-specific behavior:
// Simple controller
export default function(element) {
// Setup code
return {
dispose() {
// Cleanup code
}
};
}
// With options
export default function(element, options) {
const config = options.config || {};
// Use config...
}
Elements with the live
CSS class are automatically managed:
<div class="live" id="my-element">
Content that can be updated
</div>
// Forward click events
element.addEventListener('click', (event) => {
live.forwardEvent('my-element', event, { button: 'clicked' });
});
// Forward form submissions
form.addEventListener('submit', (event) => {
live.forwardFormEvent('my-form', event, { action: 'submit' });
});
We welcome contributions to this project.
- Fork it.
- Create your feature branch (
git checkout -b my-new-feature
). - Commit your changes (
git commit -am 'Add some feature'
). - Push to the branch (
git push origin my-new-feature
). - Create new Pull Request.
In order to protect users of this project, we require all contributors to comply with the Developer Certificate of Origin. This ensures that all contributions are properly licensed and attributed.
This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
- lively — Ruby framework for building interactive web applications.
- live — Provides client-server communication using websockets.
- live-audio-js — Web Audio API-based game audio synthesis library.