RealTime News Updates in SharePoint Online with SPFx
post
Recently, I discovered the Socket.IO endpoint in Microsoft Graph, which has enabled me to implement real-time functionality in SharePoint Online. This feature allows SPFx solutions and other types of applications to receive near-instantaneous change notifications for drives and lists, facilitating dynamic updates without manual refreshes.
alert
It’s important to note that utilizing this approach requires establishing a separate WebSocket connection for each library or list you wish to monitor. This can lead to multiple concurrent connections, which may impact performance and resource utilization.
Integrating real-time updates into SharePoint Online solutions enhances user engagement by ensuring content is always up to date. The approach utilizes SharePoint REST API endpoints to establish a WebSocket connection via Socket.IO, enabling dynamic updates without relying on Microsoft Graph (that way you do not have to request permissions).
note
If you want, you can change the code in the upcoming code snippets to make use of the Microsoft Graph
Implementing real-time news updates in SharePoint online
The provided code sample demonstrates how to create a React functional component within the SharePoint Framework (SPFx) that listens for changes in a SharePoint site pages library and updates the UI accordingly. Here’s a breakdown of the implementation:
Fetching pages
The fetchPages function retrieves the latest pages from the SharePoint site’s sitepages library using the SPHttpClient.
The fetchNotificationUrl function constructs the API URL to retrieve the notificationUrl for the specific list. This URL is essential for establishing a WebSocket connection.
The Socket.IO endpoint provides you with a URL that you can use to establish a WebSocket connection. This connection will allow you to receive real-time updates for the specific list or library you are monitoring.
Establishing a WebSocket connection
Upon obtaining the notificationUrl, a WebSocket connection is established using Socket.IO. The component listens for ’notification’ events and updates the list of pages when changes occur.
First install the socket.io-client package:
1
npm install socket.io-client
Then, you can establish the WebSocket connection as shown below:
React.useEffect(()=>{if(!notificationUrl){return;}// Make sure to define the `transports` option to only use the WebSocket transport
// It will fail if not defined, or when using polling
constsocket=io(notificationUrl,{transports:['websocket']});socket.on('connect',()=>{console.log('Connected to the notification service');setListening(true);});socket.on('notification',(data)=>{console.log('Notification received:',data);fetchPages();});return()=>{socket.disconnect();};},[notificationUrl]);
Initializing the data fetching
The useEffect hook ensures that the component fetches the initial set of pages and the notification URL when it mounts.
import{WebPartContext}from'@microsoft/sp-webpart-base';import*asReactfrom'react';import{io}from'socket.io-client';import{SPHttpClient}from'@microsoft/sp-http';exportinterfaceIRealTimeNewsProps{context: WebPartContext;}exportconstRealTimeNews: React.FunctionComponent<IRealTimeNewsProps>=({context}:React.PropsWithChildren<IRealTimeNewsProps>)=>{const[listening,setListening]=React.useState(false);const[pages,setPages]=React.useState<{Title: string,Description:[],AbsoluteUrl: string}[]>([]);const[notificationUrl,setNotificationUrl]=React.useState<string|null>(null);constfetchPages=React.useCallback(async()=>{constapiUrl=`${context.pageContext.web.absoluteUrl}/_api/sitepages/pages?$select=Title,Description,AbsoluteUrl&$top=5&$orderby=Modified desc`;constresponse=awaitcontext.spHttpClient.get(apiUrl,SPHttpClient.configurations.v1);if(!response.ok){return;}constdata=awaitresponse.json();setPages(data.value);},[context]);constfetchNotificationUrl=React.useCallback(async()=>{constlistId=context.pageContext.list?.id;if(!listId){return;}constapiUrl=`${context.pageContext.web.absoluteUrl}/_api/v2.0/sites('${location.host},${context.pageContext.site.id},${context.pageContext.web.id}')/lists('${listId}')/drive/root/subscriptions/socketIo`;constresponse=awaitcontext.spHttpClient.get(apiUrl,SPHttpClient.configurations.v1);if(!response.ok){return;}constdata=awaitresponse.json();setNotificationUrl(data.notificationUrl);},[context]);React.useEffect(()=>{if(!notificationUrl){return;}constsocket=io(notificationUrl,{transports:['websocket']});socket.on('connect',()=>{console.log('Connected to the notification service');setListening(true);});socket.on('notification',(data)=>{console.log('Notification received:',data);// eslint-disable-next-line @typescript-eslint/no-floating-promises
fetchPages();});},[notificationUrl]);React.useEffect(()=>{// eslint-disable-next-line @typescript-eslint/no-floating-promises
fetchPages();// eslint-disable-next-line @typescript-eslint/no-floating-promises
fetchNotificationUrl();},[notificationUrl]);return(<div><h3>Real-TimeNews</h3><p>Status:{listening?'Listening for changes...':'Not connected'}</p><ul>{pages.map((page,index)=>(<likey={index}><strong>{page.Title}</strong>:{page.Description}-<ahref={page.AbsoluteUrl}target="_blank"rel="noopener noreferrer">Readmore</a></li>))}</ul></div>);};
Alternative approach using Microsoft Graph
While your current implementation avoids the need for additional permissions by utilizing SharePoint REST API endpoints, it’s worth noting that similar functionality can be achieved through Microsoft Graph. However, this approach requires specific permissions to access the necessary resources.
To use Microsoft Graph for real-time notifications, you would need to request permissions such as Sites.Read.All and Files.Read. These permissions must be declared in your solution’s package-solution.json file under the webApiPermissionRequests property:
After deploying your solution, an administrator must approve these permissions in the SharePoint admin center. This process is detailed in the Microsoft documentation.
Conclusion
By leveraging SharePoint REST API endpoints and Socket.IO, you can implement real-time updates in your SharePoint Online environment without the need for additional permissions. This approach simplifies the deployment process and maintains dynamic content for users. Alternatively, integrating with Microsoft Graph offers a robust solution but requires appropriate permissions and administrative consent.