Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails actioncable alias request origins

I have an application with subdomains and I would like to be able to connect to a WebSocket of all of the subdomains when I add a subdomain to cable/config.ru

ActionCable.server.config.allowed_request_origins = ["domain.com",
 "sub1.domain.com", "sub1.domain.com"]

It works ok but how I can set alias for all subdomains somethink like this:

"[*.domain.com]"   

Char " * "dosn't work.

like image 271
Adrian Avatar asked Nov 16 '15 21:11

Adrian


2 Answers

Actioncable now supports regular expressions

Usage denoted below:

Action Cable will only accept requests from specified origins, which are passed to the server config as an array. The origins can be instances of strings or regular expressions, against which a check for match will be performed.

ActionCable.server.config.allowed_request_origins = ['http://rubyonrails.com', /http:\/\/ruby.*/]

like image 171
David Kuhta Avatar answered Sep 25 '22 07:09

David Kuhta


You can use regex for this case. In environments files (production and development) use this:

config.action_cable.allowed_request_origins = [/(?:^(http|https):\/\/)?(?:([^.]+)\.)?#{ENV["domain"]}/]

As the ENV["domain"] the domain that the application runs in.

like image 21
Pedro Samuel Avatar answered Sep 22 '22 07:09

Pedro Samuel