.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Swagger2::Guides::WebSocket 3pm" .TH Swagger2::Guides::WebSocket 3pm "2016-04-17" "perl v5.22.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Swagger2::Guides::WebSocket \- How to expose Swagger over WebSockets .SH "OVERVIEW" .IX Header "OVERVIEW" This guide will show how to expose a Swagger \s-1API\s0 over WebSockets. .PP This feature is \s-1EXPERIMENTAL\s0 and is subject to change. .PP See for a working example. .SH "DISCLAIMER" .IX Header "DISCLAIMER" This way of exposing Swagger over WebSockets is in no way compatible with . .SH "SYNOPSIS" .IX Header "SYNOPSIS" .SS "Application" .IX Subsection "Application" .Vb 2 \& package MyApp; \& use Mojo::Base "Mojolicious"; \& \& sub startup { \& my $app = shift; \& my $ws = $app\->routes\->websocket("/ws")\->to("events#ws"); \& \& $app\->plugin(Swagger2 => { \& url => app\->home\->rel_file("api.yaml"), \& ws => $ws, \& }); \& } .Ve .PP In the example application class above, we create a custom route object for handling the WebSocket request. The route object \f(CW$ws\fR is then passed on to the plugin and set up with a default route variable swagger. .PP The reason why we create a custom websocket route is so it can be used bi-directional, instead of just dispatching to Swagger actions. .SS "Controller" .IX Subsection "Controller" .Vb 1 \& package MyApp::Controller::Events; \& \& sub ws { \& my $c = shift; \& \& $c\->on(json => sub { \& my ($c, $data) = @_; \& return if $c\->dispatch_to_swagger($data); \& }); \& } .Ve .PP In the example controller above we listen to a json event which can dispatch_to_swagger. .PP This method will return boolean true if the input looks like a Swagger request. .SS "Request" .IX Subsection "Request" The input data to the WebSocket need to be \s-1JSON\s0 and look like this: .PP .Vb 7 \& { \& "id": "some unique string", \& "op": "operationId", \& "params": { \& "paramerName": "value" \& } \& } .Ve .PP The \*(L"id\*(R" is used to map the response to a unique request. This means that the \&\*(L"id\*(R" need to be generated on the client side. The uniqueness requirement is only for this WebSocket connection. .PP \&\*(L"op\*(R" need to match an operationId in the Swagger specification. .PP \&\*(L"params\*(R" must be an object where the keys match the parameters in the Swagger specification. .SS "Response" .IX Subsection "Response" .Vb 5 \& { \& "id": "some unique string", \& "code": 200, \& "body": {"any":"thing"} \& } .Ve .PP The response contains the input \*(L"id\*(R", the \s-1HTTP\s0 status \*(L"code\*(R" and the response from the Swagger action inside \*(L"body\*(R". .SS "JavaScript example" .IX Subsection "JavaScript example" Here is an example JavaScript which can communicate over the socket: .PP .Vb 1 \& var ws = new WebSocket("ws://example.com/ws"); \& \& ws.onopen = function () { \& ws.send(JSON.stringify({ \& id: "42", \& op: "listPets", \& args: {limit: 60} \& }); \& }; \& \& ws.onmessage = function (e) { \& var data = JSON.parse(e.data); \& if (data.id == "42") { \& // Response from the request above, because of matching "id" \& console.log(data.code, data.body); \& } \& }; .Ve .SH "CAVEATS" .IX Header "CAVEATS" .IP "\(bu" 4 \&\*(L"x\-mojo-around-action\*(R" is ignored when issuing WebSocket requests. This may be changed in future version. .IP "\(bu" 4 Sharing a WebSocket route object between multiple Swagger plugins is unsupported. (The outcome is unknown) .IP "\(bu" 4 The protocol and implementation is subject for change. feedback is highly appreciated. .SH "AUTHOR" .IX Header "AUTHOR" Jan Henning Thorsen \- \f(CW\*(C`jhthorsen@cpan.org\*(C'\fR