I'm using Rails5 to create an api provider to my application, and, I need use websocket.
Then, I created a very simple codes for tests:
routes.rb
Rails.application.routes.draw do
mount ActionCable.server => '/cable'
get '/testbroad', to: 'messages#testbroad'
end
messages_channel.rb
class MessagesChannel < ApplicationCable::Channel
def subscribed
stream_from 'messages'
end
end
messages_controller.rb
class MessagesController < ApplicationController
def testbroad
ActionCable.server.broadcast('messages',
message: 'foo')
head :ok
end
end
Then, I started the connection using cURL
➜ ~ curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: ws://localhost:3000/cable" http://localhost:3000/cable
HTTP/1.1 101 Web Socket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
WebSocket-Location: ws://ws://localhost:3000/cable/cable
{"type":"welcome"}
It's work! But, when I try to send the broadcast...
➜ ~ curl http://localhost:3000/testbroad
The Rails' console show success, but, I not receive any message in cURL. Why I not received the message "foo"? How to resolve it?