Skip to content

Instantly share code, notes, and snippets.

@DanKnox
Last active February 10, 2016 14:00
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DanKnox/5587677 to your computer and use it in GitHub Desktop.
Save DanKnox/5587677 to your computer and use it in GitHub Desktop.
Format of WebsocketRails Messages
/****** Current Format ******/
// Basic Message
metadata = {
id: 1234234234, // Randomly generated by the client. Used for receiving success/fail callbacks
client_id: 123456 // Sent from the server with the `websocket_rails.client_connected` event which is sent after the connection is opened. Store this and send it with each message.
data: {name: 'shoes'} // Arbitrary data. Can be Object or String. This is available through #message or #data in the controller.
}
event = ["products.new", metadata]
messsage = JSON.stringify(event)
// Server Response using #trigger_succcess or #trigger_failure methods
metadata = {
id: 1234234234, // Matches event id from client. Used for triggering success/fail callbacks
success: true, // *Optional* Boolean or null to trigger success or failure callback
result: {message: 'product created successfully'}, // *Optional* Arbitrary data object or string to pass to success or failure callback
}
["products.new", metadata]
/*** Right now the server actually sends an array of events together.
* Typically this array only ever includes a single event though.
* So when receiving from the server and parsing the JSON you will end up with
* something like the following:
*/
events = JSON.parse(messages)
console.log events[0]
["event.name",metadata_object]
// Server Message Using #send_message method
metadata = {
data: "this is a response", // Data sent from the controller action
}
["event.name", metadata]
// Client Channel Message
metadata = {
id: 1234234234, // Randomly generated by the client. Used for receiving success/fail callbacks
client_id: 123456 // Sent from the server with the `websocket_rails.client_connected` event which is sent after the connection is opened. Store this and send it with each message.
channel: "awesome_channel", // *Optional* Channel name if this is a channel message
data: "this is a channel message"
}
// Server Response Channel Message. This is broadcasted to all connected clients to this channel.
metadata = {
channel: "awesome_channel", // *Optional* Channel name if this is a channel message
data: "this is a channel message"
}
["event.name", metadata]
/****** Future Format ******/
// I want to refactor the message format in a future release to a three element array
// where the first element is the event name, the second element is the data for the
// event and the third element is any metadata.
// Examples of future format:
// Basic Message
metadata = {
id: 1234234234, // Randomly generated by the client. Used for receiving success/fail callbacks
client_id: 123456 // Sent from the server with the `websocket_rails.client_connected` event which is sent after the connection is opened
}
event = ["products.new", {name: 'shoes'}, metadata]
messsage = JSON.stringify(event)
// Server Response using #trigger_succcess or #trigger_failure methods
metadata = {
id: 1234234234, // Matches event id from client. Used for triggering success/fail callbacks
success: true, // *Optional* Boolean or null to trigger success or failure callback
result: {message: 'product created successfully'}, // *Optional* Arbitrary data object or string to pass to success or failure callback
}
["products.new",{},metadata]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment