npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xbox-rta

v2.0.3

Published

Connect to Xbox's Real Time Activity service

Downloads

46

Readme

xbox-rta

NPM version Discord

Creates and manages an Xbox RTA (Real Time Activity) connection. This websocket connection allows you to subscribe to events such as when a player's presence changes, when a player's party changes, when a session changes and more.

Installation

npm install xbox-rta

Usage

XboxRTA(authflow)

Parameters

  • authflow - Takes an Authflow instance from prismarine-auth, you can see the documentation for this here.
const { XboxRTA } = require('xbox-rta');
const { Authflow, Titles } = require('prismarine-auth');

const authflow = new Authflow('example', './', { authTitle: Titles.XboxAppIOS, deviceType: 'iOS', flow: 'sisu' });

const rta = new XboxRTA(authflow);

Events

A list of events that can be emitted by the XboxRTA class can be found below.

  • 'subscribe' - emitted when a resource has been subscribed
  • 'unsubscribe' - emitted when a resource has been unsubscribed
  • 'event' - emitted when an event has been recieved from a subscribed resource
  • 'error' - emitted when an error has occured with a subscription
rta.on('subscribe', (data) => {
	console.log(data);
});

rta.on('unsubscribe', (data) => {
	console.log(data);
});

rta.on('event', (data) => {
	console.log(data);
});

rta.on('error', (data) => {
	console.log(data);
});

Methods

A list of methods that can be called on the XboxRTA class can be found below.

#.connect()

Connects to the Xbox RTA service. This method should be called before any other methods.

await rta.connect();

#.subscribe(resource)

Subscribes to a resource. This method should be called after the connect method has been called.

Parameters

  • resource - The resource to subscribe to, this should be a valid RTA resource. A list of valid resources can be found here.
const sub = await rta.subscribe('https://userpresence.xboxlive.com/users/xuid(<player_xuid>)/richpresence');

#.unsubscribe(subscriptionId)

Unsubscribes from a resource. This method should be called after the connect method has been called.

Parameters

  • subscriptionId - The subscription ID of the resource to unsubscribe from. This can be found in the subscribe event or in the subscriptionId property of the subscribed response.
await rta.unsubscribe(sub.subscriptionId);

#.destroy()

Destroys the connection to the Xbox RTA service. This method should be called when you are finished with the XboxRTA instance.

await rta.destroy();

Full code

const { XboxRTA } = require('xbox-rta');
const { Authflow, Titles } = require('prismarine-auth');

const main = async () => {
	const auth = new Authflow('example', './', { authTitle: Titles.XboxAppIOS, deviceType: 'iOS', flow: 'sisu' });

	const rta = new XboxRTA(auth);

	rta.on('subscribe', (data) => {
		console.log(data);
	});

	rta.on('unsubscribe', (data) => {
		console.log(data);
	});

	rta.on('event', (data) => {
		console.log(data);
	});

	rta.on('error', (data) => {
		console.log(data);
	});

	await rta.connect();

	const sub = await rta.subscribe('https://userpresence.xboxlive.com/users/xuid(<player_xuid>)/richpresence');

	setTimeout(async () => {
		await rta.unsubscribe(sub.subscriptionId);
	}, 60000); // Unsubscribe after 1 minute

	setTimeout(async () => {
		await rta.destroy();
	}, 120000); // Destroy the connection after 2 minutes
};

main();

Example

View more examples

Get Connection ID

When creating or joining a session, you need to provide a connection ID, this allows you to recieve events relating to the session for example when a user joins or when the session is updated. You can get a connection ID by subscribing to the https://sessiondirectory.xboxlive.com/connections/ endpoint. An example of this being used can be found in bedrock-portal

const { XboxRTA } = require('xbox-rta');
const { Authflow, Titles } = require('prismarine-auth');

const main = async () => {
	const auth = new Authflow('example', './', { authTitle: Titles.MinecraftNintendoSwitch, deviceType: 'Nintendo' });

	const rta = new XboxRTA(auth);

	await rta.connect();

	const sub = await rta.subscribe('https://sessiondirectory.xboxlive.com/connections/');

	console.log(sub.data.ConnectionId);
};

main();

When you subscribe to the https://sessiondirectory.xboxlive.com/connections/ endpoint, you will recieve a response like this in the subscribe event. The event event will also be fired when any changes are made to the session the connection ID is associated with.

{
	"type": 1,
	"sequenceId": 0,
	"status": 0,
	"subscriptionId": 0,
	"data": { "ConnectionId": "d70140fd-7d47-4b5f-b967-fca6c16e71ab" },
	"uri": "https://sessiondirectory.xboxlive.com/connections/"
}

Recieve events when a user's presence changes

const { XboxRTA } = require('xbox-rta')
const { Authflow, Titles } = require('prismarine-auth')

const main = async () => {
  const auth = new Authflow('example', './', { authTitle: Titles.XboxAppIOS, deviceType: 'iOS', flow: 'sisu' })

  const rta = new XboxRTA(auth)

  rta.on('subscribe', (data) => {
    console.log(data)
  })

  rta.on('event', (data) => {
    console.log(data)
  })

  await rta.connect()

  await rta.subscribe('https://userpresence.xboxlive.com/users/xuid(<player_xuid>)/richpresence')

}

main()

When the player's presence changes, you will recieve an event like this in the event event.

{
	"xuid": 1234567890123456,
	"devicetype": "XboxOne",
	"titleid": 0,
	"string1": "Minecraft",
	"string2": "",
	"presenceState": "Online",
	"presenceText": "Minecraft",
	"presenceDetails": [
		{
			"isBroadcasting": false,
			"device": "iOS",
			"presenceText": "Minecraft", 
			"state": "Active", 
			"titleId": "1810924247",
			"isGame": true, 
			"isPrimary": true,
			"richPresenceText": ""
		}
	]
}

Debugging

You can enable some debugging output using the DEBUG enviroment variable. Through node.js, you can add process.env.DEBUG = 'xbox-rta' at the top of your code.

License

MIT