-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsing server-sent events - Web APIs _ MDN.html
More file actions
1118 lines (739 loc) · 67.3 KB
/
Using server-sent events - Web APIs _ MDN.html
File metadata and controls
1118 lines (739 loc) · 67.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!-- saved from url=(0092)https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events -->
<html lang="en-US" dir="ltr" class="redesign " data-ffo-opensanslight="true" data-ffo-opensans="true"><head prefix="og: http://ogp.me/ns#"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<script async="" src="./Using server-sent events - Web APIs _ MDN_files/analytics.js"></script><script>(function(d) { d.className = d.className.replace(/\bno-js/, ''); })(document.documentElement);</script>
<title>Using server-sent events - Web APIs | MDN</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="index, follow">
<link rel="home" href="https://developer.mozilla.org/en-US/">
<link rel="copyright" href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#copyright">
<link href="./Using server-sent events - Web APIs _ MDN_files/mdn.b6e66f90df84.css" rel="stylesheet" type="text/css">
<link href="./Using server-sent events - Web APIs _ MDN_files/wiki.197273d82cbe.css" rel="stylesheet" type="text/css">
<!-- common social tags -->
<meta property="og:type" content="website">
<meta property="og:image" content="https://developer.cdn.mozilla.net/static/img/opengraph-logo.dc4e08e2f6af.png">
<meta property="og:site_name" content="Mozilla Developer Network">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://developer.cdn.mozilla.net/static/img/opengraph-logo.dc4e08e2f6af.png">
<meta name="twitter:site" content="@MozDevNet">
<meta name="twitter:creator" content="@MozDevNet">
<link rel="search" type="application/opensearchdescription+xml" href="https://developer.mozilla.org/en-US/search/xml" title="Mozilla Developer Network">
<!-- third-generation iPad with high-resolution Retina display: -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="https://developer.cdn.mozilla.net/static/img/favicon144.a6e4162070f4.png">
<!-- iPhone with high-resolution Retina display: -->
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="https://developer.cdn.mozilla.net/static/img/favicon114.0e9fabd44f85.png">
<!-- first- and second-generation iPad: -->
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="https://developer.cdn.mozilla.net/static/img/favicon72.8ff9d87c82a0.png">
<!-- non-Retina iPhone, iPod Touch, and Android 2.1+ devices: -->
<link rel="apple-touch-icon-precomposed" href="https://developer.cdn.mozilla.net/static/img/favicon57.a2490b9a2d76.png">
<!-- basic favicon -->
<link rel="shortcut icon" href="https://developer.cdn.mozilla.net/static/img/favicon32.e02854fdcf73.png">
<!--[if IE]>
<meta http-equiv="imagetoolbar" content="no">
<script type="text/javascript" src="https://developer.cdn.mozilla.net/static/build/js/html5shiv.3948ccddab6f.js" charset="utf-8"></script>
<![endif]-->
<link rel="alternate" type="application/json" href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$json">
<link rel="canonical" href="./Using server-sent events - Web APIs _ MDN_files/Using server-sent events - Web APIs _ MDN.html">
<link rel="alternate" hreflang="es" href="https://developer.mozilla.org/es/docs/Server-sent_events/utilizando_server_sent_events_sse" title="Utilizando eventos enviados por el servidor (server-sent event)">
<link rel="alternate" hreflang="fa" href="https://developer.mozilla.org/fa/docs/Server-sent_events/Using_server-sent_events" title="Using server-sent events">
<link rel="alternate" hreflang="fr" href="https://developer.mozilla.org/fr/docs/Server-sent_events/Using_server-sent_events" title="Using server-sent events">
<link rel="alternate" hreflang="ja" href="https://developer.mozilla.org/ja/docs/Server-sent_events/Using_server-sent_events" title="Server-Sent Events の利用">
<link rel="alternate" hreflang="zh-CN" href="https://developer.mozilla.org/zh-CN/docs/Server-sent_events/Using_server-sent_events" title="使用服务器发送事件">
<link rel="alternate" hreflang="zh-TW" href="https://developer.mozilla.org/zh-TW/docs/Web/API/Server-sent_events/Using_server-sent_events" title="使用 server-sent 事件">
<!-- document-specific social tags -->
<meta property="og:title" content="Using server-sent events">
<meta property="og:url" content="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events">
<meta name="twitter:url" content="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events">
<meta name="twitter:title" content="Using server-sent events">
<meta property="og:description" content="The server-sent event API is contained in the EventSource interface; to open a connection to the server to begin receiving events from it, create a new EventSource object specifying the URI of a script that generates the events. For example:">
<meta name="description" content="The server-sent event API is contained in the EventSource interface; to open a connection to the server to begin receiving events from it, create a new EventSource object specifying the URI of a script that generates the events. For example:">
<meta name="twitter:description" content="The server-sent event API is contained in the EventSource interface; to open a connection to the server to begin receiving events from it, create a new EventSource object specifying the URI of a script that generates the events. For example:">
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-36116321-5', 'mozilla.org');
ga('set', 'anonymizeIp', true);
// dimension9 == "Section editing"
ga('set', 'dimension9', 'Enabled');
(function() {
// http://cfsimplicity.com/61/removing-analytics-clutter-from-campaign-urls
var win = window;
var removeUtms = function(){
var location = win.location;
if (location.href.indexOf('utm') != -1 && win.history.replaceState) {
win.history.replaceState({}, '', location.pathname);
}
};
ga('send', 'pageview', {'hitCallback': removeUtms});
})();
</script>
<script async="" data-manual="true" src="./Using server-sent events - Web APIs _ MDN_files/syntax-prism.7a66ddfa68bf.js"></script></head>
<body data-slug="Web/API/Server-sent_events/Using_server-sent_events" contextmenu="edit-history-menu" data-search-url="" class="document ">
<script type="text/javascript">
(function(win) {
'use strict';
(function(){
var FLAGS = {
'compat_api': false,'kumaediting': false,'mdn10_logo': false,'mdn10_tile': false,'page_move': false,'registration_disabled': false,'search_suggestions': false,'section_edit': true,'spam_checks_enabled': false,'spam_submissions_enabled': false,'wiki_samples': true,'wiki_spam_exempted': false
},
SWITCHES = {
'wiki_error_on_delete': false,'wiki_force_immediate_rendering': false,'enable_optimizely': false,'welcome_email': true,'application_ACAO': true,'store_revision_ips': true,'dumb_doc_urls': true
},
SAMPLES = {
};
window.waffle = {
"flag_is_active": function waffle_flag(flag_name) {
return !!FLAGS[flag_name];
},
"switch_is_active": function waffle_switch(switch_name) {
return !!SWITCHES[switch_name];
},
"sample_is_active": function waffle_sample(sample_name) {
return !!SAMPLES[sample_name];
},
"FLAGS": FLAGS,
"SWITCHES": SWITCHES,
"SAMPLES": SAMPLES
};
})();
// This needs to be set before ckeditor.js loads
window.CKEDITOR_BASEPATH = '/static/js/libs/ckeditor/build/ckeditor/';
// This represents the site configuration
win.mdn = {
// Properties and settings for CKEditor will go here
ckeditor: {},
// Feature test results and methods will be placed here
features: {},
// The path to static assets (images, CSS, JS) in MDN
staticPath: 'https://developer.cdn.mozilla.net/static/',
// Optimizely API
optimizely: win['optimizely'] || [],
// Site notifications
notifications: [],
// Wiki-specific settings will be placed here
wiki: {
autosuggestTitleUrl: '/en-US/docs/get-documents'
},
// Assets that need to be dynamically injected
assets: {
css: {
'editor-content': ['https://developer.cdn.mozilla.net/static/build/styles/editor-content.c7ceac3c1ebf.css',],
'wiki-compat-tables': ['https://developer.cdn.mozilla.net/static/build/styles/wiki-compat-tables.f3a3bfce97a1.css',]
},
js: {
'syntax-prism': ['https://developer.cdn.mozilla.net/static/build/js/syntax-prism.7a66ddfa68bf.js',],
'wiki-compat-tables': ['https://developer.cdn.mozilla.net/static/build/js/wiki-compat-tables.14ce5dcb2c3d.js',]
}
}
};
})(this);
</script>
<div class="global-notice">
<div class="wrap center">
<p><bdi>We want to help developers like you. Tell us how you use developer services: <a href="https://qsurvey.mozilla.com/s3/da2a7841fe76">https://qsurvey.mozilla.com/s3/da2a7841fe76</a></bdi></p>
</div>
</div>
<ul id="nav-access">
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#document-main" id="skip-main">Skip to main content</a></li>
<li><a id="skip-language" href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#language">Select language</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#q" id="skip-search">Skip to search</a></li>
</ul>
<!-- Header -->
<header id="main-header"><div class="center">
<div id="tabzilla">
<a href="https://www.mozilla.org/" class="no-track">mozilla</a>
</div>
<div class="clear header-clear"></div>
<a href="https://developer.mozilla.org/en-US/" class="logo">Mozilla Developer Network</a>
<div id="nav-sec">
<ul>
<li>
<form>
<div class="oauth-login-container">
<div class="oauth-login-options" tabindex="0">
<span class="oauth-login-options-text">
<span class="oauth-login-options-text-icons">Sign in with</span>
<span class="oauth-login-options-text-no-icons">Sign in</span>
</span>
<span class="oauth-icon oauth-persona launch-persona-login wait-for-persona" data-service="Persona" data-next="/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events" title="Persona"><i class="icon-user" aria-hidden="true"></i></span>
<span class="oauth-icon oauth-github" data-service="GitHub" data-href="/users/github/login/?next=%2Fen-US%2Fdocs%2FWeb%2FAPI%2FServer-sent_events%2FUsing_server-sent_events" title="GitHub"><i class="icon-github" aria-hidden="true"></i></span>
</div>
<div class="oauth-login-picker" aria-hidden="true">
<ul>
<li class="wait-for-persona"><a href="https://developer.mozilla.org/en-US/users/signin" class="login-link launch-persona-login" data-next="/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events" data-service="Persona"><i class="icon-user" aria-hidden="true"></i>Persona</a></li>
<li><a href="https://developer.mozilla.org/users/github/login/?next=%2Fen-US%2Fdocs%2FWeb%2FAPI%2FServer-sent_events%2FUsing_server-sent_events" class="login-link" data-service="GitHub"><i class="icon-github" aria-hidden="true"></i>GitHub</a></li>
</ul>
</div>
</div>
</form>
</li>
</ul>
</div>
<nav id="main-nav" role="navigation"><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web">Web Platform<i aria-hidden="true" class="icon-caret-down"></i></a>
<div class="submenu submenu-cols-2 js-submenu" id="nav-platform-submenu">
<div class="submenu-column">
<div class="title">Technologies</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML">HTML</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS">CSS</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript">JavaScript</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/Guide/Graphics">Graphics</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API">APIs / DOM</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/Apps">Apps</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/MathML">MathML</a></li>
</ul>
</div><div class="submenu-column last">
<div class="title">References & Guides</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Learn">Learn the Web</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/Tutorials">Tutorials</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/Reference">References</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/Guide">Developer Guides</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility">Accessibility</a></li>
<li><a href="https://developer.mozilla.org/en-US/demos/">Demos</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web">...more docs</a></li>
</ul>
</div>
</div>
</li><li><a href="https://developer.mozilla.org/en-US/docs/Zones">Mozilla Docs<i aria-hidden="true" class="icon-caret-down"></i></a>
<div class="submenu js-submenu" id="nav-zones-submenu">
<div class="submenu-column">
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons">Add-ons</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Firefox">Firefox</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Marketplace">Firefox Marketplace</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Firefox_OS">Firefox OS</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Persona">Persona</a></li>
</ul>
</div>
</div>
</li><li><a href="https://developer.mozilla.org/en-US/docs/Tools">Developer Tools</a></li><li><a href="https://developer.mozilla.org/en-US/demos/">Demos</a></li><li><a href="https://developer.mozilla.org/en-US/docs/MDN/Feedback">Feedback<i aria-hidden="true" class="icon-caret-down"></i></a>
<div class="submenu js-submenu" id="nav-contact-submenu">
<div class="submenu-column">
<ul>
<li><a href="https://support.mozilla.org/">Get Firefox help<i aria-hidden="true" class="icon-external-link"></i></a></li>
<li><a href="http://stackoverflow.com/">Get web development help<i aria-hidden="true" class="icon-external-link"></i></a></li>
</ul>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/MDN/Community">Join the MDN community</a></li>
<li><a href="https://bugzilla.mozilla.org/form.doc">Report a content problem<i aria-hidden="true" class="icon-external-link"></i></a></li>
<li><a href="https://bugzilla.mozilla.org/form.mdn">Report a bug<i aria-hidden="true" class="icon-external-link"></i></a></li>
</ul>
</div>
</div>
</li><li class="nav-search-link"><a href="https://developer.mozilla.org/en-US/search" title="Search"><i aria-hidden="true" class="icon-search"></i></a></li><li class="main-nav-search"><form action="https://developer.mozilla.org/en-US/search" method="get" role="search">
<div class="search-wrap">
<label for="main-q" class="offscreen">Search</label>
<input type="search" id="main-q" name="q" placeholder="Search" data-value="" value="">
<span class="search-trigger"><i aria-hidden="true" class="icon-search"></i></span>
<button type="submit" class="offscreen">Search</button>
</div>
</form></li></ul></nav>
</div></header>
<!-- Content will go here -->
<main id="content"><div class="center clear">
<!-- end is_zone -->
<div class="wiki-main-content" id="document-main" role="main"><div class="center">
<div class="article-meta">
<!-- action buttons -->
<ul class="page-buttons" data-sticky="false">
<li><button id="languages-menu" class="transparent" aria-haspopup="true" aria-owns="languages-menu-submenu" aria-expanded="false"><span>Languages</span><i aria-hidden="true" class="icon-globe"></i></button>
<div class="submenu js-submenu" id="languages-menu-submenu">
<div class="submenu-column">
<ul id="translations">
<li><bdi><a rel="internal" href="https://developer.mozilla.org/es/docs/Server-sent_events/utilizando_server_sent_events_sse" title="Utilizando eventos enviados por el servidor (server-sent event)">Español</a></bdi></li>
<li><bdi><a rel="internal" href="https://developer.mozilla.org/fa/docs/Server-sent_events/Using_server-sent_events" title="Using server-sent events">فارسی</a></bdi></li>
<li><bdi><a rel="internal" href="https://developer.mozilla.org/fr/docs/Server-sent_events/Using_server-sent_events" title="Using server-sent events">Français</a></bdi></li>
<li><bdi><a rel="internal" href="https://developer.mozilla.org/ja/docs/Server-sent_events/Using_server-sent_events" title="Server-Sent Events の利用">日本語</a></bdi></li>
<li><bdi><a rel="internal" href="https://developer.mozilla.org/zh-CN/docs/Server-sent_events/Using_server-sent_events" title="使用服务器发送事件">中文 (简体)</a></bdi></li>
<li><bdi><a rel="internal" href="https://developer.mozilla.org/zh-TW/docs/Web/API/Server-sent_events/Using_server-sent_events" title="使用 server-sent 事件">正體中文 (繁體)</a></bdi></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$locales" rel="nofollow, noindex" id="translations-add">Add a translation</a></li>
</ul>
</div>
</div>
</li>
<li class="page-buttons-edit"><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$edit" class="button" data-optimizely-hook="button-edit-doc" id="edit-button">Edit<i aria-hidden="true" class="icon-pencil"></i></a></li>
<li><button id="advanced-menu" class="only-icon" aria-haspopup="true" aria-owns="advanced-menu-submenu" aria-expanded="false"><span>Advanced</span><i aria-hidden="true" class="icon-cog"></i></button>
<div class="submenu js-submenu" id="advanced-menu-submenu">
<!-- this page -->
<div class="submenu-column">
<div class="title">Advanced</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$history" rel="nofollow, noindex">History</a></li>
<li class="page-print"><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#" onclick="return window.print();">Print this article</a></li>
</ul>
</div>
</div>
</li></ul>
<!-- crumbs -->
<nav class="crumbs" role="navigation"><ol xmlns:v="http://rdf.data-vocabulary.org/#" aria-label="breadcrumbs">
<li typeof="v:Breadcrumb"><a href="https://developer.mozilla.org/en-US" rel="v:url" property="v:title">MDN</a></li>
<li class="crumb" typeof="v:Breadcrumb"><a href="https://developer.mozilla.org/en-US/docs/Web" rel="v:url" property="v:title">Web technology for developers</a></li>
<li class="crumb" typeof="v:Breadcrumb"><a href="https://developer.mozilla.org/en-US/docs/Web/API" rel="v:url" property="v:title">Web APIs</a></li>
<li class="crumb" typeof="v:Breadcrumb"><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events" rel="v:url" property="v:title">Server-sent events</a></li>
<li class="crumb" typeof="v:Breadcrumb" property="v:title">Using server-sent events</li>
</ol></nav>
</div>
<!-- heading -->
<div id="wiki-document-head" class="document-head">
<span class="from-search-previous-box">
<a class="button from-search-previous only-icon disabled" title="Previous Search Result" data-empty-title="No Previous Search Result"><i aria-hidden="true" class="icon-chevron-left"></i></a>
</span>
<span class="from-search-navigate-wrap">
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#" class="from-search-navigate"><span class="from-search-navigate-up"><i aria-hidden="true" class="icon-double-angle-up"></i></span><span class="from-search-navigate-down"><i aria-hidden="true" class="icon-double-angle-down"></i></span></a>
</span>
<div class="from-search-toc submenu">
<span class="title">Your Search Results</span>
<ol></ol>
</div>
<span class="from-search-next-box">
<a class="button from-search-next only-icon disabled" title="Next Search Result" data-empty-title="No Previous Search Result"><i aria-hidden="true" class="icon-chevron-right"></i></a>
</span>
<div class="contributor-avatars" data-all-text="see all contributors" data-alternate-message="hide contributors" data-has-hidden="1">
<ul>
<li class="shown">
<a href="https://developer.mozilla.org/en-US/profiles/teoli" title="View profile: teoli">
<img src="./Using server-sent events - Web APIs _ MDN_files/d0d90148e08e3b64ccf75c46f31442c5" class="avatar loaded" height="34" width="34" alt="teoli"></a>
</li>
<li class="shown">
<a href="https://developer.mozilla.org/en-US/profiles/dsheiko" title="View profile: dsheiko">
<img src="./Using server-sent events - Web APIs _ MDN_files/20b14eadec7da0f5796ec6297cbe9fb9" class="avatar loaded" height="34" width="34" alt="dsheiko"></a>
</li>
<li class="shown">
<a href="https://developer.mozilla.org/en-US/profiles/crandmck" title="View profile: crandmck">
<img src="./Using server-sent events - Web APIs _ MDN_files/512abdfbbb0c2d3ce8723d6c686d7f25" class="avatar loaded" height="34" width="34" alt="crandmck"></a>
</li>
<li class="shown">
<a href="https://developer.mozilla.org/en-US/profiles/saikrishna1995" title="View profile: saikrishna1995">
<img src="./Using server-sent events - Web APIs _ MDN_files/49382c2bcb47b36ee025300fa0b649ab" class="avatar loaded" height="34" width="34" alt="saikrishna1995"></a>
</li>
<li class="shown">
<a href="https://developer.mozilla.org/en-US/profiles/openmando" title="View profile: openmando">
<img src="./Using server-sent events - Web APIs _ MDN_files/c33d1f279931c6cca13fab9f287a4b0f" class="avatar loaded" height="34" width="34" alt="openmando"></a>
</li>
<li class="shown">
<a href="https://developer.mozilla.org/en-US/profiles/afoster" title="View profile: afoster">
<img src="./Using server-sent events - Web APIs _ MDN_files/d7545d81337f0202d7e46f1d8e297045" class="avatar loaded" height="34" width="34" alt="afoster"></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/chrisdavidmills" title="View profile: chrisdavidmills">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="chrisdavidmills" data-src="https://secure.gravatar.com/avatar/deeea0ba82390bd5acc539703c4b0cfb?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">chrisdavidmills</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/aspyatkin" title="View profile: aspyatkin">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="aspyatkin" data-src="https://secure.gravatar.com/avatar/c3eedb1858ca277ec99a4fa16d3c4356?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">aspyatkin</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/jim-driscoll-hi" title="View profile: jim-driscoll-hi">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="jim-driscoll-hi" data-src="https://secure.gravatar.com/avatar/ddd0a4199d30112782de1e77446262b0?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">jim-driscoll-hi</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/oul5oul" title="View profile: oul5oul">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="oul5oul" data-src="https://secure.gravatar.com/avatar/75f55db3c60e317d9ca3b2e9579b4552?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">oul5oul</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/Sidnicious" title="View profile: Sidnicious">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="Sidnicious" data-src="https://secure.gravatar.com/avatar/d37c1c5deea40e50c6f208a277532133?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">Sidnicious</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/arknave" title="View profile: arknave">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="arknave" data-src="https://secure.gravatar.com/avatar/e168b5df01ea7dd3dd1e5cc5dc62faed?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">arknave</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/charmander" title="View profile: charmander">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="charmander" data-src="https://secure.gravatar.com/avatar/ac1ec936c9c89794aec47829185a507d?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">charmander</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/Jeremie" title="View profile: Jeremie">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="Jeremie" data-src="https://secure.gravatar.com/avatar/ea74cf7f528273838138e9ae407cd8ef?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">Jeremie</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/kscarfone" title="View profile: kscarfone">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="kscarfone" data-src="https://secure.gravatar.com/avatar/89c1e54a9703125168e40d22316d2b49?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">kscarfone</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/sudheesh001" title="View profile: sudheesh001">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="sudheesh001" data-src="https://secure.gravatar.com/avatar/49f86b03caf39d2241d3f283bf893efa?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">sudheesh001</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/DirkjanOchtman" title="View profile: DirkjanOchtman">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="DirkjanOchtman" data-src="https://secure.gravatar.com/avatar/1b171ce1f20ce82a31999e843a7fe9b6?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">DirkjanOchtman</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/TwelveBaud" title="View profile: TwelveBaud">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="TwelveBaud" data-src="https://secure.gravatar.com/avatar/98e3c6eb7be4602fc04a67f8fa3f37f1?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">TwelveBaud</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/wagstaff" title="View profile: wagstaff">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="wagstaff" data-src="https://secure.gravatar.com/avatar/52964eafdc8afb12e72fa72a512a8f3c?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">wagstaff</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/EvanProdromou" title="View profile: EvanProdromou">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="EvanProdromou" data-src="https://secure.gravatar.com/avatar/d54b3146dc0b7e92cf252e508c280abd?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">EvanProdromou</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/zmi" title="View profile: zmi">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="zmi" data-src="https://secure.gravatar.com/avatar/71d1c1578f093c4a579e5cae99e7c2c8?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">zmi</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/Sheppy" title="View profile: Sheppy">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="Sheppy" data-src="https://secure.gravatar.com/avatar/945076feb88f2d7003891ffbf06f32bf?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">Sheppy</noscript></a>
</li>
<li class="hidden">
<a href="https://developer.mozilla.org/en-US/profiles/berkerpeksag" title="View profile: berkerpeksag">
<noscript data-width="34" data-height="34" data-class="avatar" data-alt="berkerpeksag" data-src="https://secure.gravatar.com/avatar/df8e51d7618d5ed7ccbbc8dea9a9afee?s=34&r=pg&d=https%3A%2F%2Fdeveloper.cdn.mozilla.net%2Fmedia%2Fimg%2Favatar.png">berkerpeksag</noscript></a>
</li>
</ul>
<button id="contributors-toggle" type="button" class="transparent" data-alternate-message="hide contributors">see all contributors</button></div>
<h1>Using server-sent events</h1>
</div>
<!-- start the main content container -->
<div id="wiki-column-container" class="wiki-right-present wiki-left-present">
<!-- additional controls row; hidden unless needed -->
<div id="wiki-controls" class="column-container column-container-reverse">
<div class="column-strip">
<!-- Show TOC "show" link -->
</div>
<div class="column-half">
</div>
<div class="column-strip quick-links hidden">
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#show-quick-links" class="title smaller" id="show-quick-links"><i aria-hidden="true" class="icon-caret-down"></i>Show Sidebar</a>
</div>
</div>
<!-- content row with three strips -->
<div class="column-container column-container-reverse">
<!-- TOC, approvals, etc -->
<div class="column-strip wiki-column" id="wiki-right">
<!-- table of contents -->
<div id="toc" class="toc toggleable fixed" style="width: 246px; top: 0px; max-height: 850px;">
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#toc" class="title toggler" data-open-icon="icon-plus" data-closed-icon="icon-minus" aria-haspopup="true">In This Article<i class="icon-minus"></i></a>
<ol class="toggle-container">
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Receiving_events_from_the_server" rel="internal">Receiving events from the server</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Sending_events_from_the_server" rel="internal">Sending events from the server</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Error_handling" rel="internal">Error handling</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Closing_event_streams" rel="internal">Closing event streams</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format" rel="internal">Event stream format</a><ol><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Fields" rel="internal">Fields</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Examples" rel="internal">Examples</a><ol><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Data-only_messages" rel="internal">Data-only messages</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Named_events" rel="internal">Named events</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Mixing_and_matching" rel="internal">Mixing and matching</a></li></ol></li></ol></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Browser_compatibility" rel="internal">Browser compatibility</a>
</li></ol>
</div>
</div>
<!-- center: main article content -->
<div id="wiki-content" class="column-half wiki-column text-content">
<!-- just the article content -->
<article id="wikiArticle">
<p></p><section id="Quick_Links" class="Quick_links"><!-- --></section><p></p>
<div class="summary">
<p>Developing a web application that uses server-sent events is quite easy. You'll need a bit of code on the server to stream the events to the web application, but the web application side of things works nearly identical to handling any other type of event.</p>
</div>
<h2 id="Receiving_events_from_the_server">Receiving events from the server<a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$edit#Receiving_events_from_the_server" class="button section-edit only-icon"><i aria-hidden="true" class="icon-pencil"></i><span>Edit</span></a></h2>
<p>The server-sent event API is contained in the <a href="https://developer.mozilla.org/en/Server-sent_events/EventSource" title="en/Server-sent events/EventSource"><code>EventSource</code></a> interface; to open a connection to the server to begin receiving events from it, create a new <a href="https://developer.mozilla.org/en/Server-sent_events/EventSource" title="en/Server-sent events/EventSource"><code>EventSource</code></a> object specifying the URI of a script that generates the events. For example:</p>
<pre class="brush: js line-numbers language-js"><code class=" language-js"><span class="token keyword">var</span> evtSource <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">EventSource</span><span class="token punctuation">(</span><span class="token string">"ssedemo.php"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="line-numbers-rows"><span></span></span></code></pre>
<p>If the event generator script is hosted on a different domain a new <a href="https://developer.mozilla.org/en/Server-sent_events/EventSource">EventSource </a>object should be created that specifies both the URI and options dictionary. For example, assuming the client script is on example.com:</p>
<pre class="brush: js"><code>var evtSource = new EventSource("//api.example.com/ssedemo.php", { withCredentials: true } ); </code></pre>
<div class="note">
<p><strong>Note:</strong> using <a href="https://developer.mozilla.org/en/Server-sent_events/EventSource">EventSource</a><a href="https://developer.mozilla.org/En/HTTP_access_control"> </a>is not supported by all browsers. Please check out the <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventSource#Browser_compatibility">Browser compatibility section</a>.</p>
</div>
<p>Once you've instantiated your event source, you can begin listening for messages:</p>
<pre class="brush: js line-numbers language-js"><code class=" language-js">evtSource<span class="token punctuation">.</span>onmessage <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">var</span> newElement <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">createElement</span><span class="token punctuation">(</span><span class="token string">"li"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
newElement<span class="token punctuation">.</span>innerHTML <span class="token operator">=</span> <span class="token string">"message: "</span> <span class="token operator">+</span> e<span class="token punctuation">.</span>data<span class="token punctuation">;</span>
eventList<span class="token punctuation">.</span><span class="token function">appendChild</span><span class="token punctuation">(</span>newElement<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>This code listens for incoming messages (that is, notices from the server that do not have an <code>event</code> field on them) and appends the message text to a list in the document's HTML.</p>
<p>You can also listen for events, using <code>addEventListener()</code>:</p>
<pre class="brush: js line-numbers language-js"><code class=" language-js">evtSource<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"ping"</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">var</span> newElement <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">createElement</span><span class="token punctuation">(</span><span class="token string">"li"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> obj <span class="token operator">=</span> JSON<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
newElement<span class="token punctuation">.</span>innerHTML <span class="token operator">=</span> <span class="token string">"ping at "</span> <span class="token operator">+</span> obj<span class="token punctuation">.</span>time<span class="token punctuation">;</span>
eventList<span class="token punctuation">.</span><span class="token function">appendChild</span><span class="token punctuation">(</span>newElement<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>This code is similar, except that it will be called automatically whenever the server sends a message with the <code>event</code> field set to "ping"; it then parses the JSON in the <code>data</code> field and outputs that information.</p>
<h2 id="Sending_events_from_the_server">Sending events from the server<a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$edit#Sending_events_from_the_server" class="button section-edit only-icon"><i aria-hidden="true" class="icon-pencil"></i><span>Edit</span></a></h2>
<p>The server-side script that sends events needs to respond using the MIME type text/event-stream. Each notification is sent as a block of text terminated by a pair of newlines. For details on the format of the event stream, see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format">Event stream format</a>.</p>
<p>The PHP code for the example we're using here follows:</p>
<pre class="brush: php line-numbers language-php"><code class=" language-php">date_default_timezone_set("America/New_York");
header("Content-Type: text/event-stream\n\n");
$counter = rand(1, 10);
while (1) {
// Every second, sent a "ping" event.
echo "event: ping\n";
$curDate = date(DATE_ISO8601);
echo 'data: {"time": "' . $curDate . '"}';
echo "\n\n";
// Send a simple message at random intervals.
$counter--;
if (!$counter) {
echo 'data: This is a message at time ' . $curDate . "\n\n";
$counter = rand(1, 10);
}
ob_end_flush();
flush();
sleep(1);
}<span class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>The code above generates an event every second, with the event type "ping". Each event's data is a JSON object containing the ISO 8601 timestamp corresponding to the time at which the event was generated. At random intervals, a simple message (with no event type) is sent.</p>
<h2 id="Error_handling">Error handling<a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$edit#Error_handling" class="button section-edit only-icon"><i aria-hidden="true" class="icon-pencil"></i><span>Edit</span></a></h2>
<p>When problems occur (such as a network timeout or issues pertaining to <a href="https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS" title="/en-US/docs/HTTP/Access_control_CORS">access control</a>), an error event is generated. You can take action on this programmatically by implementing the <code>onerror</code> callback on the EventSource object:</p>
<pre class="brush: js line-numbers language-js"><code class=" language-js">evtSource<span class="token punctuation">.</span>onerror <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"EventSource failed."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span><span class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>As of Firefox 22, it does not appear that there is any way to distinguish between different kinds of error events.</p>
<h2 id="Closing_event_streams">Closing event streams<a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$edit#Closing_event_streams" class="button section-edit only-icon"><i aria-hidden="true" class="icon-pencil"></i><span>Edit</span></a></h2>
<p>By default, if the connection between the client and server closes, the connection is reset. The connection is terminated with the <code>.close()</code> method.</p>
<pre class="line-numbers language-html"><code class=" language-html">evtSource.close();<span class="line-numbers-rows"><span></span></span></code></pre>
<h2 id="Event_stream_format">Event stream format<a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$edit#Event_stream_format" class="button section-edit only-icon"><i aria-hidden="true" class="icon-pencil"></i><span>Edit</span></a></h2>
<p>The event stream is a simple stream of text data which must be encoded using UTF-8. Messages in the event stream are separated by a pair of newline characters. A colon as the first character of a line is in essence a comment, and is ignored.</p>
<div class="note"><strong>Note:</strong> The comment line can be used to prevent connections from timing out; a server can send a comment periodically to keep the connection alive.</div>
<p>Each message consists of one or more lines of text listing the fields for that message. Each field is represented by the field name, followed by a colon, followed by the text data for that field's value.</p>
<h3 id="Fields">Fields</h3>
<p>The following field names are defined by the specification:</p>
<dl>
<dt><code>event</code></dt>
<dd>The event's type. If this is specified, an event will be dispatched on the browser to the listener for the specified event name; the web site source code should use <code>addEventListener()</code> to listen for named events. The <code>onmessage</code> handler is called if no event name is specified for a message.</dd>
<dt><code>data</code></dt>
<dd>The data field for the message. When the EventSource receives multiple consecutive lines that begin with <code>data:</code>, <a href="http://www.w3.org/TR/eventsource/#dispatchMessage" class="external external-icon">it will concatenate them</a>, inserting a newline character between each one. Trailing newlines are removed.</dd>
<dt><code>id</code></dt>
<dd>The event ID to set the <a href="https://developer.mozilla.org/en/Server-sent_events/EventSource" title="en/Server-sent events/EventSource"><code>EventSource</code></a> object's last event ID value.</dd>
<dt><code>retry</code></dt>
<dd>The reconnection time to use when attempting to send the event. [<em>What code handles this?</em>] This must be an integer, specifying the reconnection time in milliseconds. If a non-integer value is specified the field is ignored.</dd>
</dl>
<p>All other field names are ignored.</p>
<div class="note"><strong>Note:</strong> If a line doesn't contain a colon the entire line is treated as the field name with an empty value string.</div>
<h3 id="Examples">Examples</h3>
<h4 id="Data-only_messages">Data-only messages</h4>
<p>In the following example, there are three messages sent. The first is just a comment, since it starts with a colon character. As mentioned previously, this can be useful as a keep-alive if messages may not be sent regularly.</p>
<p>The second message contains a data field with the value "some text". The third message contains a data field with the value "another message\nwith two lines". Note the newline special character in the value.</p>
<pre class="line-numbers language-html"><code class=" language-html">: this is a test stream
data: some text
data: another message
data: with two lines<span class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="Named_events">Named events</h4>
<p>This example sends some named events. Each has an event name specified by the <code>event</code> field, and a <code>data</code> field whose value is an appropriate JSON string with the data needed for the client to act on the event. The <code>data</code> field could, of course, have any string data; it doesn't have to be JSON.</p>
<pre class="line-numbers language-html"><code class=" language-html">event: userconnect
data: {"username": "bobby", "time": "02:33:48"}
event: usermessage
data: {"username": "bobby", "time": "02:34:11", "text": "Hi everyone."}
event: userdisconnect
data: {"username": "bobby", "time": "02:34:23"}
event: usermessage
data: {"username": "sean", "time": "02:34:36", "text": "Bye, bobby."}<span class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="Mixing_and_matching">Mixing and matching</h4>
<p>You don't have to use just unnamed messages or typed events; you can mix them together in a single event stream.</p>
<pre class="line-numbers language-html"><code class=" language-html">event: userconnect
data: {"username": "bobby", "time": "02:33:48"}
data: Here's a system message of some kind that will get used
data: to accomplish some task.
event: usermessage
data: {"username": "bobby", "time": "02:34:11", "text": "Hi everyone."}<span class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="Browser_compatibility">Browser compatibility<a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$edit#Browser_compatibility" class="button section-edit only-icon"><i aria-hidden="true" class="icon-pencil"></i><span>Edit</span></a></h2>
<p></p><div class="htab">
<a name="AutoCompatibilityTable" id="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
<div id="compat-desktop" style="display: block;">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>EventSource support</td>
<td>9</td>
<td><a href="https://developer.mozilla.org/en-US/Firefox/Releases/6" title="Released on 2011-08-16.">6.0</a> (6.0)</td>
<td><span style="color: #f00;">Not supported</span></td>
<td>11</td>
<td>5</td>
</tr>
</tbody>
</table>
</div><div id="compat-mobile" style="display: none;">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>EventSource support</td>
<td>42</td>
<td><span title="Compatibility unknown; please update this." style="color: rgb(255, 153, 0);">?</span></td>
<td><span title="Compatibility unknown; please update this." style="color: rgb(255, 153, 0);">?</span></td>
<td>11.1</td>
<td>4</td>
</tr>
</tbody>
</table>
</div></div><p></p>
<p> </p>
</article>
<div class="share">
<strong>Share:</strong>
<ul>
<li><a href="http://twitter.com/share?url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FServer-sent_events%2FUsing_server-sent_events%3Futm_campaign%3Dshare%26utm_medium%3Ddoc%2Bshare%2Blink%26utm_source%3Dtwitter.com&text=I+learned+about+Using+server-sent+events+on+MDN." target="_blank" data-url="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events?utm_campaign=share&utm_medium=doc+share+link&utm_source=twitter.com" data-title="I learned about Using server-sent events on MDN." class="share-link twitter"><i aria-hidden="true" class="icon-twitter"></i>Twitter</a></li>
<li><a href="http://facebook.com/sharer.php?s=100&p[url]=https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events?utm_campaign=share&utm_medium=doc+share+link&utm_source=facebook.com" target="_blank" data-url="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events?utm_campaign=share&utm_medium=doc+share+link&utm_source=facebook.com" data-title="I learned about Using server-sent events on MDN." class="share-link facebook"><i aria-hidden="true" class="icon-facebook"></i>Facebook</a></li>
<li><a href="https://plus.google.com/share?url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FServer-sent_events%2FUsing_server-sent_events%3Futm_campaign%3Dshare%26utm_medium%3Ddoc%2Bshare%2Blink%26utm_source%3Dplus.google.com" target="_blank" data-url="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events?utm_campaign=share&utm_medium=doc+share+link&utm_source=plus.google.com" data-title="I learned about Using server-sent events on MDN." class="share-link gplus"><i aria-hidden="true" class="icon-google-plus"></i>Google+</a></li>
</ul>
</div>
<!-- contributors -->
<div class="wiki-block contributors">
<h2 class="offscreen">Document Tags and Contributors</h2>
<div class="tag-attach-list contributors-sub">
<i aria-hidden="true" class="icon-tags icon-fw"></i>
<strong>Tags:</strong>
<ul class="tags tags-small">
<li><a href="https://developer.mozilla.org/en-US/docs/tag/Advanced">Advanced</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/tag/DOM">DOM</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/tag/Guide">Guide</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/tag/Server-sent%20events">Server-sent events</a></li>
</ul>
</div>
<div class="contributors-sub">
<i aria-hidden="true" class="icon-group icon-fw"></i> <strong>Contributors to this page:</strong>
<a href="https://developer.mozilla.org/en-US/profiles/teoli">teoli</a>,
<a href="https://developer.mozilla.org/en-US/profiles/dsheiko">dsheiko</a>,
<a href="https://developer.mozilla.org/en-US/profiles/crandmck">crandmck</a>,
<a href="https://developer.mozilla.org/en-US/profiles/saikrishna1995">saikrishna1995</a>,
<a href="https://developer.mozilla.org/en-US/profiles/openmando">openmando</a>,
<a href="https://developer.mozilla.org/en-US/profiles/afoster">afoster</a>,
<a href="https://developer.mozilla.org/en-US/profiles/chrisdavidmills">chrisdavidmills</a>,
<a href="https://developer.mozilla.org/en-US/profiles/aspyatkin">aspyatkin</a>,
<a href="https://developer.mozilla.org/en-US/profiles/jim-driscoll-hi">jim-driscoll-hi</a>,
<a href="https://developer.mozilla.org/en-US/profiles/oul5oul">oul5oul</a>,
<a href="https://developer.mozilla.org/en-US/profiles/Sidnicious">Sidnicious</a>,
<a href="https://developer.mozilla.org/en-US/profiles/arknave">arknave</a>,
<a href="https://developer.mozilla.org/en-US/profiles/charmander">charmander</a>,
<a href="https://developer.mozilla.org/en-US/profiles/Jeremie">Jeremie</a>,
<a href="https://developer.mozilla.org/en-US/profiles/kscarfone">kscarfone</a>,
<a href="https://developer.mozilla.org/en-US/profiles/sudheesh001">sudheesh001</a>,
<a href="https://developer.mozilla.org/en-US/profiles/DirkjanOchtman">DirkjanOchtman</a>,
<a href="https://developer.mozilla.org/en-US/profiles/TwelveBaud">TwelveBaud</a>,
<a href="https://developer.mozilla.org/en-US/profiles/wagstaff">wagstaff</a>,
<a href="https://developer.mozilla.org/en-US/profiles/EvanProdromou">EvanProdromou</a>,
<a href="https://developer.mozilla.org/en-US/profiles/zmi">zmi</a>,
<a href="https://developer.mozilla.org/en-US/profiles/Sheppy">Sheppy</a>,
<a href="https://developer.mozilla.org/en-US/profiles/berkerpeksag">berkerpeksag</a>
</div>
<div class="contributors-sub">
<i aria-hidden="true" class="icon-clock-o icon-fw"></i> <strong>Last updated by:</strong>
<a href="https://developer.mozilla.org/en-US/profiles/teoli">teoli</a>,
<time datetime="2015-09-17T04:41:54-07:00">Sep 17, 2015, 4:41:54 AM</time>
</div>
</div>
</div>
<!-- quick links and zone subnav strip -->
<div id="wiki-left" class="column-strip wiki-column">
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#quick-links" class="title smaller" id="quick-links-toggle"><i aria-hidden="true" class="icon-caret-up"></i>Hide Sidebar</a>
<!-- quick links -->
<div class="quick-links" id="quick-links">
<div class="title see-also">See also</div>
<ol><li><strong><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events">Server-sent events</a></strong></li><li data-default-state="open" class="toggleable current" data-closed=""><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#" class="toggler" aria-haspopup="true"><i aria-hidden="true" class="icon-caret-down"></i><strong>Guides</strong></a><ol class="toggle-container" aria-expanded="true" style="display: block;"><li><a href="./Using server-sent events - Web APIs _ MDN_files/Using server-sent events - Web APIs _ MDN.html" title="The server-sent event API is contained in the EventSource interface; to open a connection to the server to begin receiving events from it, create a new EventSource object specifying the URI of a script that generates the events. For example:">Using server-sent events</a></li></ol></li><li data-default-state="open" class="toggleable current" data-closed=""><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#" class="toggler" aria-haspopup="true"><i aria-hidden="true" class="icon-caret-down"></i><strong>Interfaces</strong></a><ol class="toggle-container" aria-expanded="true" style="display: block;"><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/EventSource"><code>EventSource</code></a></li></ol></li><li data-default-state="open" class="toggleable current" data-closed=""><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#" class="toggler" aria-haspopup="true"><i aria-hidden="true" class="icon-caret-down"></i><strong>Events</strong></a><ol class="toggle-container" aria-expanded="true" style="display: block;"><li><a href="https://developer.mozilla.org/en-US/docs/Web/Events/open"><code>open</code></a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/Events/message"><code>message</code></a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/Events/error"><code>error</code></a></li></ol></li></ol>
</div>
<!-- approvals -->
</div>
</div>
</div>
</div>
</div> <!-- ends "main-content" -->
<menu type="context" id="edit-history-menu">
<menuitem data-action="/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$edit" label="Edit page"></menuitem>
<menuitem data-action="/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events$history" label="View page history"></menuitem>
</menu>
</div></main>
<!-- Footer -->