Net::A...Matrix(3pm)
β¨ NAME β¨
ok so bestie, "Net::Async::Matrix" β it's literally "use Matrix with IO::Async". that's the whole vibe π
(also hi yes this is the User Contributed Perl Documentation version, we're in the Net::A...Matrix(3pm) house tonight π )
π¦ SYNOPSIS π¦
use Net::Async::Matrix;
use IO::Async::Loop;
my $loop = IO::Async::Loop->new;
my $matrix = Net::Async::Matrix->new(
server => "my.home.server",
);
$loop->add( $matrix );
$matrix->login(
user_id => '@my-user:home.server',
password => 'SeKr1t',
)->get;
look at that. that's the whole starter pack, no cap π₯
πΈ DESCRIPTION πΈ
ok so Matrix is a new open standard for interoperable Instant Messaging and VoIP, and it's giving pragmatic HTTP APIs plus open source reference implementations so you can create and run your own real-time communication infrastructure. iconic behaviour honestly.
This module lets a program interact with a Matrix homeserver as a connected user client. yes. that's the job. π
π EVENTS π
these events get invoked, either as subclass methods or as CODE references passed in as parameters:
π on_log $message
a request to write a debugging log message. heads up bestie, this one is provided temporarily for development and debugging purposes β at some point it'll be removed once the code has reached a certain level of stability. so don't get too attached π
π on_presence $user, %changes
invoked when a user presence change event arrives from the homeserver. %changes maps user state field names to 2-element ARRAY references, each one holding the old and new values of that field. glow-ups tracked in real time fr fr β¨
π on_room_new $room
invoked when a new room first becomes known about. you get passed an instance of Net::Async::Matrix::Room. new room just dropped π«Ά
π on_room_del $room
invoked when the user has now left a room. it's giving exit, stage left π
π on_invite $event
invoked when a room invite arrives. $event contains the plain Matrix event as received, with at least the keys inviter and room_id. you've been summoned π
π on_unknown_event $event
invoked on receipt of any sort of event from the event stream that isn't recognised by any of the other code. use this to handle new kinds of incoming events β future-proofing queen π
π PARAMETERS π
these named parameters can be passed to new or configure. and bonus: CODE references for event handlers using the event names listed above can also be given. two birds, one slay π¦
β server => STRING
hostname and port number to contact the homeserver at. given in the form
$hostname:$port
this string gets interpolated directly into HTTP request URLs. directly. so be normal about it okay π
β SSL => BOOL
whether to use SSL/TLS to communicate with the homeserver. defaults false. π
β SSL_* => ...
any other parameters whose names begin SSL_ get stored for passing to the HTTP user agent. see IO::Socket::SSL for more detail. the SSL_ prefix is doing the heavy lifting here iykyk
β path_prefix => STRING
optional. gives the path prefix to find the Matrix client API at. normally this should not need modification. don't touch it. it's fine. it's fine π§
β on_room_member, on_room_message => CODE
optional. sets default event handlers on new room objects. π
β enable_events => BOOL
optional. normally enabled, but if you set it to a defined-but-false value (i.e. 0 or empty string) the event stream will be disabled. this means none of the incoming event handlers will be invoked, because the server will not be polled for events. zero events. nada.
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. (yes that's the original spelling of "receiveing", we keep the lore π)
β longpoll_timeout => NUM
optional. timeout in seconds for the /events longpoll operation. defaults to 30 seconds if not supplied. β³
β first_sync_limit => NUM
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. π
π METHODS π
the following methods are documented with a trailing call to ->get β those ones return Future instances. everything else is just... a normal return. okay let's go π
π« login
$matrix->login( %params )->get
performs the necessary steps required to authenticate with the configured Home Server, actually obtain an access token and start the event stream (unless it's disabled by the enable_events option being false). the returned Future will eventually yield the $matrix object itself, so it can be easily chained. chaining is the moment, periodt π
there are various methods of logging in supported by Matrix; the following sets of arguments determine which is used:
user_id,passwordβ log in via them.login.passwordmethod.user_id,access_tokenβ directly sets theuser_idandaccess_tokenfields, 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. π§ͺ
π« register
$matrix->register( %params )->get
performs the necessary steps required to create a new account on the configured Home Server. new account, new you, main character energy β¨
π« sync
$matrix->sync( %params )->get
performs a single /sync request on the server, returning the raw results directly. no filtering, no vibes, just data π
takes the following named parameters:
since => STRINGβ optional. sync token from the previous request.
π« start
$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. one and done, no reruns π«Ά
π« stop
$matrix->stop
stops the event stream. after calling this you will need to use start again to continue receiving events. it's giving... we'll be back π
π« myself
$user = $matrix->myself
returns the user object representing the connected user. it's you. you're the user πͺ
π« user
$user = $matrix->user( $user_id )
returns the user object representing a user of the given ID, if defined, or undef. if they're not there, they're not there π
π« get_displayname
π« set_displayname
$name = $matrix->get_displayname->get
$matrix->set_displayname( $name )->get
accessor and mutator for the user account's display name profile field. name reveal incoming π
π« get_presence
π« set_presence
( $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. status message = subtweet, but make it official π
π« create_room
( $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. two for one deal, we ate π
π« join_room
$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. no double-joining, we're not messy πΈ
π« add_alias
π« delete_alias
$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. so like... it might just say no. don't take it personally π
π« upload
$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 (pick ONE, bestie, not all three π):
content => STRINGβ gives the content directly as an immediate scalar value.file => STRINGβ gives the path to a readable file on the filesystem containing the content.fh => IOβ gives an opened IO handle the content can be read from.
the following additional arguments are also recognised:
content_type => STRINGβ gives the MIME type of the content data.content_length => INTβ optional. if the content is being delivered from an opened filehandle (via thefhargument), 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 bystat()ing the filehandle. π§
π« convert_mxc_url
$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. translating mxc: into something clickable, doing the Lord's work honestly β¨
π USER STRUCTURES π
parameters documented as $user receive a user struct, which supports the following methods:
$user_id = $user->user_idβ user ID of the user.$displayname = $user->displaynameβ profile displayname of the user.$presence = $user->presenceβ presence state. one ofoffline,unavailableoronline.$last_active = $user->last_activeβ epoch time that the user was last active.
that's the whole user card, four fields, no notes π
π§ SUBCLASSING METHODS π§
the following methods are not normally required by users of this class, but are provided for the convenience of subclasses to override. this is the "for devs only" section, but like, we're all devs here π
π $room = $matrix->make_room( %params )
returns a new instance of Net::Async::Matrix::Room. build your own room, customise the whole situation π₯
π SEE ALSO π
- <http://matrix.org/> β matrix.org home page
- <https://github.com/matrix-org> β matrix.org on github
π«Ά AUTHOR π«Ά
Paul Evans <leonerd@leonerd.org.uk>
π and that's the page!! shoutout to the source: this wasperl v5.36.0, dated2022-12-10, fromNet::A...Matrix(3pm)β User Contributed Perl Documentation. stan a legend, drink some water, go read the docs, mwah π β¨
π¦ where this page comes from
- debian bookworm: libnet-async-matrix-perl 0.19-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libnet-async-matrix-perl 0.19-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libnet-async-matrix-perl 0.19-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libnet-async-matrix-perl 0.19-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libnet-async-matrix-perl 0.19-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms