Aji Achmad
Aji Achmad

Reputation: 43

Websocket in rails not working because of routes

I create web app in rails using websocket. I create add below in routes.rb

get '/:username' => 'users#profile', as:"profile"

after this, i add bellow in Gemfile

gem 'websocket-rails'

and then I add below in application.js

var dispatcher = new WebSocketRails('localhost:3000/websocket');
channel = dispatcher.subscribe('articles');
channel.bind('new', function(article) {
  console.log('a new article about '+article.id+' arrived!');
})

The problem is

localhost:3000/websocket =====> run profile_path (get '/:username' => 'users#profile', as:"profile")

how to solve my websocket problem without removing { get '/:username' => 'users#profile', as:"profile" }..?

I am new user in developing rails using web socket.

Upvotes: 1

Views: 563

Answers (1)

maxm
maxm

Reputation: 3667

I fixed this using a routing constraint:

get '/:hash', to: 'conversations#show', constraints: {hash: /((?!websocket).)*/}

The route will not work unless the :hash does not contain the string 'websocket'

I'm sure there's a better regex out there, and if my hash ever randomly contains the string "websocket" it'll fail.

But, this fixes your problem for me in development.

Upvotes: 1

Related Questions