Activity Streams 2.0

At long last, the Activity Streams 2.0 specification is nearing W3C Candidate Recommendation stage. Hopefully the CR will post sometime next week.

As a reminder, Activity Streams is a JSON syntax for describing Activity (social or otherwise). For instance, the expression, “Krista added a photo to an album” can be represented using JSON such as:

{
"@context": "http://www.w3.org/ns/activitystreams",
"actor": {
"id": "http://krista.example.org",
"name": "Krista",
"type": "Person"
},
"type": "Add",
"object": {
"id": "http://photos.example.org/id/123abc",
"type": "Image",
"name": "Picture of my Cat",
"url": "http://photos.example.org/id/123abc.png"
},
"target": {
"id": "http://albums.example.org/id/cat-pics",
"name": "My Cat Pics",
"url": "http://albums.example.org/id/cat-pics"
}
}

The Activity Streams 2.0 specification comes in two parts:

The Core specification describes the JSON serialization, while the Vocabulary specification defines the object types and properties that can be represented.

What’s New in AS 2.0?

The first version of the Activity Streams specification was published in 2011 at http://activitystrea.ms/specs/json/1.0/. The 2.0 version represents a significant evolution that provides:

  • Improved Internationalization with explicit language context and bidirectional text support;
  • Improved Extensibility via JSON for Linked Data support;
  • Optionally Ordered Collections and built-in support for paging;
  • Support for Intransitive (or Object-less) Activities (e.g. Activities where the Actor is also the Object, like “Joe is traveling”);
  • A first-class Link model;
  • 28 built-in common types of Activities;
  • 13 built-in common types of Objects, including a Tombstone type used to represent deleted objects, and a Relationship type used to represent social network relationships;
  • Built-in audience targeting (this was previously an extension to Activity Streams 1.0);
  • Built-in geotagging;
  • The ability to identity both the origin and destination of an Activity (e.g. “Aaron moved the document from Folder A to Folder B”);
  • The ability to describe the result of an Activity;
  • And much more…

How can I use it?

If you use Node.js, then you can use the JavaScript implementation I’ve put together. Simply do:

$ npm install activitystrea.ms

The module supports the production and consumption of Activity Streams 2.0 objects but it does not do any persistence on it’s own. To create an Activity:

const as = require('activitystrea.ms');
const activity = as.add()
.actor(as.person()
.id('http://krista.example.org')
.name('Krista'))
.object(as.image()
.id('http://photos.example.org/id/123abc')
.name('Picture of my Cat')
.url('http://photos.example.org/id/123abc.png'))
.target(as.object()
.id('http://albums.example.org/id/cat-pics')
.name('My Cat Pics')
.url('http://albums.example.org/id/cat-pics'))
.get();

To serialize the `activity` object as JSON, call either the `write()` or `prettyWrite()` method:

activity.prettyWrite((err, doc) => {
if (err) throw err;
console.log(doc); // doc will be a string
});

Note that the `write()` and `prettyWrite()` methods are async only because there’s some JSON-LD stuff going on under the covers that requires async handling.

Parsing a serialized Activity is straightforward. The following example shows parsing using a stream:

const as = require('activitystrea.ms');
const fs = require('fs');
const AS2Stream = as.Stream;
const path = require('path');
const through = require('through2');
fs.createReadStream(path.resolve(__dirname,'test.json'))
.pipe(new AS2Stream())
.pipe(through.obj((obj,encoding,callback) => {
console.log(obj.type);
console.log(obj.name);
}));

View the activitystrea.ms modules README.md for more API details.

Like what you read? Give James M Snell a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.