{"action":"created","issue":{"url":"https://api.github.com/repos/whatwg/html/issues/1271","repository_url":"https://api.github.com/repos/whatwg/html","labels_url":"https://api.github.com/repos/whatwg/html/issues/1271/labels{/name}","comments_url":"https://api.github.com/repos/whatwg/html/issues/1271/comments","events_url":"https://api.github.com/repos/whatwg/html/issues/1271/events","html_url":"https://github.com/whatwg/html/issues/1271","id":155476080,"number":1271,"title":"WebSockets: Message received before binding onmessage","user":{"login":"dbrgn","id":105168,"avatar_url":"https://avatars.githubusercontent.com/u/105168?v=3","gravatar_id":"","url":"https://api.github.com/users/dbrgn","html_url":"https://github.com/dbrgn","followers_url":"https://api.github.com/users/dbrgn/followers","following_url":"https://api.github.com/users/dbrgn/following{/other_user}","gists_url":"https://api.github.com/users/dbrgn/gists{/gist_id}","starred_url":"https://api.github.com/users/dbrgn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dbrgn/subscriptions","organizations_url":"https://api.github.com/users/dbrgn/orgs","repos_url":"https://api.github.com/users/dbrgn/repos","events_url":"https://api.github.com/users/dbrgn/events{/privacy}","received_events_url":"https://api.github.com/users/dbrgn/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/whatwg/html/labels/clarification","name":"clarification","color":"fef2c0"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-05-18T11:16:02Z","updated_at":"2016-05-18T12:27:05Z","closed_at":null,"body":"If I understand [this spec](https://html.spec.whatwg.org/multipage/comms.html#the-websocket-interface:concept-websocket-establish) correctly, then messages that arrive before the `onmessage` event handler is bound in the browser will be lost.\r\n\r\nThe usual workflow for listening to WebSockets is as follows:\r\n\r\n```javascript\r\nvar ws = WebSocket('wss://...');\r\nws.onmessage = function(event) { ... };\r\n```\r\n\r\nBut there appears to be no way to specify an event handler before the connection is opened.\r\n\r\nI'm working on a client library for [SaltyRTC](https://github.com/saltyrtc/saltyrtc-meta), where the protocol currently sends a \"server-hello\" message as soon as a client connects. That requires the client implementation to listen to events as soon as the connection is up.\r\n\r\nTo test this issue in the browser (latest Chromium and Firefox), I used this snippet:\r\n\r\n```javascript\r\nws = new WebSocket('wss://...', 'saltyrtc-1.0'); ws.binaryType = 'arraybuffer'; setTimeout(function() { ws.onmessage = function(ev) { console.info(ev); }}, 350)\r\n```\r\n\r\nUp to a delay of around 350-400ms, the message is logged, but above that the message is lost.\r\n\r\nWe'd like to avoid adding an explicit client-hello message to the protocol because it is unnecessary and adds another round-trip.\r\n\r\nWhat is the usual approach to avoid the loss of messages? Is this a non-issue in real life?\r\n\r\n---\r\n\r\nAs a sidenote, it appears that in the [MessagePort spec](https://html.spec.whatwg.org/multipage/comms.html#examples-5), the connection must be opened explicitly using `.start()` or implicitly by setting the `.onmessage` handler."},"comment":{"url":"https://api.github.com/repos/whatwg/html/issues/comments/220010740","html_url":"https://github.com/whatwg/html/issues/1271#issuecomment-220010740","issue_url":"https://api.github.com/repos/whatwg/html/issues/1271","id":220010740,"user":{"login":"dbrgn","id":105168,"avatar_url":"https://avatars.githubusercontent.com/u/105168?v=3","gravatar_id":"","url":"https://api.github.com/users/dbrgn","html_url":"https://github.com/dbrgn","followers_url":"https://api.github.com/users/dbrgn/followers","following_url":"https://api.github.com/users/dbrgn/following{/other_user}","gists_url":"https://api.github.com/users/dbrgn/gists{/gist_id}","starred_url":"https://api.github.com/users/dbrgn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dbrgn/subscriptions","organizations_url":"https://api.github.com/users/dbrgn/orgs","repos_url":"https://api.github.com/users/dbrgn/repos","events_url":"https://api.github.com/users/dbrgn/events{/privacy}","received_events_url":"https://api.github.com/users/dbrgn/received_events","type":"User","site_admin":false},"created_at":"2016-05-18T12:27:05Z","updated_at":"2016-05-18T12:27:05Z","body":"Thanks for the clarification! Yes, a short sentence about this situation would probably benefit future readers :)"}}