PieSocket logo

Publish with Ruby

Use the following code snippet to publish a message on a WebSocket channel with Ruby.

require "uri"
require "net/http"
require "json"

url = URI("https://www.piesocket.com/api/publish")

https = Net::HTTP.new(url.host, url.port);
https.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Content-Type"] = "application/json"
request.body = {
    key: "oCdCMcMPQpbvNjUIzqtvF1d2X2okWpDQj4AwARJuAgtjhzKxVEjQU6IdCjwm", #Demo key, get yours at https://piesocket.com
    secret: "d8129f82f8dd71910aa4a7efa30a7297", #Demo secret, get yours at https://piesocket.com
    channelId: 1,
    message: { "text": "Hello world!" }
}.to_json

response = https.request(request)
puts response.read_body

This sends a JSON message to channel 1, to send a string, replace message: { "text": "Hello world!" } with message: "Hello world!"

Please note that this example uses a demo key and secret which is public. The demo key is only for quick testing purpose and you should not use this in your application.

You can get your free API key and API secret here.

Help

Facing difficulties? Use the chat box on the bottom-right corner of this page to reach us.