Skip to content

Commit

Permalink
event source
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Mar 24, 2019
1 parent ac02464 commit 5c4804c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
14 changes: 14 additions & 0 deletions LongPollingChat/docker/nginx/conf.d/longpolling.conf
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ server
push_stream_ping_message_interval 10s;
}

# Event source subscriber
#
location /es
{
push_stream_subscriber eventsource;

# configure channels path from argument in query string, e.g. /subscribe?channel=general
push_stream_channels_path $arg_channel;

# The time interval in which a keepalive message is sent to subscribers.
# If you do not want to send ping messages, just not set this directive.
push_stream_ping_message_interval 10s;
}

# Route used to mublish message to all subscribed clients.
# Subscription identified by channel, specified in "push_stream_channels_path"
#
Expand Down
17 changes: 15 additions & 2 deletions LongPollingChat/templates/chat.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<div class="dropdown-menu">
<a class="dropdown-item" href="#" data-transport="long-polling">long-polling</a>
<a class="dropdown-item" href="#" data-transport="websocket">websocket</a>
<a class="dropdown-item" href="#" data-transport="eventsource">eventsource</a>
</div>
</div>
<input type="text" name="message" class="form-control">
Expand Down Expand Up @@ -72,8 +73,16 @@
// get messages from websocket queue
const startWebSocket = function(channel) {
const w = new window.WebSocket('ws://longpolling/ws?channel=' + channel);
w.onmessage = function(e) {
const ws = new window.WebSocket('ws://longpolling/ws?channel=' + channel);
ws.onmessage = function(e) {
appendMessage(JSON.parse(e.data));
};
};
// get messages from websocket queue
const startEventSource = function(channel) {
const es = new window.EventSource('http://longpolling/es?channel=' + channel);
es.onmessage = function(e) {
appendMessage(JSON.parse(e.data));
};
};
Expand Down Expand Up @@ -107,6 +116,10 @@
// set new transport
if (transport === 'websocket') {
startWebSocket(channel);
} else if (transport === 'eventsource') {
startEventSource(channel);
} else if (transport === 'long-polling') {
startLongPolling(channel);
}
});
Expand Down

0 comments on commit 5c4804c

Please sign in to comment.