sse.js
sse.js copied to clipboard
Sending custom headers
I see there are multiple PR's for adding custom request headers. However, whenever I try sending up custom headers with this library in angular I cant seem to get any message to the client when I do client.send("Testing!")
. Even though it is making the connection.
However it seems to work just fine when I dont send up any headers. Has anyone run into this issue before?
Client:
this.eventSource = new EventSourcePolyfill('http://localhost:3000/sse',
{ headers:
{ 'Authorization': `Bearer ${localStorage.getItem('token')}`,
'ocp-apim-subscription-key': environment.worklistApiSubKey }
});
Server:
export function initializeSSE(server: Server) {
sse = new SSE(server);
sse.on('connection', (client: any) => {
log.info(`new clint connected ${client.req.url}`);
client.send({data: 'Sup Kyle!'});
});
}
Ive also added supporting for custom header on the initialization
SSEClient.prototype.initialize = function() {
this.req.socket.setNoDelay(true);
this.res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache, no-transform',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Authorization, ocp-apim-subscription-key'
});
this.res.write(':ok\n\n');
};