Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@ably-labs/models
Advanced tools
<img src="https://github.com/ably-labs/models/actions/workflows/dev-ci.yml/badge.svg?
The Ably Models SDK is a key component of the LiveSync product that lets you stream realtime updates from your database at scale to frontend clients.
The Models SDK is a frontend library that simplifies subscribing to the changes in data models, applying optimistic updates and merging them with confirmed updates. It is a standalone SDK built on Ably’s JavaScript SDK with full TypeScript support.
The Database Connector and Ably Channels are the other two components of LiveSync that help publish changes from your database to frontend clients.
A model represents a data model of a specific part of your frontend application. Each frontend client can have multiple data models within the same application.
When creating a new Model using the Models SDK you provide two functions to the Model a sync()
function and a merge()
function:
sync(
) function is used by the SDK to retrieve the current state of the data model from your backend.merge()
function is used by the SDK to merge state change events published by the Database Connector with the existing frontend state in the Model.You can use the Models SDK as a standalone library to merge new update events with existing frontend state, but the SDK works best as part of LiveSync.
The data models as part of the Models SDK remain synchronized with the state of your database, in realtime. You can easily integrate this SDK into your project regardless of which frontend framework you use.
LiveSync, and the Models SDK, is in public alpha so that you can explore its capabilities. Your feedback will help prioritize improvements and fixes for later releases. The features in this release have been built to work under real-world situations and load, and for real-world use-cases, but there may still be some rough edges in this alpha.
To begin, you will need the following:
subscribe
capabilities.Install the Ably JavaScript SDK and the Realtime Data Models SDK:
npm install ably @ably-labs/models
Though you can test your installation and authentication with basic authentication, we strongly recommend token authentication for in production environments.
To instantiate the Realtime Data Models SDK, create an Ably client and pass it into the ModelsClient constructor:
import ModelsClient from '@ably-labs/models';
import { Realtime } from 'ably';
const ably = new Realtime({ key: 'YOUR_ABLY_API_KEY' });
const modelsClient = new ModelsClient({ ably });
A Model
represents a live, observable data model supported by the database.
To create a model, you need to:
// this is the type for our model's data as represented in the frontend application
type Post = {
id: number;
text: string;
comments: string[];
};
// a function used by the model to initialise with the correct data from your backend
async function sync() {
const result = await fetch('/api/post');
return result.json(); // e.g. { sequenceId: 1, data: { ... } }
}
// a function used by the model to merge a change event that is received and the existing model state
function merge(state: Post, event: OptimisticEvent | ConfirmedEvent) {
return {
...state,
text: event.data, // replace the previous post text field with the new value
}
}
// a function that you might use to mutate the model data in your backend
async function updatePost(mutationId: string, content: string) {
const result = await fetch(`/api/post`, {
method: 'PUT',
body: JSON.stringify({ mutationId, content }),
});
return result.json();
}
// create a new model instance called 'post' by passing the sync and merge functions
const model = modelsClient.models.get({
channelName: 'models:posts',
sync: sync,
merge: merge,
})
// subscribe to live changes to the model data!
model.subscribe((err, post) => {
if (err) {
throw err;
}
console.log('post updated:', post);
});
// apply an optimistic update to the model
// confirmation is a promise that resolves when the optimistic update is confirmed by the backend.
// cancel is a function that can be used to cancel and rollback the optimistic update.
const [confirmation, cancel] = await model.optimistic({
mutationId: 'my-mutation-id',
name: 'updatePost',
data: 'new post text',
})
// call your backend to apply the actual change
updatePost('my-mutation-id', 'new post text')
// wait for confirmation of the change from the backend
await confirmation;
For more information, see usage docs within this repository.
The Models SDK is currently in public alpha. We'd love to hear your feedback.
FAQs
<img src="https://github.com/ably-labs/models/actions/workflows/dev-ci.yml/badge.svg?
The npm package @ably-labs/models receives a total of 21 weekly downloads. As such, @ably-labs/models popularity was classified as not popular.
We found that @ably-labs/models demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.