Websocket Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is WebSocket? And why do we need it?

A

WebSockets are a protocol built on top of TCP ( As HTTP for example) . They hold the connection to the server open so that the server can send information to the client, even in the absence of a request from the client. WebSockets allow for bi-directional, “full-duplex” communication between the client and the server by creating a persistent connection between the two.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Connection?

A

A place for authorizing connection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Channel?

A

A place for common logic between channels.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to generate new channel?

A

rails g channel channel_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to broadcast event?

A

ActionCable.server.broadcast “channel_name”, {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What files creates rails channel generator?

A
# app/channels/animals_channel.rb
class AnimalsChannel < ApplicationCable::Channel
  def subscribed
    # stream_from "some_channel"
  end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end

app/javascript/channels/animals_channel.js

consumer.subscriptions.create("AnimalsChannel", {
  connected() {
    // Called when the subscription is ready for use on the server
  },
  disconnected() {
    // Called when the subscription has been terminated by the server
  },
  received(data) {
    // Called when there's incoming data on the websocket for this channel
  }
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What files creates rails channel generator? ( 2 files and 5 methods)

A
# app/channels/animals_channel.rb
class AnimalsChannel < ApplicationCable::Channel
  def subscribed
    # stream_from "some_channel"
  end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end

app/javascript/channels/animals_channel.js

consumer.subscriptions.create("AnimalsChannel", {
  connected() {
    // Called when the subscription is ready for use on the server
  },
  disconnected() {
    // Called when the subscription has been terminated by the server
  },
  received(data) {
    // Called when there's incoming data on the websocket for this channel
  }
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to create a stream subscription?

A

def subscribed
stream_from “sample”
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the main elements to create websocket event? ( 5 things)

A

1) Button
2) Action inside controller
3) Event which we run from action
4) A subscription that catch this event
5) JS that show the result on every page

How well did you know this?
1
Not at all
2
3
4
5
Perfectly