How to close connection to sse flux with javascript-client? - javascript

I build a server-send-event endpoint with spring webflux. My javascript app subscribes to that endpoint and receives the published events correctly. BUT when call
EventSource.close() it seems the publisher is not informed that the client closed the connection.
Hence, the publisher is still thinking that there is a subscription, and publishes events to that.
The event stream is potentially never ending, so I never get a complete signal on the publisher side, which means the client has to close the connection.
I think that this happens because the close() call can't reach the underlying flux.
Do you have any solution or workaround for this problem?
#GetMapping(value = INSTANCE_UPDATE_ENDPOINT + "/{dealId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<InstanceUpdatedQuery>> getInstanceUpdateEventStreamForDeal(#PathVariable final String dealId) {
return this.instanceUpdatedApplicationEventFlux
.filter(instanceUpdatedEvent -> instanceUpdatedEvent.getDealId().equals(dealId))
.map(instanceUpdatedEvent -> ServerSentEvent.<InstanceUpdatedQuery>builder()
.id(instanceUpdatedEvent.getId())
.retry(Duration.ofSeconds(1))
.data(new InstanceUpdatedQuery(instanceUpdatedEvent))
.build())
}

You should add a handler to catch the doOnNext(Signal) and then detect the close signal and respond to it

Related

multiple browser instances with websocket capabilities in node?

Let's say I am building a social app. I want to log into multiple accounts (one per browser instance) without an user interface (all via node), and by calling all respective endpoints to log in and start chatting.
The important part is to test when an user closed the tab or logs out or leaves the group and therefore the websocket's connection closes.
If I understand you correctly.
You would like to make a server-side event happen whenever a client connects or disconnects, without any html,css.... or any other user interface.
You can do it like this in node :
For connection you use :
Server.on("connection,function(ws){some stuff...})
The function that is called on connection will get the websocket that connected as parameter by default. I just use a lambda function here you can also call one that will then get the websocket as parameter.
For disconnection you put a function in the Server.on function to monitor when the client disconnected. Like this :
Server.on("connection,function(ws){
ws.onclose = function (ws) {
some stuff...
}
})
You can again replace the lambda function by another one.
Server is in my case equal to this :
const wsserver = require("ws").Server
const server = new wsserver({ port: someport })
But it can vary.
All you need to do apart from that is connect the client.
I do it like this but it can vary as well.
const ws = new WebSocket("ws://localhost:someport");
I hope this is helpful.

How to keep a websocket connection in OPEN readystate after send?

I am new to websockets.
In my setup I have a trivial websocket server written in Go (playground)
I make a WebSocket object, set up its onmessage callback and call its send method to test.
var w = new WebSocket("ws://localhost:12345/echo")
w.onmessage = (msg) => {
console.log(msg.data)
}
w.onopen = () => {
w.send("Hello") // this fires OK
}
What I expect to happen based on the server code is to receive the "Hello" message and to keep sending "yahoo" every 1.5s to the client. What actually happens is "Hello" is sent, but none of the "yahoo"'s make it thru. It seems somewhere along the WebSocket.readystate becomes 3 (CLOSED).
To clarify, the server receives and prints "Hello" then actually fires a "yahoo" message every 1.5s, but the connection is closed by then so the onmessage callback never fires.
Am I missing or misunderstanding anything?
EDIT: Ran across comparison github.com/gorilla vs. golang.org/x/net, claims golang.org/x/net websocket implementation does not support pong. This may be the confirmation of it.
EDIT: Package golang.org/x/net/websocket closes the websocket connection when the handler ServeHTTP function returns. By default a websocket connection is tied to an instance of the handler.
When the handler function returns, in your case EchoServer the socket will be automatically closed by the http framework.
Since you start a go routine for the loop writing the yahooresponse to the client the EchoServer function will terminate (and therefor closing the socket) before it has time to send a response.
The solution is to remove the spawning of the go routine and just do the loop inside the EchoServer.

Client side SignalR on reconnect doesn't receive messages

I have SignalR Hub running on a server being run as a service. This server for whatever reason, may stop due to power loss, being updated (we use Octopus to automatically update), or whatever.
If I have a user connected to this service sending messages to the server, and then in turn, forwarding this message to a sensor. The sensor then returns a message to the server, and gets forwarded on to the client webapp.
This works fine on the first connection. If the service is stopped and restarted before the reconnect times out, I get my reconnect with the same connection id, and I can send messages. I can't however, receive messages.
If I refresh the page, I will get them as this creates a new connection. The same code is called whether it is OnConnect() or OnReconnect().
Example
public override Task OnConnected()
{
EstablishConnection("Connect");
return base.OnConnected();
}
public override Task OnReconnected()
{
// Remove old Connection Id to receive messages on reconnect
ConnectionsHandler.Instance.RemoveTerminalClient(Context.ConnectionId);
EstablishConnection("Reconnect");
// This block is to tell the user that connection has been reconnected. This message shows on the webapp so connection has been restored.
var hubContext = GlobalHost.ConnectionManager.GetHubContext<TerminalHub>();
var message = TerminalMessageColorer.ColorMessageLime(String.Format("Connection has been re-established!"));
hubContext.Clients.Client(Context.ConnectionId).TerminalEcho(message);
return base.OnReconnected();
}
private void EstablishConnection(string conType)
{
ConnectionsHandler.Instance.AddTerminalClient(Context.ConnectionId, "null");
var terminal = ConnectionsHandler.Instance.GetTerminalClient(Context.ConnectionId);
#if DEBUG
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{0}\t{1}\t...{2}", terminal, Context.ConnectionId, conType);
#endif
}
I found what the problem was. I needed to wrap a $timeout in the client side code as there seems to be a little delay in the C# code. I stumbled across this from the chrome dev tools once I was debugging the initializer, thinking something had to be wrong that the user id wasn't being passed. Once I added a breakpoint at the call to initialize, it would go there so that work, but continuing from there I would receive messages afterwards. Without the breakpoint I wouldn't. So I added the $timeout around the call.
$timeout(function () {
initializeTerminal();
}, 100);
does not work - due to jquery version
<script src="~/Scripts/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="~/Scripts/signalr/jquery.signalR-2.4.2.js"></script>
<script src="~/signalr/hubs"></script>
<script src="~/Areas/Noc/NocDOCSIS/Scripts/script.noc.light.js"></script>
works - due to Jquery version
<script src="~/Scripts/jquery-2.2.4/jquery-2.2.4.min.js"></script>
<script src="~/Scripts/signalr/jquery.signalR-2.4.2.js"></script>
<script src="~/signalr/hubs"></script>
<script src="~/Areas/Noc/NocDOCSIS/Scripts/script.noc.light.js"></script>

Emit an event with node.js socket client to sails.js (0.11.x) [duplicate]

This question already has an answer here:
Emitting a message in sails v0.11 (client-side)
(1 answer)
Closed 6 years ago.
The server: sails.js (0.11.x) is the server
The client: A node.js script with sails.io#0.11.5 and socket.io-client#1.3.5
Big picture: I have, or will have, a farm of node.js scripts that connect to the sails.js server and will perform various tasks.
Immediate Goal: I want to emit an event during a socket connection from client->server such as:
socket.emit('got_job', job.id);
Why? If this is possible I can create various event handlers on the server side in one controller (or controller + service) and keep my code clean while managing a set of stateful transactions between client/server endpoints for supporting this script farm.
The documentation: This is how one goes about using socket.io-client for sails.js this per sails docs: https://github.com/balderdashy/sails.io.js?files=1#for-nodejs
I haven't much code to share other than what's in that link, but I'll paste it here just in case:
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');
// Instantiate the socket client (`io`)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);
// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';
// ...
// Send a GET request to `http://localhost:1337/hello`:
io.socket.get('/hello', function serverResponded (body, JWR) {
// body === JWR.body
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
// When you are finished with `io.socket`, or any other sockets you connect manually,
// you should make sure and disconnect them, e.g.:
io.socket.disconnect();
// (note that there is no callback argument to the `.disconnect` method)
});
What I have looked into: I've drilled into various levels of these objects and I can't seem to find anything exposed to use. And simply trying io.socket.emit() as it doesn't exist. But io.socket.get() and io.socket.post(), etc work fine.
console.log(socketIOClient);
console.log(sailsIOClient);
console.log(io);
console.log(io.socket._raw);
console.log(io.sails);
Thanks, and I'll try to update this as needed for clarification.
UPDATE:
Misc Server Info.:
I'm using nginx on port 443, with SSL termination, pointing to 4 (and
soon more) sails.js instances on separate ports (3000-3003).
I'm also using Redis for sessions and sockets.
You're close:
Your io.socket.get call is kind of like a rest api call. You'd need a sails controller bound to a get request on the url '/hello',
//client
io.socket.get('/hello', function serverResponded (body, JWR) {
//this is the response from the server, not a socket event handler
console.dir(body);//{message:"hello"}
});
in
config/routes.js:
{
'get /hello':'MyController.hello'
}
//controllers/MyController
{
hello:function(req,res){
res.json({message:"hello"});
}
}
The method you're looking for is, io.socket.on('eventName');
Here's an example:
//client
io.socket.get('/hello', function serverResponded (body, JWR) {
//all we're doing now is subscribing to a room
console.dir(body);
});
io.socket.on('anevent',function(message){
console.dir(message);
});
in
config/routes.js:
{
'get /hello':'MyController.hello'
}
//controllers/MyController
{
hello:function(req,res){
sails.sockets.join(req.socket,'myroom');
res.json({message:'youve subscribed to a room'});
}
}
What we've effectively done is, setup our socket to be part of a "room", which is basically an event namespace. Then, some other controller only has to do this:
sails.sockets.broadcast('myroom','anevent',{message:'socket event!'});
After this call is made, you would receive the message in the callback of io.socket.on('anevent').

Meteor client disconnected event on server

Simple question, maybe simple answer: how do I know on the server that a certain client has disconnected? Basic use case: the serve would need to know if a player has dropped the connection.
In the publish function, you can watch socket close event as follows.
this.session.socket.on "close", -> # do your thing
Meteor.publish("yourPublishFunction", function()
{
var id = this._session.userId;
this._session.socket.on("close", Meteor.bindEnvironment(function()
{
console.log(id); // called once the user disconnects
}, function(e){console.log(e)}));
return YourCollection.find({});
});
I've created a pretty comprehensive package to keep track of all logged-in sessions from every user, as well as their IP addresses and activity:
https://github.com/mizzao/meteor-user-status
To watch for disconnects, you can just do the following, which catches both logouts and browser closes:
UserStatus.on "sessionLogout", (userId, sessionId) ->
console.log(userId + " with session " + sessionId + " logged out")
You can also check out the code and do something similar for yourself.
Maybe (in the server code)
Meteor.default_server.sessions.length
or
Meteor.default_server.stream_server.open_sockets.length
you can do one thing make a event on server and call it from browser with ajax which call after some small time interval settimeinterval using with session values into header and if server din`t get request from user it means he dropped connection

Categories