I'm a beginner in Rust and WebSockets and I'm trying to deploy on Heroku a little chat backend I wrote (everything works on localhost). The build went well and I can see the app is running, and I'm now trying to connect to the WebSocket from a local HTML/Javascript frontend, but it is not working.
Here is my code creating the WebSocket on my rust server on Heroku (using the tungstenite WebSocket crate):
async fn main() -> Result<(), IoError> {
let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string());
let addr = format!("0.0.0.0:{}", port);
// Create the event loop and TCP listener we'll accept connections on.
let try_socket = TcpListener::bind(&addr).await;
let listener = try_socket.expect("Failed to bind");
println!("Listening on: {}", addr);
and here is the code in my Javascript file that tries to connect to that WebSocket:
var ws = new WebSocket("wss://https://myappname.herokuapp.com/");
My web client gets the following error in the console:
WebSocket connection to 'wss://https//rocky-wave-51234.herokuapp.com/' failed
I searched to find the answer to my issue but unfortunately didn't find a fix so far. I've found hints that I might have to create an HTTP server first in my backend and then upgrade it to a WebSocket, but I can't find a resource on how to do that and don't even know if this is in fact the answer to my problem. Help would be greatly appreciated, thanks!
I think your mistake is the URL you use:
"wss://https://myappname.herokuapp.com/"
A URL usually starts with <protocol>://. The relevant protocols here are:
http - unencrypted hypertext
https - encrypted hypertext
ws - unencrypted websocket
wss - encrypted websocket
So if your URL is an encrypted websocket, it should start only with wss://, a connection cannot have multiple protocols at once:
"wss://myappname.herokuapp.com/"
I try to ping/pong json packages between a FastAPI websocket endpoint and a client using the websocket library from Zephyr RTOS. Unfortunately the connection is closed by the FastAPI server after ~40 seconds. This time seems to be constant. My guess is, that there must occur a timeout event due to a missing/wrong condition.
This post seems to address my issue. Unfortunately my code is not running with a newer uvicorn version which would support --ws-ping-interval or --ws-ping-timeout.
For a simple test I did the following:
Server side:
#app.websocket("/ws/update_status")
async def websocket_endpoint(websocket: WebSocket, db: Session = Depends(get_db)):
await websocket.accept()
while True:
data = await websocket.receive_json()
await websocket.send_json('{"status":"ok"}')
Client side (C code running on Zephyr-RTOS):
// some code to define JSON package
while(1)
{
websocket_send_msg(sock, send_buffer, json_length, WEBSOCKET_OPCODE_DATA_TEXT, true, true, SYS_FOREVER_MS);
websocket_recv_msg(sock, receive_buffer, sizeof(receive_buffer), &message_type, 0, SYS_FOREVER_MS);
}
I use the following package versions:
uvicorn=='0.13.4'
fastapi=='0.68.2'
Does anyone know why the connection aborts after 40 seconds and how it can be avoided?
I have two WebSocket servers that can communicate wonderfully with a client. They are on two separate machines, implemented in Java and running inside WildFly8 webservers. What I need them to do now is communicate with each other. That means: client sends message to server 1, server 1 sends message to server 2, receives the reply and sends it back to client.
The servers run on different apps in OpenShift and I need them to use websockets. Or some other type of communication, but I haven't managed to find anything that actually works so far (RMI or normal socket connections won't work).
What I basically tried to do is use the same code from the client within the onMessage method of the first server. Something like this:
#OnMessage
public void message(Session session, String msg){
...
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
Session NewSession = container.connectToServer(Client.class, URI.create(URL));
NewSession.getBasicRemote().sendText("Routed :" + input);
...
}
However, the server does not connect to the other server and I don't know why.
Any suggestions?
Thank you!
Put connectToServer inside a try {} catch, you might get an error. Log it.
I'm struggling to do exactly the same things (2 websocket servers, Wildfly 8), and I get a permission denied error. See my post here:
https://stackoverflow.com/questions/30966757/java-server-to-server-communication-with-websockets-permission-denied
How to broadcast a message among all the connected clients in socket.io?
I am using mrniko/netty-cosket.io server with with java (play/eclipse) on server side and socket.io.js 0.9 on client side. Both client & server working fine (i.e. sending and receiving messages). To keep the records of the connected clients , I am using hashMaps and to send a message to all connected users I am iterating the hashmap and sending messages one by one. just wanna know is there any better way to send messages to all connected clients at once?
Any help will be highly appreciated.
Here is an example in case of mrniko/netty-socket.io usage:
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);
SocketIOServer server = new SocketIOServer(config);
BroadcastOperations bo = server.getBroadcastOperations();
// every method will broadcast messages to all server clients
bo.sendJsonObject(...)
server.start();
...
you can use the following code:
//broadcasts to all clients connected
io.sockets.emit('function', {foo:bar});
//broadcasts to all clients connected, except the sender
io.sockets.on('connection', function (socket) {
socket.on('message', function(data) {
socket.broadcast.emit('function', {foo:bar});
}
}
Is there any way to disconnect a client with SocketIO, and literally close the connection? So if someone is connected to my server, and I want to close the connection between them and my server, how would I go about doing that?
Edit: This is now possible
You can now simply call socket.disconnect() on the server side.
My original answer:
This is not possible yet.
If you need it as well, vote/comment on this issue.
socket.disconnect() can be used only on the client side, not on the server side.
Client.emit('disconnect') triggers the disconnection event on the server, but does not effectively disconnect the client. The client is not aware of the disconnection.
So the question remain : how to force a client to disconnect from server side ?
Any reason why you can't have the server emit a message to the client that makes it call the disconnect function?
On client:
socket.emit('forceDisconnect');
On Server:
socket.on('forceDisconnect', function(){
socket.disconnect();
});
Checking this morning it appears it is now:
socket.close()
https://socket.io/docs/client-api/#socket-close
For those who found this on google - there is a solution for this right now:
Socket.disconnect() kicks the client (server-side). No chance for the client to stay connected :)
Assuming your socket's named socket, use:
socket.disconnect()
This didn't work for me:
`socket.disconnect()`
This did work for me:
socket.disconnect(true)
Handing over true will close the underlaying connection to the client and not just the namespace the client is connected to Socket IO Documentation.
An example use case: Client did connect to web socket server with invalid access token (access token handed over to web socket server with connection params). Web socket server notifies the client that it is going to close the connection, because of his invalid access token:
// (1) the server code emits
socket.emit('invalidAccessToken', function(data) {
console.log(data); // (4) server receives 'invalidAccessTokenEmitReceived' from client
socket.disconnect(true); // (5) force disconnect client
});
// (2) the client code listens to event
// client.on('invalidAccessToken', (name, fn) => {
// // (3) the client ack emits to server
// fn('invalidAccessTokenEmitReceived');
// });
In my case I wanted to tear down the underlying connection in which case I had to call socket.disconnect(true) as you can see is needed from the source here
client._onDisconnect() should work
I'm using client.emit('disconnect') + client.removeAllListeners() for connected client for ignore all events after disconnect
Socket.io uses the EventEmitter pattern to disconnect/connect/check heartbeats so you could do. Client.emit('disconnect');
I am using on the client side socket.disconnect();
client.emit('disconnect') didnt work for me
You can do socket = undefined in erase which socket you have connected. So when want to connected do socket(url)
So it will look like this
const socketClient = require('socket.io-client');
let socket;
// Connect to server
socket = socketClient(url)
// When want to disconnect
socket = undefined;
I have using the socket client on React Native app, when I called socketIOClient.disconnect() this disconnects from the server but when I connect to the socket again the previous events were connected again, and the below code works for me by removing all existing events and disconnecting socket conneciton.
socketIOClient.removeAllListeners();
socketIOClient.disconnect(true);
To disconnect socket forcefully from server side
socket.disconnect(true)
OR
To disconnect socket by client side event
On client:
socket.emit('forceDisconnect');
On Server:
socket.on('forceDisconnect', function(){
socket.disconnect(true);
});
You can call socket.disconnect() on both the client and server.
Add new socket connections to an array and then when you want to close all - loop through them and disconnect. (server side)
var socketlist = [];
io.sockets.on('connection', function (socket) {
socketlist.push(socket);
//...other code for connection here..
});
//close remote sockets
socketlist.forEach(function(socket) {
socket.disconnect();
});
use :
socket.Disconnect() //ok
do not use :
socket.disconnect()