![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@yomo/presencejs
Advanced tools
@yomo/presencejs ⚡️ made realtime web applications edge-aware by YoMo
Presencejs
is a JavaScript library that allows you to build real-time web applications quickly, the server is built atop of YoMo, which provide secure, low-latency, and high-performance geo-distributed services.
...and a lot more.
With presencejs
, components will get data flow in real time. Thus, the UI will be always fast and reactive.
presencejs
to your web appUsing npm
$ npm i --save @yomo/presencejs
Using yarn
$ yarn add @yomo/presencejs
Using pnpm
$ pmpm i @yomo/presencejs
For CDN, you can use skypack: https://cdn.skypack.dev/@yomo/presencejs
<script type="module">
import Presence from 'https://cdn.skypack.dev/@yomo/presencejs';
</script>
The client need to authenticate with YoMo to establish a realtime connection. The following code sample uses a demo YoMo's server(https://presencejs.yomo.dev
) and public Key to authenticate and print the message Connected to YoMo!
when you’ve successfully connected.
If you build your application using next.js, then you can use API Routes to get the access token.
For example, the following API route pages/api/presence-auth.js
returns a json response with a status code of 200:
export default async function handler(req, res) {
if (req.method === 'GET') {
try {
const response = await fetch('https://prsc.yomo.dev/api/v1/auth', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
app_id: process.env.PRESENCE_APP_ID,
app_secret: process.env.PRESENCE_APP_SECRET,
}),
});
const data = await response.json();
const token = data.data;
if (token) {
res.status(200).json(token);
} else {
res.status(400).json({ msg: data.message });
}
} catch (error) {
if (typeof error === 'string') {
res.status(500).json({ msg: error });
} else if (error instanceof Error) {
res.status(500).json({ msg: error.message });
}
}
} else {
// Handle any other HTTP method
res.status(405).json({})
}
}
Response data:
{
"token": "eyJhbGciOiJIUzI1..."
}
Presence
instance.import Presence from '@yomo/presencejs';
// create an instance.
const yomo = new Presence('https://presence.yomo.dev', {
auth: {
// Certification Type
type: 'token',
// api for getting access token
endpoint: '/api/presence-auth',
},
});
yomo.on('connected', () => {
console.log('Connected to server: ', yomo.host);
});
Call the toRoom('001')
function to enter room 001
, without it, you are in the default room.The client receives a message with the name online
through the on
callback function, and can also subscribe to a message with the name mousemove
by returning an observable object through on$
.
yomo.on('connected', () => {
// Enter a room
yomo.toRoom('001');
// Function to handle response for given event from server
yomo.on('online', data => {
console.log('online:', data);
});
// Same as the `on` method, returns an observable response
yomo.on$('mousemove').subscribe(data => {
console.log('mousemove:', data);
});
// If you want to display the latency, you can get the value of the latency like this
yomo.on('latency', data => {
const { id, latency, meshId } = data;
console.log('latency:', latency);
});
});
The following example code send a message to the quickstart room with the name online
and the contents pixel position.
You can use rxjs to get an observable sequence of browser event transitions,then send the data to the signal channel with the name mousemove
in room 001
via ofRoom('001', 'mousemove')
.
import { fromEvent } from 'rxjs';
import { map } from 'rxjs/operators';
const ID = '34a1dbb5-c031-4680-926c-84a789d251e0';
yomo.on('connected', () => {
// Function for sending data to the server
yomo.send('online', {
id: ID,
x: 10,
y: 10,
});
// Converting browser events into observable sequences.
const mousemove$ = fromEvent(document, 'mousemove').pipe(
map(event => {
return {
id: ID,
x: event.clientX,
y: event.clientY,
};
})
);
// Sending data streams to the server
mousemove$.subscribe(yomo.ofRoom('001', 'mousemove'));
});
A connection to YoMo can be closed once it is no longer needed.
yomo.close();
yomo.on('closed', () => {
console.log('Closed the connection');
});
Methods of instance | Description | Type |
---|---|---|
on | Function to handle response for given event from server | on<T>(event: string, cb: (data: T) => void): void |
on$ | Same as the on method, returns an observable response | on$<T>(event: string): Observable<T> |
send | Function for sending data to the server | send<T>(event: string, data: T) |
toRoom | Enter a room | toRoom(roomName: string): Presence |
ofRoom | Function for sending data streams to the server | ofRoom(roomName: string, event: string) |
close | A connection to YoMo can be closed once it is no longer needed. | close(): void |
The MIT License.
FAQs
@yomo/presencejs ⚡️ made realtime web applications edge-aware by YoMo
The npm package @yomo/presencejs receives a total of 5 weekly downloads. As such, @yomo/presencejs popularity was classified as not popular.
We found that @yomo/presencejs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.