Net::Async::Matrix - use Matrix with IO::Async
my $loop = IO::Async::Loop->new;
my $matrix = Net::Async::Matrix->new(
server => "my.home.server",
$loop->add( $matrix );
user_id => '@my-user:home.server',
password => 'SeKr1t',
Matrix is an new open standard for interoperable Instant Messaging and VoIP, providing pragmatic HTTP APIs and open source reference implementations for creating and running your own real-time communication infrastructure.
This module allows an program to interact with a Matrix homeserver as a connected user client.
The following events are invoked, either using subclass methods or CODE references in parameters:
A request to write a debugging log message. This is provided temporarily for development and debugging purposes, but will at some point be removed when the code has reached a certain level of stability.
Invoked on receipt of a user presence change event from the homeserver. %changes will map user state field names to 2-element ARRAY references, each containing the old and new values of that field.
Invoked when a new room first becomes known about.
Passed an instance of Net::Async::Matrix::Room.
Invoked when the user has now left a room.
Invoked on receipt of a room invite. The $event will contain the plain Matrix event as received; with at least the keys inviter and room_id.
Invoked on receipt of any sort of event from the event stream, that is not recognised by any of the other code. This can be used to handle new kinds of incoming events.
The following named parameters may be passed to new or configure. In addition, CODE references for event handlers using the event names listed above can also be given.
Hostname and port number to contact the homeserver at. Given in the form
This string will be interpolated directly into HTTP request URLs.
Whether to use SSL/TLS to communicate with the homeserver. Defaults false.
Any other parameters whose names begin SSL_ will be stored for passing to the HTTP user agent. See IO::Socket::SSL for more detail.
Optional. Gives the path prefix to find the Matrix client API at. Normally this should not need modification.
Optional. Sets default event handlers on new room objects.
Optional. Normally enabled, but if set to a defined-but-false value (i.e. 0 or empty string) the event stream will be disabled. This will cause none of the incoming event handlers to be invoked, because the server will not be polled for events.
This may be useful in simple send-only cases where the client has no interest in receiveing any events, and wishes to reduce the load on the homeserver.
Optional. Timeout in seconds for the /events longpoll operation. Defaults to 30 seconds if not supplied.
Optional. Number of events per room to fetch on the first /sync request on startup. Defaults to the server's builtin value if not defined, which is likely to be 10.
The following methods documented with a trailing call to ->get return Future instances.
$matrix->login( %params )->get
Performs the necessary steps required to authenticate with the configured Home Server, actually obtain an access token and starting the event stream (unless disabled by the enable_events option being false). The returned Future will eventually yield the $matrix object itself, so it can be easily chained.
There are various methods of logging in supported by Matrix; the following sets of arguments determine which is used:
Log in via the m.login.password method.
Directly sets the user_id and access_token fields, bypassing the usual login semantics. This presumes you already have an existing access token to re-use, obtained by some other mechanism. This exists largely for testing purposes.
$matrix->register( %params )->get
Performs the necessary steps required to create a new account on the configured Home Server.
$matrix->sync( %params )->get
Performs a single /sync request on the server, returning the raw results directly.
Takes the following named parameters
Optional. Sync token from the previous request.
$f = $matrix->start
Performs the initial sync on the server, and starts the event stream to begin receiving events.
While this method does return a Future it is not required that the caller keep track of this; the object itself will store it. It will complete when the initial sync has fininshed, and the event stream has started.
If the initial sync has already been requested, this method simply returns the future it returned the last time, ensuring that you can await the client starting up simply by calling it; it will not start a second time.
Stops the event stream. After calling this you will need to use start again to continue receiving events.
$user = $matrix->myself
Returns the user object representing the connected user.
$user = $matrix->user( $user_id )
Returns the user object representing a user of the given ID, if defined, or undef.
$name = $matrix->get_displayname->get
$matrix->set_displayname( $name )->get
Accessor and mutator for the user account's "display name" profile field.
( $presence, $msg ) = $matrix->get_presence->get
$matrix->set_presence( $presence, $msg )->get
Accessor and mutator for the user's current presence state and optional status message string.
( $room, $room_alias ) = $matrix->create_room( $alias_localpart )->get
Requests the creation of a new room and associates a new alias with the given localpart on the server. The returned Future will return an instance of Net::Async::Matrix::Room and a string containing the full alias that was created.
$room = $matrix->join_room( $room_alias_or_id )->get
Requests to join an existing room with the given alias name or plain room ID. If this room is already known by the $matrix object, this method simply returns it.
$matrix->add_alias( $alias, $room_id )->get
$matrix->delete_alias( $alias )->get
Performs a directory server request to create the given room alias name, to point at the room ID, or to remove it again.
Note that this is likely only to be supported for alias names scoped within the homeserver the client is connected to, and that additionally some form of permissions system may be in effect on the server to limit access to the directory server.
$content_uri = $matrix->upload( %params )->get
Performs a post to the server's media content repository, to upload a new piece of content, returning the content URI that points to it.
The content can be specified in any of three ways, with the following three mutually-exclusive arguments:
Gives the content directly as an immediate scalar value.
Gives the path to a readable file on the filesystem containing the content.
Gives an opened IO handle the content can be read from.
The following additional arguments are also recognised:
Gives the MIME type of the content data.
Optional. If the content is being delivered from an opened filehandle (via the fh argument), this gives the total length in bytes. This is required in cases such as reading from pipes, when the length of the content isn't immediately available such as by stat()ing the filehandle.
$url = $matrix->convert_mxc_url( $mxc )
Given a plain string or URI instance containing a Matrix media URL (in the mxc: scheme), returns an http: or https: URL in the form of an URI instance pointing at the media repository on the user's local homeserver where it can be downloaded from.
Parameters documented as $user receive a user struct, which supports the following methods:
User ID of the user.
Profile displayname of the user.
Presence state. One of offline, unavailable or online.
Epoch time that the user was last active.
The following methods are not normally required by users of this class, but are provided for the convenience of subclasses to override.
Returns a new instance of Net::Async::Matrix::Room.
http://matrix.org/ - matrix.org home page
https://github.com/matrix-org - matrix.org on github
Paul Evans <email@example.com>
To install Net::Async::Matrix, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell
For more information on module installation, please visit the detailed CPAN module installation guide.